CakePHP 3.4.3日志记录在插件中不起作用

时间:2017-04-13 08:29:50

标签: authentication plugins login finder cakephp-3.x

我有登录问题。新用户注册工作,密码加密,但我无法登录。我的登录操作在UsersAcces插件中,我认为这是一个问题。怎么了?

在src / AppController中配置Auth:

$this->loadComponent('Auth', [
        'loginAction' => [
            'controller' => 'Users',
            'action' => 'login',
            'plugin' => 'UsersAcces'
        ],
        'loginRedirect' => [
            'plugin' => 'UsersAcces',
            'controller' => 'Users',
            'action' => 'index'
        ],
        'logoutRedirect' => [
            'controller' => 'Users',
            'action' => 'login',
            'plugin' => 'UsersAcces'
        ],
        'authError' => 'Brak dostępu!',
        'authenticate' => [
            'Form' => [
                'fields' => ['username' => 'username','password'=>'password'],
                'userModel'=>'UsersAcces.Users',
                'relatedModel' => ['UsersAcces.Roles', 'UsersAcces.Addresses', 'UsersAcces.Emails', 'UsersAcces.Permissions', 'UsersAcces.Telephones'],//'UsersAcces.Roles',
                'finder' => 'auth'
            ]
        ],
        'storage' => 'Session',
        'autoRedirect' => false
    ]);

操作登录i plugins / UsersAcces / src / Controller / Users:

public function login(){
    $this->viewBuilder()->setLayout("login");


    if ($this->request->is('post')) {
        $user = $this->Auth->identify();
        if ($user) {
            return $this->redirect($this->Auth->redirectUrl());
        }else{
            $this->Flash->error(__('Niewłaściwy login lub hasło. Spróbuj ponownie.'));
        }
    }
    //$user = $this->Users->newEntity();
    $this->set(compact("user"));

}

插件中的函数findAuth / UsersAcces / src / Model / Table / UsersTable:

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

插件中的函数setPassword / UsersAcces / src / Model / Entity / User:

protected function _setPassword($password)
{
    if (strlen($password) > 0) {
      return (new DefaultPasswordHasher)->hash($password);
    }
}

请帮忙。

2 个答案:

答案 0 :(得分:1)

我找到了解决方案!我没有在文件中添加一行"插件/ UsersAcces / src / Controller / Users":

$this->Auth->setUser($user);

答案 1 :(得分:0)

您需要在'表格'下添加密码Hasher。您的Auth组件初始化参数中的键,如:

'authenticate' => array( 'Form' => array( 'passwordHasher' => 'Default' ) ),