Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache error #171

Open
IlyaSavich opened this issue Nov 17, 2016 · 7 comments
Open

Cache error #171

IlyaSavich opened this issue Nov 17, 2016 · 7 comments

Comments

@IlyaSavich
Copy link
Contributor

IlyaSavich commented Nov 17, 2016

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?

@felorodri
Copy link

felorodri commented Nov 17, 2016

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?

@IlyaSavich
Copy link
Contributor Author

IlyaSavich commented Nov 18, 2016

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

@felorodri
Copy link

felorodri commented Nov 18, 2016

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

@felorodri
Copy link

felorodri commented Nov 21, 2016

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.

@kodeine
Copy link
Owner

kodeine commented Nov 22, 2016

@felorodri @IlyaSavich
is this related to #169?

@IlyaSavich
Copy link
Contributor Author

IlyaSavich commented Nov 25, 2016

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

@IlyaSavich
Copy link
Contributor Author

IlyaSavich commented Nov 28, 2016 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants