Yii Framework Forum: Accessrules() - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Accessrules() Rate Topic: -----

#1 User is offline   RSfTDL 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 89
  • Joined: 22-June 12

Posted 28 May 2013 - 09:46 AM

Hey guys!

In one of my controllers, i'm trying to redirect the user instead of displaying the default 403 error (Error 403: You are not authorized to perform this action.) that shows when a rule fails on the accessRules(). For that i'm using the deniedCallback but for some reason it's not working!

...
return array(
    array('allow', // allow anonymous user to perform 'login' and 'register' actions
        'actions' => array('login', 'register'),
        'users' => array('?'),
        'deniedCallback' => function() { $this->redirect(Yii::app()->homeUrl); }
    ),                    
);
...


Any clue what's going on?
0

#2 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,670
  • Joined: 04-March 10
  • Location:UK

Posted 28 May 2013 - 09:52 AM

I guess that because this is an "allow" rule rather than a "deny" rule that the deniedCallback is never being called. A user can't be denied by this rule, they can either be allowed access or evaluation will continue with the next rule in the chain.

Try applying the deniedCallback on a deny rule further down.
0

#3 User is offline   RSfTDL 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 89
  • Joined: 22-June 12

Posted 28 May 2013 - 10:02 AM

View PostKeith, on 28 May 2013 - 09:52 AM, said:

I guess that because this is an "allow" rule rather than a "deny" rule that the deniedCallback is never being called. A user can't be denied by this rule, they can either be allowed access or evaluation will continue with the next rule in the chain.

Try applying the deniedCallback on a deny rule further down.


Oh damn... you're totally right Keith, i was calling on the wrong place! Applying the callback on a deny rule work perfectly.

Here's the accessRules()

public function accessRules()
{
                return array(
                    array('allow', // allow anonymous user to perform 'login' and 'register' actions
                        'actions' => array('login', 'register'),
                        'users' => array('?'),                        
                    ),
                    array('allow', // allow authenticated user to perform 'logout' action
                        'actions' => array('logout'),
                        'users' => array('@'),
                    ),
                    array('deny', // deny all users
                        'users' => array('*'),
                        'deniedCallback' => function() { $this->redirect(Yii::app()->homeUrl); }
                    ),
                );
}


Ty for the tip Keith!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users