CakePHP-baseLoginDirect

CakePHP-baseLoginDirect,php,cakephp,authentication,cakephp-2.0,Php,Cakephp,Authentication,Cakephp 2.0,我有许多身份验证组件,它们扩展了BaseAuthenticate类。这些是以正常方式在AppController中设置的 身份验证组件是否可以更改AuthComponent的LoginDirect变量? 为了澄清这种情况,我的一个身份验证组件查看了特定的用户子集。它先检查凭证是否有效,然后再检查此人是否有任何未付发票。根据未完成的值,我希望将用户重定向到给定的页面或将其完全屏蔽 谢谢是的,这是可能的。AuthComponent的重定向位置只是一个会话变量(因此技术上可以在任何地方设置) 要更改重

我有许多身份验证组件,它们扩展了
BaseAuthenticate
类。这些是以正常方式在AppController中设置的

身份验证组件是否可以更改AuthComponent的LoginDirect变量?

为了澄清这种情况,我的一个身份验证组件查看了特定的用户子集。它先检查凭证是否有效,然后再检查此人是否有任何未付发票。根据未完成的值,我希望将用户重定向到给定的页面或将其完全屏蔽


谢谢

是的,这是可能的。AuthComponent的重定向位置只是一个会话变量(因此技术上可以在任何地方设置)

要更改重定向位置,可以手动设置:

$this->Session->write('Auth.redirect', 'http://example.com');
在下一个请求中,AuthComponent将重定向它们

或者,让您的组件立即重定向它们:

$this->_Collection->getController()->redirect('http://example.com');

非常感谢@Jerrermyharris将我推向了正确的方向。这就是我最后所做的

1。扩展了AuthComponent

    App::uses('AuthComponent', 'Controller/Component');

    class MyAuthComponent extends AuthComponent {

      var $components = array('Session'); 

      public function identify(CakeRequest $request, CakeResponse $response) {
        if (empty($this->_authenticateObjects)) {
            $this->constructAuthenticate();
        }
        foreach ($this->_authenticateObjects as $auth) {
            $result = $auth->authenticate($request, $response);
            if (!empty($result) && is_array($result)) {
                if(isset($result['Redirect']))
                {
                    $this->Session->write('Auth.redirect', $result['Redirect']);            
                }
                return $result;
            }
        }
        return false;
      }
    }
App::uses('BaseAuthenticate', 'Controller/Component/Auth');

class TutorAuthenticate extends BaseAuthenticate {

    public function authenticate(CakeRequest $request, CakeResponse $response) {
        $user = ...... // However you authenticate your user
        $user['Redirect'] = "http://example.com";
        return $user;
    }
}
2。将此添加到AppController组件中

public $components = array(
        'Auth' => array(
            'className' => 'MyAuth',
        )
);
在AuthComponent的任何其他定义周围添加此位

3。从身份验证组件返回重定向

    App::uses('AuthComponent', 'Controller/Component');

    class MyAuthComponent extends AuthComponent {

      var $components = array('Session'); 

      public function identify(CakeRequest $request, CakeResponse $response) {
        if (empty($this->_authenticateObjects)) {
            $this->constructAuthenticate();
        }
        foreach ($this->_authenticateObjects as $auth) {
            $result = $auth->authenticate($request, $response);
            if (!empty($result) && is_array($result)) {
                if(isset($result['Redirect']))
                {
                    $this->Session->write('Auth.redirect', $result['Redirect']);            
                }
                return $result;
            }
        }
        return false;
      }
    }
App::uses('BaseAuthenticate', 'Controller/Component/Auth');

class TutorAuthenticate extends BaseAuthenticate {

    public function authenticate(CakeRequest $request, CakeResponse $response) {
        $user = ...... // However you authenticate your user
        $user['Redirect'] = "http://example.com";
        return $user;
    }
}
因此,现在如果您想根据用户重定向,您可以将其添加到中,如果您不这样做,那么cake将遵守您在AppController中设置的指令


哇,看起来我不得不多做一些事情,但这是正确的做法。

谢谢杰里米,我最终选择了这条路线。值得注意的是,默认情况下,BaseAuthenticate类没有访问Auth组件的权限,我必须执行
App::import('component','session')->write('Auth.redirect','http://example.com');是。这是因为身份验证类应该只进行身份验证。他们不应该改变方向。重定向逻辑属于组件或控制器。组件和控制器可以有其他组件(如
会话
)。这是一个有趣的问题,但如果没有,它应该放在哪里?这一点的逻辑是特定于这个用户子集的,他们已经有了自己的身份验证组件。你认为他们也应该有自己的控制器来处理这种类型的逻辑吗?嗯,在不知道确切问题的情况下,我会说扩展AuthComponent,覆盖该方法(这是通过身份验证对象进行迭代的)并在那里处理设置。每个身份验证对象都可以持有重定向位置,并且
\u getUser()
将其设置在那里。因此,现在使用
MyAuthComponent
扩展了
AuthComponent
,我是否必须通过
$this->MyAuth
到处访问它,或者是否有某种方式将其别名改回
$this->Auth