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

collisions in using traits

Open ra3oul opened this issue 8 years ago • 8 comments

in laravel 5.1.11 your HasRole trait have collisions with laravel embed traits because in this version of laravel acl system is added check this out

ra3oul avatar Sep 01 '15 05:09 ra3oul

I think I have the same problem

use Kodeine\Acl\Traits\HasRole;

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

in php artisan tinker I've this message

[Symfony\Component\Debug\Exception\FatalErrorException]                                                   
  Trait method can has not been applied, because there are collisions with other trait methods on App\User 

maybe this fix?

  use Authenticatable, Authorizable, CanResetPassword, HasRole {
        HasRole::can insteadof Authorizable;
    }

but i'm a newbie...

wdog avatar Sep 04 '15 14:09 wdog

same here

ghost avatar Sep 13 '15 19:09 ghost

So how you guys solve this?

hstung11 avatar Oct 04 '15 07:10 hstung11

Like i've said i've solved using this in class User, I don't know if is the best solution but works

 use Authenticatable, Authorizable, CanResetPassword, HasRole {
        HasRole::can insteadof Authorizable;
    }

wdog avatar Oct 04 '15 09:10 wdog

So far I used

use Authenticatable, CanResetPassword, HasRole; instead of //use Authenticatable, Authorizable, CanResetPassword;

hikmatbiskandar avatar Oct 12 '15 04:10 hikmatbiskandar

use Kodeine\Acl\Traits\HasRole;

class User extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract { use Authenticatable, CanResetPassword, HasRole;

it will fix your issue

nadeem0035 avatar Jan 08 '16 12:01 nadeem0035

I think it's wrong to override the laravel gate instead do this:

use ...., HasRole {
        Authorizable::can insteadof HasRole;
        HasRole::can as havePermission;
    }

as the can() is more appropriate in the gate service because the end point of really determinate if user can do some action is inside the gate can where you could do like:

$gate->define('update-post', function ($user, $post) {
            if ($user->id !== $post->user_id) return false;
            if (!$user->havePermission('update-post) return false;
            return true;
        });

as you can see if you name the method can is not true because a user can have permission of update post, but the post maybe not belong to him, so the "can" name should belong to the gate as it the end point of really saying if you can or can't do something

@kodeine

boynet avatar Jan 11 '16 18:01 boynet

The workaround to map the clashing method to a new method should at least be mentioned in the documentation IMO.

judgej avatar Mar 22 '16 23:03 judgej