简体   繁体   中英

Assigning roles to a user in registration process [yii (users + auth)]

Im using users + auth modules in Yii. I've made 3 roles in auth using administration:

auth/role/ 
  1. Admin
  2. Company
  3. Other

Also, in users module

user/profileField

I've made one profile field which will determinate in registration page, what account you want to make

[boolean: true="Company" false="Other"]

Admin acc can be only created in module

I want to assign roles based on this field in registration process. I assume i should write this in this method (?):

/user/controllers/RegistrationController.php:
public function actionRegistration(){...
...
 if ($model->save()) {
                        $profile->user_id=$model->id;
                        ....my role assign here ?...
                        $profile->save();
 ...

btw: is it classy to change /user/controllers/RegistrationController.php directly ?

To assign roles to users use:

Yii::app()->authManager->assign($role,$userId);

as explained here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM