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

@can not working as expected #13

Closed
santiagogg opened this issue Oct 10, 2015 · 17 comments
Closed

@can not working as expected #13

santiagogg opened this issue Oct 10, 2015 · 17 comments

Comments

@santiagogg
Copy link

Hi there,
I upgraded from version 5.1. following http://laravel.com/docs/5.1/upgrade#upgrade-5.1.11
Everything seems to work but $user->can('create post') is false when $user->hasPermissionTo('create post') is true... What I'm missing?

Thanks!

@freekmurze
Copy link
Member

Did you register the service provider?

@AEK-BKF
Copy link

AEK-BKF commented Dec 24, 2015

same issue with me !!!

@freekmurze
Copy link
Member

@AEK-BKF Did you register the service provider? Which error are you getting?

@AEK-BKF
Copy link

AEK-BKF commented Dec 26, 2015

Yes I did, @ROLE works fine, but @can always returns true if the permission exists, and false when not.
here my User Model code :

@jpmurray
Copy link

jpmurray commented Jan 9, 2016

I'm not sure if I'm having the same trouble, but...

$permission = Permission::create(['name' => 'create user']);
$role = Role::findByName('superadmin');
$role->givePermissionTo('create user');

Works fine.For the current user, $user->hasRole('superadmin'); returns true.

But trying to do $user->can('create user'); will result in

FatalErrorException in Model.php line 1011:
Class name must be a valid object or a string

Any idea?

EDIT: yes, everyting looks fine in the database. and I do have the service provider registered.

@AEK-BKF
Copy link

AEK-BKF commented Jan 9, 2016

I solved my issue by editing hasRole & PermissionServiceProvider classes like this :
Function hasDirectPermission in hasRole class :
public function hasDirectPermission($permissions)
{
if (is_string($permissions)) {
return $this->permissions->contains('name', $permissions);
}
if ($permissions instanceof Permission) {
return $this->permissions->contains('id', $permissions->id);
}

    return $this->permissions->contains('name', $permissions);
}

registerBladeExtensions in PermissionServiceProvider class :
Add these lines :
$bladeCompiler->directive('permission', function ($permission) {
return "check() && auth()->user()->hasDirectPermission({$permission})): ?>";
});
$bladeCompiler->directive('endpermission', function () {
return '';
});

And all is working fine , I can use this : @Permission('Add-User') it works :))
Try it @jpmurray and tell me if it's ok ^^'

@jpmurray
Copy link

jpmurray commented Jan 9, 2016

I'll wait for @freekmurze input on that before messing with the package files ;-)

@jpmurray
Copy link

jpmurray commented Jan 9, 2016

Welp, shame on me. My issue is basically related to me not scrolling enough in my files. I just found a old stray "permissions" function in my User model. Now that it's gone, everything works fine :/

@freekmurze
Copy link
Member

@jpmurray nice, I'm glad you found the solution quickly 👍

@jpmurray
Copy link

@freekmurze although are the permissions functions (can, hasPermissionTo, etc) works with $roles ?

Curently trying to do a $role->hasPermissionTo($permission->name) that results a Call to undefined method Illuminate\Database\Query\Builder::hasPermissionTo()...

(EDIT: I'm actually trying to have an interface update a role, I think I'll try Laravel's attach, detach to the Model, let's see if that works!)

(EDIT 2 : And of course, sync() is the way to go! I don't know what I was stuborn and kept trying to just loop through them :P Oh well!)

@freekmurze
Copy link
Member

No, see #31

On Sunday, 10 January 2016, Jean-Philippe Murray [email protected]
wrote:

@freekmurze https://github.com/freekmurze although are the permissions
functions (can, hasPermissionTo, etc) works with $roles ?

Curently trying to do a $role->hasPermissionTo($permission->name) that
results a Call to undefined method
Illuminate\Database\Query\Builder::hasPermissionTo()...


Reply to this email directly or view it on GitHub
#13 (comment)
.

Freek Van der Herten
https://spatie.be
+32 495 84 27 91

@razikallayi
Copy link

What is the final answer? @can is not working for me too.

@desaijay
Copy link

desaijay commented May 17, 2016

 public function boot(GateContract $gate)
    {
        $this->registerPolicies($gate);

         foreach ($this->getPermissions() as $permission) {
            $gate->define($permission->name, function ($user) use ($permission) {
                return $user->hasPermission($permission);
            });
        }
    }

      protected function getPermissions()
    {
        return Permission::with('roles')->get();
    }

add this to the AuthServiceProvider.php file and you can then use @can ???

I dont know wat @freekmurze will suggest, but even after registering the service provider class in app.php, @can doesnt work.

Well i dnt know whether to mess thecode or try it like @AEK-BKF did.

the only problems you can face is when assigning multiple roles andmultiple permissions. I think that multiple permissions can be assigned to a role, but users should be only given the role and then the role must check whether the user is assigned the role and it has certain permissions on it.

@freekmurze
Copy link
Member

Normally if you the service provider is registered correctly, the package will register all permissions at the gate.

This package has tests to prove that the provided functionality is working. If you are still having problem, could you please submit a failing test? That would help me immensely in finding the problem/solution.

@desaijay
Copy link

it was just a suggestion to above user, for me this package is working perfectly except @can is not working in blade templates and my testing is little poor, so cnt help you out much, lots of usage coming across the way.

I had jut one simple question, i had done something from the scratch , and was able to relate all the users roles and permission, and assigning the roles the permissions, and fetching, revoking and updating the roles and permission.

But i am still figuring how will i use the middleware to check the role of the user and then redirect to following path , and also the routes should be served to that roles only.

I have tried your another package, n thats works perfectly for me, but if you can help me from scratch.
There is function redirectPath , which i have modified in authcontroller.php, and it works with the redirection of particular type if users.

But this should be more normalised and i dnt want to mess with the authcontroller file, any help/hint would be good for me?

@simondotwhite
Copy link

I know this issue was closed, but I'm still running into the same issue. I've registered the service provider.

can() returns false even though the user has the permission through the role(s) assigned.
_hasPermission_To returns correctly

@freekmurze
Copy link
Member

Could you PR a failing test?

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

7 participants