【问题标题】:How to use Customizing Find Query for authentication in CakePHP 3.4如何在 CakePHP 3.4 中使用自定义查找查询进行身份验证
【发布时间】:2017-10-17 22:48:10
【问题描述】:

我需要在 CakePHP 3.4 中使用自定义查找查询对用户进行身份验证。我为此遵循了烹饪书,但在我的findAuth 方法$option 中返回空array。我不知道代码中的错误是什么。

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

UsersTable.php

public function findAuth(\Cake\ORM\Query $query, array $options)
{
    debug($options);
    $query->select(['sno', 'username', 'password','contact'])
        ->where(['Users.username' => $options['username']]);
    return $query;
}

登录.ctp

<?php
   echo $this->Form->create();
   echo $this->Form->input('username');
   echo $this->Form->input('password');
   echo $this->Form->submit();
   echo $this->Form->end();
?>

【问题讨论】:

  • 我看不出这是怎么发生的,除非围绕查找器的其他相关核心代码会被修改/覆盖?你确切的 CakePHP 版本是什么(vendor/cakephp/cakephp/VERSION.txt 中的最后一行)?
  • 它在 version.txt 中显示 3.2.6
  • 这与声称的 3.4 相去甚远。请至少升级到最新的 3.2.x 版本,username 选项仅在 CakePHP 3.2.9 中引入
  • @ndm 感谢您提供此重要信息...这是错误的,我同时拥有导致我在此错误中挖掘的两个版本..
  • 还有一个问题,我有两个表,一个用于管理员,另一个用于用户....我需要创建单一登录如何实现这一点,以便管理员只能访问管理员授权页面,用户可以访问用户授权页面...以及如何在用户名字段中使用联系电话或电子邮件地址登录

标签: php cakephp cakephp-3.4


【解决方案1】:
   public function findAuth(\Cake\ORM\Query $query, array $options)
    {
        debug($options);
        $query->select(['sno', 'username', 'password','contact'])
            ->where(['Users.username' => $options['username']]);
        return $query;
    }

应该是:

public function findAuth(\Cake\ORM\Query $query, array $options)
{
    debug($options);
    $query->select(['sno', 'username', 'password','contact']));
    return $query;
}

参考:https://book.cakephp.org/3.0/en/controllers/components/authentication.html#creating-custom-authentication-objects

【讨论】:

  • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
猜你喜欢
  • 2017-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多