نحوه استفاده از RBAC بعداز انجام تنظیمات

ساخت وبلاگ

سلام
من میخوام تو پروژم از RBAC استفاده کنم و یکسری کد اضافه کردم ولی دقیقا نمیدونم چطوری باید جاهای دیگه مثل اضافه شدن کاربر ازش استفاده کنم.
کد زیر کنترلر rbac:


<?php

namespace 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);
}
}


و این هم فانکشن create که در کنترلر user قرار داره:

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,
]);
}

}}


این هم کد لاگینم در کنترلر site :

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;
}



من همه مراحلو انجام دادم. کانفیگ لازمو انجام دادم. جدولای migration رو درست کردم. آیا نیازی هست با وجود این جدولا بازم فیلد role در جدول user باشه؟
در کل چجوری باید ازش تعیین سطح دسترسی استفاده کنم.

برنامه نویس...
ما را در سایت برنامه نویس دنبال می کنید

برچسب : نویسنده : محمد رضا جوادیان programers بازدید : 142 تاريخ : چهارشنبه 13 مرداد 1395 ساعت: 15:44