سلام
من میخوام تو پروژم از RBAC استفاده کنم و یکسری کد اضافه کردم ولی دقیقا نمیدونم چطوری باید جاهای دیگه مثل اضافه شدن کاربر ازش استفاده کنم.
کد زیر کنترلر rbac:
<?phpnamespace commands;
use Yii;
use yiiconsoleController;class RbacController extends Controller
{
public function actionInit()
{
$auth = Yii::$app->authManager;// add "login" permission
$login = $auth->createPermission('login');
$login->description = 'login';
$auth->add($login);// add "create user" permission
$create = $auth->createPermission('create');
$create->description = 'create user';
$auth->add($create);// add "user" role and give this role the "createPost" permission
$user = $auth->createRole('user');
$auth->add($user);
$auth->addChild($user, $login);// add "admin" role and give this role the "updatePost" permission
// as well as the permissions of the "user,server,guest" role
$admin = $auth->createRole('admin');
$auth->add($admin);
$auth->addChild($admin, $create);
$auth->addChild($admin,$user);// Assign roles to users. 1 and 2 are IDs retued by IdentityInterface::getId()
// usually implemented in your User model.$auth->assign($user, 2);
$auth->assign($admin, 1);
}
}
class UsersController extends Controller
{public function actionCreate()
{
$model = new User();
// $model = new User(['scenario' => 'register']);
if ($model->load(Yii::$app->request->post()) && $model->save()) {retu $this->redirect(['view', 'id' => $model->id]);
} else {
retu $this->render('create', [
'model' => $model,
]);
}}}
class SiteController extends Controller
{public function behaviors()
{
retu [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout'],
'rules' => [
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
], ];
}public function actionLogin()
{
if (!Yii::$app->user->isGuest) {
retu $this->goHome();
}$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
retu $this->goBack();
}
retu $this->render('login', [
'model' => $model,
]);
}}
public function signup()
{
if ($this->validate()) {
$user = new User();
$user->useame = $this->useame;
$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());retu $user;
}retu null;
}
برچسب:
نویسنده: محمد رضا جوادیان