如何在CakePHP 3.4中使用自定义查找查询进行身份验证

时间:2017-05-18 08:24:29

标签: php cakephp cakephp-3.4

我需要使用Customizing Find Query对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;
}

login.ctp

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

1 个答案:

答案 0 :(得分:0)

//group list entries to sub - lists having the same effect value
Map<String, List<Item>> map = itemList.stream()
                                      .collect(Collectors.groupingBy(Item::getEffect)) ;

List<Item> uniqueEfectMaxValue = new ArrayList<>();
//get item having max value and add to uniqueEfectMaxValue
map.forEach((key,list)->{
                          Item item = list.stream().
                             collect(Collectors.maxBy(Comparator.
                                  comparing(Item::getValue))).get();
                          uniqueEfectMaxValue.add(item);
                         }
             );

应为:

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

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