在注册过程中为用户分配角色[yii(users + auth)]

时间:2013-10-24 17:24:06

标签: php authentication yii roles

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

auth/role/ 
  1. 管理
  2. 公司
  3. 其他
  4. 另外,在用户模块中

    user/profileField
    

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

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

    Admin acc只能在模块中创建

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

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

    btw:直接更改/user/controllers/RegistrationController.php是否优雅?

1 个答案:

答案 0 :(得分:3)

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

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

as explained here

相关问题