0

I installed dektrium\user and dektrium\rbac\ modules for manage user and access control.Related tables and files installed completely and i can see several tabs in /user/admin path ( Users, Roles, Permissions, Rules, Create ) for work with modules.I can manage users perfectly(create user, reset password, edit,..). buy I can not create a rule. I created a class in app\rbac\rules folder named AuthorRule :

<?php 
namespace app\rbac\rules;

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

/**
 * 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['news']) ? $params['news']->createdBy == $user : false;
    }
}

(I created news class with model,controler,views) but when I entered name and class rule in my modules. Neither the data is logged nor the error message. I can't add the rest of the sections until I get into the rule.

enter image description here

maryam
  • 584
  • 9
  • 26
  • does it save the rule with the above details?, if yes then you need to assign the rule to the user by creating a new permission and selecting the above created rule in the form. See the create new persmission form and you will understand – Muhammad Omer Aslam Oct 06 '19 at 10:13
  • to understand the complete process see the answer on this [post](https://stackoverflow.com/questions/57778924/yii2-rbac-rule-to-allow-view-own-data/57796394#57796394) where the process is done manually without using the above form – Muhammad Omer Aslam Oct 06 '19 at 10:19
  • I know I should to assign, but I must be made a rule that I can not do. it dose not save rule. – maryam Oct 06 '19 at 12:13
  • what error does it throw ? – Muhammad Omer Aslam Oct 06 '19 at 14:28
  • I cannot create any rule and I have not any message in view. I have the second picture when I click "save" Button. I searched a lot. But I couldn't find a solution muhammad can you help me if you want please send mail for me : masoudy.maryam@gmail.com .Thanks for guiding me – maryam Oct 08 '19 at 06:58

1 Answers1

0

I certainly hope the OP has solved their problem by now, but other people might encounter it.

First a remark: as described, the Save fails silently. This is because the form is submitted with Ajax (XHR). The error can be seen in the browser console.

This is the relevant part of the error message:

preg_match(): Compilation failed: invalid range in character class at offset 8

Due to the architecture of Yii 2, the actual regexp is a little tricky to find. It is in the model for Rules in yii2-rbac vendor/dektrium/yii2-rbac/models/Rule.php, line 86.

The original regexp is /^[\w][\w-.:]+[\w]$/

PHP 7.3 uses the PCRE2 library instead of the original PCRE, and the pattern above is wrong. The dash (-) needs to be escaped.

The full line should now be:

['name', 'match', 'pattern' => '/^[\w][\w\-.:]+[\w]$/'],

As the yii2-rbac package is abandoned, you can just modify the file. A more robust solution would be to override the Class.

Christian Lescuyer
  • 18,893
  • 5
  • 49
  • 45