1

Hi developers I am new to YII , I have installed the YII2 framework and want a RBAC I have installed mdmsoft/yii2-admin module , but I do not know how to create RULE class, where to create it and then how to use. When I create a role in admin section it say , enter a Class Name. I don't know how to create and use the RULE feature of YII. I have attached the screen shot.enter image description here

1 Answers1

0

If you are using an Advanced template here are the steps;

  1. Create a directory under frontend and rename it rbac
  2. Create a file under this new directory, say, AuthorRule.php. Here is a sample file given in the official docs;
namespace app\rbac;


use yii\rbac\Rule;
use app\models\Post;

/**
 * Checks if authorID matches user passed via params
 */
class AuthorRule extends Rule
{
    public $name = 'isAuthor';

    /**
     * @param string|int $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 bool 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']->createdBy == $user : false;
    }
}
  1. Next step is to navigate to http://localhost/path/to/index.php?r=admin/rule and create a new rule with class name \app\rbac\AuthorRule
  2. Finally you can add the new rule to roles and permissions depending on your needs.

You can read the official docs for more information on rules; the official docs.

Phemelo Khetho
  • 200
  • 2
  • 13
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/19533149) – Rob Apr 24 '18 at 11:22
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/19533149) – EM-Creations Apr 24 '18 at 12:31