entrust icon indicating copy to clipboard operation
entrust copied to clipboard

Laravel 5.1.11 - Trait method can has not been applied, because there are collisions with other trait methods on App\User

Open whoacowboy opened this issue 8 years ago • 33 comments

It appears that the new Authorizable trait also has a can method. I have temporarily resolved it using may instead which I believe is grammatically more correct anyway.

use Authenticatable, CanResetPassword, PresentableTrait, Authorizable, EntrustUserTrait {
    EntrustUserTrait::can as may;
    Authorizable::can insteadof EntrustUserTrait;
}

whoacowboy avatar Sep 16 '15 18:09 whoacowboy

Also got the same error. @whoacowboy Don't you think you approach will cause some error down the line when using the package

stanwarri avatar Sep 17 '15 00:09 stanwarri

It's a temp fix, I updated the issue. I actually think that Taylor is planning on building roles and permissions into a later release so it won't be much of an issue going forward. I wish he'd just incorporate Entrust and make my life easy.

whoacowboy avatar Sep 17 '15 00:09 whoacowboy

follow

mhnrcjgs avatar Sep 17 '15 05:09 mhnrcjgs

@whoacowboy 's solution worked, but when I attach a role to a user like so:

$user->attachRole('1'); // or $user->roles()->attach('1');

This error occurs:

BadMethodCallException in Builder.php line 2025:
Call to undefined method Illuminate\Database\Query\Builder::attach()
screen shot 2015-09-20 at 1 59 47 am

doncadavona avatar Sep 19 '15 18:09 doncadavona

Hey @doncadavona, I am unable to reproduce this issue.

php artisan tinker
>>> $role1 = \App\Role::find(1);
>>> $role2 = \App\Role::find(2);
>>> $user = \App\User::find(32);
>>> $user->attachRole($role1);
>>> $user->roles()->attach($role2);

database checks out.

Have you tried composer dump-autoload?

Does attach and attachRole need an int instead of a string?

$user->attachRole(1);
// or
$user->roles()->attach(1);

Are you using any other packages or traits that touch User?

whoacowboy avatar Sep 21 '15 17:09 whoacowboy

Face palm, Just realized Entrust isn't compatible with Laravel 5.1. Entrust conflicts with Laravel 5.1's new built-in Authorization in 5.1: http://laravel.com/docs/5.1/authorization. I didn't realize Laravel had Authorization in 5.1 until now. Is it a goodbye to Entrust?

doncadavona avatar Sep 23 '15 03:09 doncadavona

I don;t think Laravel 5.1 have Role/Permissions things. Have you check

use Zizaco\Entrust\Traits\EntrustUserTrait; 
public function roles();

at Users Model ?

JFOC avatar Sep 23 '15 09:09 JFOC

@doncadavona Laravel still doesn't have roles and permissions, but I suspect it is coming in the near future, maybe 5.1.29

whoacowboy avatar Sep 23 '15 19:09 whoacowboy

maybe.. maybe probably never have . Is the problem solved?

At 2015-09-24 03:04:48, "James" [email protected] wrote:

@doncadavona Laravel still doesn't have roles and permissions, but I suspect it is coming in the near future, maybe 5.1.29

— Reply to this email directly or view it on GitHub.

luffyzhao avatar Sep 25 '15 02:09 luffyzhao

Another possible solution would be to just remove "use Authorizable" from the User Model, since we are using Entrust in place of Laravel's default. Setting this up on a current project will notify if I run into any issues

kilrizzy avatar Sep 30 '15 19:09 kilrizzy

Like @kilrizzy said, use Authorizable must be removed.

You can just replace this:

// ...
class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;
    // ...

with the following:

// ...
use Zizaco\Entrust\Traits\EntrustUserTrait;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable;
    use CanResetPassword;
    use EntrustUserTrait;
    // ...

I suggest updating the README and closing this issue.

pedzed avatar Oct 15 '15 13:10 pedzed

pedzed solution worked for me

jago86 avatar Oct 19 '15 20:10 jago86

@pedzed solution works well for Laravel 5.1.

slavensaka avatar Oct 26 '15 09:10 slavensaka

@pedzed Thanks, it worked :)

shraddhabanerjee avatar Oct 27 '15 10:10 shraddhabanerjee

@pedzed removing Authorizable will make gate stops working...

milewski avatar Oct 28 '15 07:10 milewski

@pedzed removing Authorizable will make gate stops working...

I know. You should either use Entrust or Laravel 5.1.11's built-in Authorization (http://laravel.com/docs/5.1/authorization). Why would you use both?

pedzed avatar Oct 28 '15 08:10 pedzed

@pedzed Laravel's built-in is different from entrust.. it`s not checking if user has role X .. it's mostly permissions not role... i mixed both by on the policy class calling something like this

return $user->hasRole('admin') 

in built in laravel u would need to check against something

return $user->id === $something->id

milewski avatar Oct 29 '15 01:10 milewski

Laravel's built-in is different from entrust.. it`s not checking if user has role X .. it's mostly permissions not role... i mixed both by on the policy class calling something like this

return $user->hasRole('admin')

in built in laravel u would need to check against something

return $user->id === $something->id

I know, but in my opinion, if you use Entrust, you don't need Laravel's Authorization. But if it's really something you want, you could work around it by writing your own code.

pedzed avatar Oct 29 '15 08:10 pedzed

It'd be better if this can be added to readme.md

jnbn avatar Dec 12 '15 16:12 jnbn

This working for me. Thanks @pedzed

class User extends Model implements AuthenticatableContract,
                                    //AuthorizableContract,
                                    CanResetPasswordContract
{
    use Authenticatable,
        //Authorizable,
        EntrustUserTrait,
        CanResetPassword;

ariefadjie avatar Mar 01 '16 07:03 ariefadjie

conflicts with use SoftDeletes;

Trait method restore has not been applied, because there are collisions with other trait methods on App\User

mixisLv avatar Mar 11 '16 11:03 mixisLv

One of the way is to modify as below, informing the class that we will use the restore in EntrustUserTrait instead of the one found in SoftDeletes, the restore() in EntrustUserTrait will be calling SoftDeletes' restore() anyway

use EntrustUserTrait { EntrustUserTrait::restore insteadof SoftDeletes; }

omegachien avatar Mar 29 '16 08:03 omegachien

@omegachien Thank you for the hint, this helped me with the conflict issue and now soft deletion works.

My only problem is, that when soft deleting the user the role relationships are deleted by Entrust, so when I restore the user, the roles are gone. Isn't the idea of soft deleting about keeping the related data, so it is their when restoring? Is there any way to keep the relations when soft deleting the user with Entrust?

kp77 avatar Apr 01 '16 17:04 kp77

I found the answer to my problem, it's described here: #306

kp77 avatar Apr 01 '16 18:04 kp77

@omegachien Very Thank!

josejlpp avatar May 17 '16 20:05 josejlpp

@omegachien thanks a lot, your fix works in Laravel 5.2 also

jcxnet avatar Jun 21 '16 16:06 jcxnet

@omegachien thanks you !good solution

tranxuandien avatar Jul 21 '16 04:07 tranxuandien

@josejlpp @jcxnet @tranxuandien Not too long ago, GitHub introduced reaction emojis. Please, just thumbs up. Over 20 people get an unnecessary email when you post something.

Thanks.

pedzed avatar Jul 21 '16 09:07 pedzed

What about this issue? I've got this "can" trait conflict with lumen 5.2, dingo api, jwt-auth and entrust. Could it solve with may but like to do it perfect ;)

solidevolution avatar Oct 06 '16 20:10 solidevolution

Beware of using @omegachien solution.

When i use his solution and tried to restore "a" single user. Every deleted user get restore instead.

Running code below restore "all" the deleted user , not just "one" when I use @omegachien solution.

$member = User::withTrashed()->where("id" , 1 )->first(); $member->restore();

I decided to use softdelete restore instead. ( hopefully no future bugs :S )

use EntrustUserTrait;
use SoftDeletes { SoftDeletes::restore insteadof EntrustUserTrait; }

cjmling avatar Oct 24 '16 09:10 cjmling