Cakephp 在身份验证用户会话中包括字段子集

Cakephp 在身份验证用户会话中包括字段子集,cakephp,cakephp-2.0,cakephp-2.1,Cakephp,Cakephp 2.0,Cakephp 2.1,我的Users表有一大堆字段,其中大部分我不需要/不想存储在Auth User会话中。如何限制登录用户在会话中存储哪些字段 我知道您可以使用“contain”键选择关联模型的字段,但通常要选择顶级模型的字段,您会使用“fields”键。但在Auth的情况下,“fields”键用于选择要通过哪些字段对用户进行身份验证,而不是选择要在会话中包括哪些字段 给一些上下文,这里是我的代码到目前为止。。。我该怎么做才能使Auth会话中只存储email和firstname字段,而不是Users表中的所有字段

我的Users表有一大堆字段,其中大部分我不需要/不想存储在Auth User会话中。如何限制登录用户在会话中存储哪些字段

我知道您可以使用“contain”键选择关联模型的字段,但通常要选择顶级模型的字段,您会使用“fields”键。但在Auth的情况下,“fields”键用于选择要通过哪些字段对用户进行身份验证,而不是选择要在会话中包括哪些字段

给一些上下文,这里是我的代码到目前为止。。。我该怎么做才能使Auth会话中只存储email和firstname字段,而不是Users表中的所有字段

$this->Auth->authenticate = array(
    'Blowfish' => array(
        'fields' => array(
            'username' => 'email',
            'password' => 'password',
        )
    )
);

在我的UsersController的beforeFilter中,我有一些类似于您的登录名的内容

然后我将afterLogin函数设置为重定向

 $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'afterLogin');
 $this->Auth->loginRedirectTrue = array('controller' => 'users', 'action' => 'index');
 $this->Auth->logoutRedirect = array('controller' => 'pages', 'action' => 'display');
登录函数进行一些检查,然后重定向到

if ($this->Auth->login()){
    // code here
    $this->redirect($this->Auth->redirect());
}
还有像这样的afterLogin函数

 function afterLogin(){
    $session = $this->Session->read('Auth');
    $user_id = $session['User']['id'];

    // change this to find only the fields you need and then override the Auth.User...
    $user = $this->User->findById($user_id);
    if (!empty($user)){
        $this->Session->write('Auth.UserProfile', $user['UserProfile']);
    }
    $this->redirect($this->Auth->loginRedirectTrue);
 }
您应该更改findById以满足您的需要,然后覆盖会话中的Auth.User字段


祝你好运

我对有用的答案投了赞成票,尽管这些答案是围绕解决方案进行的-谢谢

我认为“正确”的答案是,使用现成的CakePHP Auth组件无法做到这一点,您必须破解它(例如,使用下面的解决方案之一)。我查看了BaseAuthenticate.php中的
\u findUser
方法,它证实了这一点

如果CakePHP核心开发人员正在阅读(DeEuroMarK?),这可能是一个非常常见的需求,我认为这是一个值得内置的特性

建议的实现:只需将您想要的字段作为额外字段包含在“字段”数组中,并且只需假设除“用户名”和“密码”之外的每个键都是应该包含在身份验证会话中的额外字段。这样,它与其他模型查找语法一致

例如:

$this->Auth->authenticate = array(
    'Blowfish' => array(
        'fields' => array(
            'username' => 'email',
            'password' => 'password',
            'another_field',
            'yet_another_field'
        )
    )
);

我认为最简单的方法是添加如下内容:

向Auth组件配置中添加包含

$this->loadComponent('Auth', [
        'authorize' => 'Controller',
        'loginRedirect' => [
            'controller' => 'Users',
            'action' => 'index'
        ],
        'logoutRedirect' => [
            'controller' => 'Users',
            'action' => 'login'
        ],
        'authenticate' => [
            'Form' => [
                'fields' => ['username' => 'email'],
                'contain' => ['Groups']
            ]
        ],
        'unauthorizedRedirect' => $this->referer()
    ]);
...
在登录操作中,在会话中保存用户:

$foundUser = $this->Auth->identify();
if ($foundUser) {
    $this->Auth->setUser($foundUser);
}
...
这将向Auth.User添加包含组


这是针对CakePhp3的-在旧版本中可能会有所不同。

我不知道会发生什么,但是如果您执行
contain=>array('User'=>array('field1','fields2'))
?它能正确地使用它吗?这样您就可以手动包含顶级模型了?也许这就是诀窍?(可能不是).Nup,这会给你一个警告:Model“User”与Model“User”没有关联,看看你是否能找到关于它的票证。如果没有,请创建一个:-)这是放置它的最佳位置。同时,现在是否有“字段”键可用?