php-casbin/laravel-authz

Error on php artisan role:assign eve write

vadbard opened this issue · 5 comments

ErrorException  : Creating default object from empty value

  at /var/www/html/vendor/casbin/casbin/src/Model/Policy.php:174
    170|      * @param string[] $rule
    171|      */
    172|     public function addPolicy(string $sec, string $ptype, array $rule): void
    173|     {
  > 174|         $this->items[$sec][$ptype]->policy[] = $rule;
    175|         $this->items[$sec][$ptype]->policyMap[implode(self::DEFAULT_SEP, $rule)] = count($this->items[$sec][$ptype]->policy) - 1;
    176|     }
    177| 
    178|     /**

  Exception trace:

  1   Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Creating default object from empty value", "/var/www/html/vendor/casbin/casbin/src/Model/Policy.php", ["g", "g"])
      /var/www/html/vendor/casbin/casbin/src/Model/Policy.php:174

  2   Casbin\Model\Policy::addPolicy("g", "g")
      /var/www/html/vendor/casbin/casbin/src/InternalEnforcer.php:48

sorry for formatting :)
Fresh install on Laravel 6. Migrations created table rules
Just tried some artisan commands, the first two created something and the last one
php artisan role:assign eve write
made an error.

@vadbard can you improve the code format? Like this:

public function addPolicy(string $sec, string $ptype, array $rule): void

@techoner

@hsluoyz done :)

@vadbard you are trying to add a g policy rule. Before doing this, you need to make sure you are using a RBAC model (https://casbin.org/docs/en/rbac#role-definition), for example: https://casbin.org/docs/en/supported-models#examples

[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, act

[role_definition]
g = _, _

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act

I guess you missed the [role_definition] part.

@hsluoyz you are right, I missed the [role_definition] part. Thank you!