Zizaco / entrust

Role-based Permissions for Laravel 5
MIT License
6.05k stars 1.29k forks source link

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

Open whoacowboy opened 8 years ago

whoacowboy commented 8 years ago

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;
}
stanwarri commented 8 years ago

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

whoacowboy commented 8 years ago

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.

mhnrcjgs commented 8 years ago

follow

doncadavona commented 8 years ago

@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
whoacowboy commented 8 years ago

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?

doncadavona commented 8 years ago

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?

JFOC commented 8 years ago

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 ?

whoacowboy commented 8 years ago

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

luffyzhao commented 8 years ago

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

At 2015-09-24 03:04:48, "James" notifications@github.com 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.

kilrizzy commented 8 years ago

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

pedzed commented 8 years ago

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.

jago86 commented 8 years ago

pedzed solution worked for me

slavensaka commented 8 years ago

@pedzed solution works well for Laravel 5.1.

shraddhabanerjee commented 8 years ago

@pedzed Thanks, it worked :)

milewski commented 8 years ago

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

pedzed commented 8 years ago

@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?

milewski commented 8 years ago

@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
pedzed commented 8 years ago

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.

jnbn commented 8 years ago

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

ariefadjie commented 8 years ago

This working for me. Thanks @pedzed

class User extends Model implements AuthenticatableContract,
                                    //AuthorizableContract,
                                    CanResetPasswordContract
{
    use Authenticatable,
        //Authorizable,
        EntrustUserTrait,
        CanResetPassword;
mixisLv commented 8 years ago

conflicts with use SoftDeletes;

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

omegachien commented 8 years ago

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; }

kp77 commented 8 years ago

@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 commented 8 years ago

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

josejlpp commented 8 years ago

@omegachien Very Thank!

jcxnet commented 8 years ago

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

tranxuandien commented 8 years ago

@omegachien thanks you !good solution

pedzed commented 8 years ago

@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.

solidevolution commented 7 years ago

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 ;)

cjmling commented 7 years ago

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; }
farhanwazir commented 7 years ago

No luck, i have tried with the both ways, even i have override restore method.

jyj1993126 commented 7 years ago

I've done a trick like this:


  use SoftDeletes,EntrustUserTrait{
        SoftDeletes::restore as soft_delete_restore;
        EntrustUserTrait::restore as entrust_restore;
    }

    public function restore()
    {
        $result = $this->soft_delete_restore();
        $this->entrust_restore();
        return $result;
    }
rojonunoo commented 6 years ago

removing the Authorizable, solved the issue for me