在注册过程中向用户分配角色 [yii(用户 + 身份验证)]


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

我在 Yii 中使用用户 + 身份验证模块。我使用管理在身份验证中扮演了 3 个角色:

auth/role/ 
  1. 管理
  2. 公司
  3. 其他

此外,在用户模块中

user/profileField

我制作了一个个人资料字段,该字段将在注册页面中确定您要创建哪个帐户

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

管理员帐户只能在模块中创建

我想在注册过程中根据此字段分配角色。我假设我应该用这种方法(?

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

顺便说一句:直接更改/user/controllers/RegistrationController.php是优雅的吗?

要向用户分配角色,请使用:

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

如此处所述