سطح دسترسی RBAC در Yii2 (قسمت دوم)

خوب در قسمت قبل بخش اصلی RBAC را آموزش دادیم .
ما دو نقش به نام نویسنده و مدیر ایجاد کردیم
کهنویسنده امکان ثبت نوشته رو داشت و مدیر علاوه بر دسترسی به کارهای نویسنده می توانست نوشته را ویرایش نیز بکند.
سوالی که این جا پیش می آید این است که نویسنده چگونه بتواند نوشته های خود را ویرایش کند
برای اینکار ما از Rule استفاده می کنیم .
در واقع ما در Rule قوانینی تعریف می کنیم که قبل از اعمال سطوح دسترسی آن قوانین بررسی می شوند .

برای ایجاد قانون می بایست مراحل زیر را انجام دهیم :
ابتدا ورژن advanced
برای تعریف باید در فولدر common فولدری با نام component ایجاد کنیم
سپس rule خود را تعریف نماییم
من فایلی با نام AuthorRule.php ایجاد می کنم و کلاس زیر را تعریف می کنم

namespace common\component;
use yii\rbac\Rule;
/**
 * Checks if authorID matches user passed via params
 */
class AuthorRule extends Rule {
    public $name = 'isAuthor';
    /**
     * @param string|integer $user the user ID.
     * @param Item $item the role or permission that this rule is associated with
     * @param array $params parameters passed to ManagerInterface::checkAccess().
     * @return boolean a value indicating whether the rule permits the role or permission it is associated with.
     */
    public function execute($user, $item, $params) {
        return isset($params['post']) ? $params['post']->user_id == $user : false;
    }
}

کلاسی با نام AuthorRule ایجاد می کنیم که از Rule خود rbac ارث می برد.
سپس نامی دلخواه isAuthor ایجاد کردیم.
سپس در متد execute بررسی می کنیم اگر دیتا یا مدلی post شده بود و user_id آن (نویسنده ی آن نوشته که ما با user_id آن ا بررسی کردیم) با کاربر جاری یکی بود مقدار true و اگر یکی نبود مقدار false را برگرداند
.
به این معنی است زمانی که کاربری قصد ویرایش نوشته ای را داشته باشد و دسترسی برای ویرایش نداشته باشد .با استفاده از Rule بررسی می کنیم اگر این نوشته برای خود نویسنده بود امکان ویرایش را فراهم کند.

در ورژن basic
ما Rule را در فولدر commands با محتوای زیر :

namespace app\commands;
use yii\rbac\Rule;
/**
 * Checks if authorID matches user passed via params
 */
class AuthorRule extends Rule {
    public $name = 'isAuthor';
    /**
     * @param string|integer $user the user ID.
     * @param Item $item the role or permission that this rule is associated with
     * @param array $params parameters passed to ManagerInterface::checkAccess().
     * @return boolean a value indicating whether the rule permits the role or permission it is associated with.
     */
    public function execute($user, $item, $params) {
        return isset($params['post']) ? $params['post']->user_id == $user : false;
    }
}

و همچنین RbacController در همین فولدر با کد زیر :

namespace app\commands;
use Yii;
use yii\console\Controller;
class RbacController extends Controller {
    public function actionInit() {
        $auth = Yii::$app->authManager;
        // add "createPost" permission
        $createPost = $auth->createPermission('createPost');
        $createPost->description = 'Create a post';
        $auth->add($createPost);
        // add "updatePost" permission
        $updatePost = $auth->createPermission('updatePost');
        $updatePost->description = 'Update post';
        $auth->add($updatePost);
        // add "author" role and give this role the "createPost" permission
        $author = $auth->createRole('author');
        $auth->add($author);
        $auth->addChild($author, $createPost);
        // add "admin" role and give this role the "updatePost" permission
        // as well as the permissions of the "author" role
        $admin = $auth->createRole('admin');
        $auth->add($admin);
        $auth->addChild($admin, $updatePost);
        $auth->addChild($admin, $author);
        // Assign roles to users. 1 and 2 are IDs returned by IdentityInterface::getId()
        // usually implemented in your User model.
        $auth->assign($author, 2);
        $auth->assign($admin, 1);
        $rule = new AuthorRule;
        $auth->add($rule);
// add the "updateOwnPost" permission and associate the rule with it.
        $updateOwnPost = $auth->createPermission('updateOwnPost');
        $updateOwnPost->description = 'Update own post';
        $updateOwnPost->ruleName = $rule->name;
        $auth->add($updateOwnPost);
// "updateOwnPost" will be used from "updatePost"
        $auth->addChild($updateOwnPost, $updatePost);
// allow "author" to update their own posts
        $auth->addChild($author, $updateOwnPost);
        echo "Success";
    }
}

در آخر نیز امکان ویرایش نوشته ی خود نویسنده را با عنوان updateOwnPost به نویسنده نسبت می دهیم

$auth = Yii::$app->authManager;
// add the rule
$rule = new \app\rbac\AuthorRule;
$auth->add($rule);
// add the "updateOwnPost" permission and associate the rule with it.
$updateOwnPost = $auth->createPermission('updateOwnPost');
$updateOwnPost->description = 'Update own post';
$updateOwnPost->ruleName = $rule->name;
$auth->add($updateOwnPost);
// "updateOwnPost" will be used from "updatePost"
$auth->addChild($updateOwnPost, $updatePost);
// allow "author" to update their own posts
$auth->addChild($author, $updateOwnPost);

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *