laravel-acl icon indicating copy to clipboard operation
laravel-acl copied to clipboard

Cache error

Open IlyaSavich opened this issue 8 years ago • 7 comments

Good day. I try yo update roles for user. Updating works well, but after updating I have a problem that method getRoles() returns previous state. I tried to change 'cacheMinutes' => 1, to 0 or null, but got the same result. Can you help me?

IlyaSavich avatar Nov 17 '16 19:11 IlyaSavich

This is my first time using this package and I just found this same issue. I had to customize and override some models and traits to fit my needs, but I can't figure out what's happening here. In summary I am using something like this in my controller (I am just playing around with this package):

$newRole = Role::create([
      'id'   => 'role1',
      'name' => 'common_user',
      'slug' => 'common_user',
    ]);
Auth::user()->assignRole($newRole);
dd(Auth::user()->hasRole('common_user'));

I'm using L5.3 and, as I said, I did some changes, so I can use Roles with string ID fields. The thing here is about the dd function response which return false right after the role assignation to the logged user. I changed "cacheMinutes" variable in the acl.php config file but I get the same result. I also try not using cache to get the user roles (overwriting this portion of code in my custom trait from hasRole trait):

public function getRoles()
  {
    $this_roles = $this->roles; //<----- this line changed to skip the cache usage when read roles
    $slugs = method_exists($this_roles, 'pluck') ? $this_roles->pluck('slug', 'id') : $this_roles->lists('slug', 'id');
    return is_null($this_roles)
    ? []
    : $this->collectionAsArray($slugs);
  }

Once again, no luck with this change, I get exactly the same behavior if I ask if the logged user has the role right after the role assignation.

The strange thing here is that if I make a new request to the same controller or any other controller with this last change, the dd(Auth::user()->hasRole('common_user')) function returns true (same happens if you use the Cache but you make the request after the cached info expires).

So... where is that roles info comming from? Can you use new roles and permissions just after the cache info expires? Am I missing something? How can I use the role or ask for it right after the assignation?

felorodri avatar Nov 17 '16 22:11 felorodri

Method hasRole() checks over slug, but not over id. You need pass in this method slug or override this method

IlyaSavich avatar Nov 18 '16 07:11 IlyaSavich

Sorry, I am looking by slug not by id, I just mistyped for this example. My previous comment was updated.

felorodri avatar Nov 18 '16 13:11 felorodri

Ok, I am here to clarify that my problem is not a package issue at all. I found that this is an expected behavior from the laravel Auth facade and its guard (/Illuminate/Auth/Guard.php). The Auth::user() and its relations are locked (by locked I mean cached) under the same request. Unless you use Auth::user()->fresh() to fetch your user data you not gonna see any change made.

felorodri avatar Nov 21 '16 13:11 felorodri

@felorodri @IlyaSavich is this related to #169?

kodeine avatar Nov 22 '16 06:11 kodeine

Yes, I think, but I'm understanding how to update the package to apply this changes ? What version ?

IlyaSavich avatar Nov 25 '16 07:11 IlyaSavich

ok, thanks )

2016-11-25 19:36 GMT+03:00 Quentin Pétel [email protected]:

My commits have not been merged yet. You could use my fork : https://github.com/QuentinPetel/laravel-acl

ex with composer : "repositories": [ { "type": "vcs", "url": "https://github.com/QuentinPetel/laravel-acl" } ],

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/kodeine/laravel-acl/issues/171#issuecomment-262992934, or mute the thread https://github.com/notifications/unsubscribe-auth/AL7_5N6B3umU7ynwvdd90JsWpJBH3XgFks5rBw6KgaJpZM4K1r9g .

IlyaSavich avatar Nov 28 '16 10:11 IlyaSavich