CakePHP 2.6被授权不被称为

时间:2016-02-10 14:29:05

标签: authentication cakephp cakephp-2.6

我有一个工作项目,我希望通过身份验证进行扩展。我遵循了简单身份验证和授权教程here

登录工作,我可以打印用户名等但访问控制不起作用。授权方法根本不起作用。我在这里缺少什么?

编辑 - 我收到以下错误;

"You are not authorized to access that location."

我的AppController的一部分:

public $components = array('Flash', 'RequestHandler', 'Cookie', 'Session', 'Auth' => array(
        'loginRedirect' => array(
            'controller' => 'status',
            'action' => 'index'
        ),
        'logoutRedirect' => array(
            'controller' => 'user',
            'action' => 'login'
        ),
        'authenticate' => array(
            'Form' => array(
                'passwordHasher' => 'Blowfish'
            ),
        ),
        'authorize' => array('Controller')
    ));

public function isAuthorized($user)
{
    if (isset($user['role']) && $user['role'] === 'admin') return true;

    return false;
}

我的taskController的一部分:

public function isAuthorized($user)
{
    debug($user); die();

    if ($this->action === 'index') return true;

    if (in_array($this->action, array('edit', 'delete')))
    {
        $postId = (int) $this->request->params['pass'][0];
        if ($this->Post->isOwnedBy($postId, $user['id'])) return true;
    }

    return parent::isAuthorized($user);
}

UsersController的一部分:

public function login()
{
    if ($this->request->is('post'))
    {
        if ($this->Auth->login())
        {
            return $this->redirect($this->Auth->redirectUrl()); // This is being called after login so it seems to work!
        }

        $this->Flash->error(__('Invalid username or password, try again'));
    }
}

0 个答案:

没有答案