These are chat archives for dektrium/yii2-user

2nd
Aug 2015
golubenkonick
@golubenkonick
Aug 02 2015 16:53
This message was deleted
This message was deleted
golubenkonick
@golubenkonick
Aug 02 2015 17:07

Hello.
I want new user has the role "student" after registering.
I added 3 lines in model RegistrationForm.php:
...
public function register()
{
if (!$this->validate()) {
return false;
}

    /** @var User $user */
    $user = Yii::createObject(User::className());
    $user->setScenario('register');
    $this->loadAttributes($user);

    if (!$user->register()) {
        return false;
    }


    // Added by me
    $auth = Yii::$app->authManager;
    $authorRole = $auth->getRole('student');
    $auth->assign($authorRole, $user->getId());
    // end

    Yii::$app->session->setFlash(
        'info',
        Yii::t('user', 'Your account has been created and a message with further instructions has been sent to your email')
    );

    return true;
}

...
It works, but I think it's not a good practice to change files in vendor.
Can I do this in another way?
Thank you.