Need Help to checking Permission

Hi.

I have a class that extended from Controller.

here is the class

the class name is Controller.php that located in protected/components/




class Controller extends CController

{

	/**

	 * @var array the breadcrumbs of the current page. The value of this property will

	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}

	 * for more details on how to specify this property.

	 */

	public $breadcrumbs=array();


        public function init(){

            parent::init();

        }


        public function checkAuth(){

            if(Yii::app()->controller->action->id !== 'login'){

                if(Yii::app()->user->isGuest){

                    $this->redirect(array('admin/login'));

                }

            }

        }

}



the checkAuth() function is used to checking permission that allow only non-guest member to access the action in controller.

I have been using this way (manual way) to checking current user permission:




class SiteController extends Controller {

   public function actionAdmin(){

      $this->checkAuth();

      ...

   }


   public function actionAdd(){

      $this->checkAuth();

      ...

   }


   public function actionEdit(){

      $this->checkAuth();

      ...

   }

}



whether there is another way that is more practical to put checkAuth function(). so I no longer need to put checkAuth function() every action controller?

thanks for advance :slight_smile:

In Controller.php rewrite this method:

http://www.yiiframework.com/doc/api/CController#beforeAction-detail

Don’t forget to return true in the end of it.

:) thank you mr. Andy_s, fixed. :)