CakePHP 2.6 isAuthorized not being called

241 Views Asked by At

I have a working project that i want to extend with authentication. I followed the Simple Authentication and Authorization Tutorial here.

Logging in works and i can print the Username etc. But the access control is not working. The Method isAuthorized is simply not working. What am i missing here?

Edit - I receive the below error;

"You are not authorized to access that location."

Part of my 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;
}

Part of my 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);
}

Part of 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

There are 0 best solutions below