-
Notifications
You must be signed in to change notification settings - Fork 333
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
HasRolesAndAbilities trait conflict in Laravel 5.4.31 #187
Comments
I encountered a similar/related issue, though there's an easy enough workaround (just use the It affects Laravel 5.4.30. I assume it would affect 5.4.31 as well, though I haven't checked.
Laravel 5.4.30's $user = App\User::first();
// { id: 1, name: "Steve", ... }
$role = \Silber\Bouncer\Database\Role::first();
// { id: 1, name: "admin", ... }
// both result in error
$user->is('admin');
$user->is($role->name);
TypeError: Argument 1 passed to Illuminate\Database\Eloquent\Model::is()
must be an instance of Illuminate\Database\Eloquent\Model, string given on line 1 Eloquent\Model's // Illuminate\Database\Eloquent\Model
// around line 990
/**
* Determine if two models have the same ID and belong to the same table.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return bool
*/
public function is(Model $model)
{
// ...
} Workarounds1. Just use the FacadeIt's easy enough to work around the problem by just using the facade instead of directly asking the e.g. $user = App\User::first();
// { id: 1, name: "Steve", ... }
$role = \Silber\Bouncer\Database\Role::first();
// { id: 1, name: "admin", ... }
Bouncer::is($user)->a($role->name);
// true
Bouncer::is($user)->an('admin');
// true 2. Just make your own method(s)You could also simply define your own method(s) directly on your // app\User.php
class User
{
public function hasRole($role)
{
if ($role instanceof \Silber\Bouncer\Database\Role) {
$role = $role->name;
}
return \Bouncer::is($this)->a($role);
}
// public function hasAllRoles()
// public function hasAnyRoles()
// and so on...
} I'm sure there are other approaches, but these two will work for you if you really need to check a user's role directly from an I don't mind if these direct user role checks are deprecated or removed entirely; I agree that we should check for abilities instead of roles. I'm ok with Facades. But the I did spend some more time attempting to override the traits, substituting other traits/methods using |
I'm afraid we'll have to do the same thing we did to |
@JosephSilber We are also having the same issue. For now we'll just downgrade to Laravel 5.4.0 anyway we are still on early part of the development. Hope to receive the bug fix soon when you have time. Thank you. |
laravel new my-app
App\User
modelcomposer require silber/bouncer v1.0.0-beta.2
and install per instructionsAdding the
HasRolesAndAbilities
trait to defaultApp\User
model results in an errorDigging a bit, it looks like
Illuminate\Database\Eloquent\Model
now defines its ownisNot()
method:HasRoles
trait is responsibleThis doesn't complain:
But
use HasRoles
does squak:I tried to override
isNot()
on myApp\User
model, but Bouncer'sisNot()
takes a(string) $role
argument, while Model'sisNot()
expects aModel
instance (isNot(Model $model) {}
).Not sure if there's an easy fix for this. I know it's possible to substitute trait-provided functions, but I don't know if/how much Bouncer uses
isNot($role)
internally.In my case, I can probably just stick w/ Laravel 5.4.30.
The text was updated successfully, but these errors were encountered: