CommentRulesController.php 2.11 KB
<?php
    namespace console\controllers;

    use common\components\rules\CommentOwnRule;
    use yii\console\Controller;
    use yii\rbac\Permission;

    class CommentRulesController extends Controller
    {
        public function actionCreate() {
            $auth = \Yii::$app->getAuthManager();
            $commentownrule = new CommentOwnRule();
            $auth->add($commentownrule);
            $auth->add(new Permission([
                'name' => 'Update own comment',
                'ruleName' => $commentownrule->name,
                'description' => 'Can update own comment',
            ]));
            $auth->add(new Permission([
                'name' => 'Delete own comment',
                'ruleName' => $commentownrule->name,
                'description' => 'Can delete own comment',
            ]));
            return 'Success';
        }

        public function actionUpdate()
        {
            $auth = \Yii::$app->getAuthManager();
            $commentownrule = new CommentOwnRule();
            $auth->add(new Permission([
                'name' => \rmrevin\yii\module\Comments\Permission::CREATE,
                'description' => 'Can create own comments',
            ]));
            $auth->add(new Permission([
                'name' => \rmrevin\yii\module\Comments\Permission::UPDATE,
                'description' => 'Can update all comments',
            ]));
            $auth->add(new Permission([
                'name' => \rmrevin\yii\module\Comments\Permission::UPDATE_OWN,
                'ruleName' => $commentownrule->name,
                'description' => 'Can update own comments',
            ]));
            $auth->add(new Permission([
                'name' => \rmrevin\yii\module\Comments\Permission::DELETE,
                'description' => 'Can delete all comments',
            ]));
            $auth->add(new Permission([
                'name' => \rmrevin\yii\module\Comments\Permission::DELETE_OWN,
                'ruleName' => $commentownrule->name,
                'description' => 'Can delete own comments',
            ]));
            echo 'ok';
            return 'ok';
        }
    }