Cakephp:使用AuthComponent登录管理员

时间:2015-10-09 12:27:48

标签: php cakephp login

我有一个名为admins的表。它包含用户名和密码字段。我的销售用户使用admins表进行销售登录。我想使用AuthComponent登录销售。我已经为此编写了代码,如下所示。

AppController的

public $components = [ 
        'Auth' => [ 
                'loginAction' => [ 
                        'controller' => '',
                        'action' => 'login' 
                ],
                'logoutRedirect' => [ 
                        'controller' => '',
                        'action' => 'login' 
                ],
                'loginRedirect' => [ 
                        'controller' => '',
                        'action' => 'deshboard' 
                ],
                'className' => 'MyAuth'
        ]]

public function beforeFilter() {
        $this->Auth->authenticate = [
                    'Form' => ['userModel' => 'admin', "fields" => ["username" => "username", 
                                                                                "password" => "password"]
                    ]];
}

SalesController

function login() {
    $post = $this->request->data('Admin');

    if ($this->request->is('post') && !empty($post)) {

        //var_dump($this->Auth->login());exit;
        if ($this->Auth->login()) {
            return $this->Auth->redirect($this->Auth->redirectUrl());
        }

        // perform login throttling (failure and block) if Sales or Admin portal
        // set an appropriate failure message
    }
}

当我打印auth-> login()函数的返回值时。它总是返回假。

我已经搜索了很多这个问题,但我找不到合适的答案。

先谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

我找到了上述问题的解决方案。

这个问题是因为passwordHaser。我在添加或更新密码中使用差异密码,因此如果我们不使用简单的登记,我们必须在组件配置中定义passwordHaser。

 'Auth' => [
        'className'      => 'MyAuth',
        'authenticate'   => [
            'Form' => [
                'userModel' => 'Admin',
                'fields'    => [
                    'username' => 'username',
                    'password' => 'password'
                ],
                'passwordHasher' => [
                    'className' => 'Simple'
                ]
            ]
        ],
    ],