在cakephp 3.6中,如何更改身份验证组件的用户查找器查询?

时间:2018-09-27 09:12:24

标签: authentication cakephp cakephp-3.6

对于cakephp 3.6,cakephp.org在以下链接中介绍了如何针对身份验证组件自定义用户查找器查询: link 但是我不知道如何执行它? 我在属于部门表的用户表中有“ department_id”列。我想更改以下查询:

public function findAuth(){
    $query
    ->select(['id', 'username', 'password'])->contain(['Departments'])
        ->where(['Users.active' => 1]);
    return $query;
}

上面的代码可以工作吗? 请告诉我我必须在哪个文件中编写函数? 还有其他必要的步骤来完成它,以便在$ this-> Auth-> User中获得所有与用户相关的信息?

1 个答案:

答案 0 :(得分:0)

首先,您将需要加载Auth组件并在要使用自定义查找器的任何控制器中传递自定义配置,如下所示:

public function initialize()
{
    parent::initialize();
    $this->loadComponent('Auth', [
        'authenticate' => [
            'Form' => [
                'finder' => 'auth'
            ]
        ],
    ]);
}

然后,在您的UsersTable中,您将拥有自定义查找器:

public function findAuth(\Cake\ORM\Query $query, array $options)
{
    $query
    ->select(['id', 'username', 'password'])->contain(['Departments'])
    ->where(['Users.active' => 1]);

    return $query;
}

此答案也可能会帮助Containing tables in custom Auth finder

相关问题