【问题标题】:In cakephp 3.6, How to change user finder query for auth component?在 cakephp 3.6 中,如何更改身份验证组件的用户查找器查询?
【发布时间】:2018-09-27 09:12:23
【问题描述】:

对于 cakephp 3.6,cakephp.org 在以下链接中介绍了如何自定义用户查找器查询以获取 auth 组件: link 但我不知道如何实现它? 我在属于部门表的用户表中有'department_id'列。我想更改以下查询:

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

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

【问题讨论】:

    标签: authentication cakephp cakephp-3.6


    【解决方案1】:

    首先,您需要加载 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有帮助

    【讨论】:

      猜你喜欢
      • 2017-10-17
      • 1970-01-01
      • 1970-01-01
      • 2019-01-04
      • 2023-03-08
      • 1970-01-01
      • 2014-04-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多