user765368
user765368

Reputation: 20346

cakephp isAuthorized being called on every action call

I have a controller in a CakePHP application in which I have a bunch of actions. I'm using the ACL Component to manage which user is allowed to execute which actions in my controller. I have an isAuthorized() action in my controller to check if a certain logged user is allowed to execute a requested action that looks like this:

function isAuthorized()
{
    switch($this->action)
    {
        case 'myAction':
            $user_id = $this->Auth->user('id'); // the id of the connected user        

            if($user_id)
            {
                return $this->Acl->check(
                    array('model' => 'MyModel', 'foreign_key' => $user_id),
                    'controllers/MyController/myAction'
                );
            }

            break;
    }
}

As you can see above, all I'm doing is check if the connected user is allowed to execute myAction by using the method check of the Acl component. The problem I have with this approach is that this check is done every single time myAction is called. Is there a way to tell Cake to perform this check only one time (on the first call of the action for example)?. By checking every single time if a user is allowed to execute a controller action that slows down the application a lot.

Any help or suggestions is appreciated

Thank you

Upvotes: 0

Views: 405

Answers (1)

datasage
datasage

Reputation: 19563

Technically speaking, HTTP is stateless and each request does not have any affinity to any other request from the same user. State-fullness is created by using sessions.

You could store the ACL check result in a session variable. But you would need some way to reset it if the users access were to change while logged in.

Upvotes: 2

Related Questions