Creating And Assining Roles

is it right way to create and assign roles in yii? also suggest where should I put this code for logged in users. Should we put it into webUser or UserIdentity for the purpose of optimization and performance.

Create Roles


$auth = Yii::app()->authManager;

        if ($auth->getAuthItem('admin') === NULL) {

           Yii::app()->authManager->createAuthItem('admin',2);

        }

        if ($auth->getAuthItem('editor') === NULL) {

            Yii::app()->authManager->createAuthItem('editor',2);

        }

        if ($auth->getAuthItem('reader') === NULL) {

            Yii::app()->authManager->createAuthItem('reader',2);

        }

Assign Roles


        if (!$auth->isAssigned('admin',Yii::app()->user->id)) {

            $auth->assign('admin',Yii::app()->user->Id);

            $changed = true;

        }

        if (!$auth->isAssigned('editor',Yii::app()->user->id)) {

            $auth->assign('editor',Yii::app()->user->Id);

        }



Hope this will help:

wiki