http://www.yiiframework.com/doc-2.0/guide-security-authorization.html#role-based-access-control-rbac如何爲Yii2中的基本模板用戶分配角色?
在本文檔中,它說,你可以使用此代碼分配角色在高級模板的用戶:
public function signup()
{
if ($this->validate()) {
$user = new User();
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();
$user->save(false);
// the following three lines were added:
$auth = Yii::$app->authManager;
$authorRole = $auth->getRole('author');
$auth->assign($authorRole, $user->getId());
return $user;
}
return null;
}
的問題是,我使用的基本模板。在基本模板中有沒有這樣做的方法?
我想過使用afterSave方法;但是,我不知道如何做到這一點。
public function afterSave($insert)
{
}
關於如何完成任何想法?
public function afterSave($insert)
{
$auth = Yii::$app->authManager;
$authorRole = $auth->getRole('author');
$auth->assign($authorRole, $this->Id());
}
我在想這可行,但我不完全確定。