Denis Alexandrov
Denis Alexandrov

Reputation: 185

Yii: init module when it's not in config

I have some modules in my Yii project, and I would like to init them only if current user is privileged to use this module.

Here's what I did: In SiteController I fetch the db table with user-module relations and got an array of modules' names. Then I've wanted to load each available module when no moudules are defined in config. I couldn't find any operator or method to do this. Is there a way to realize this without making workaround?

Another thing I think about is to cancel module init in beforeControllerAction of each module, but I stil can't figure this out either.

Upvotes: 1

Views: 141

Answers (1)

Goodnickoff
Goodnickoff

Reputation: 2267

You really better to use beforeControllerAction for checking access rights as follows:

public function beforeControllerAction($controller, $action)
{
   if(parent::beforeControllerAction($controller, $action))
   {
      // this method is called before any module controller action is performed
      // you may place customized code here              
      if(!Yii::app()->user->checkAccess('getMyModule')){
          throw new CHttpException(403, Yii::t('yii','You do not have sufficient permissions to access.'));
      }             
      return true;
   }
   else
      return false;
}

Upvotes: 1

Related Questions