Sentry 2 for laravel 4 ACL

1k Views Asked by At

Laravel 4 with Sentry 2 for ACL Note: I am using laravel functions for login, but need sentry only for ACL purpose, to check permissions

Want to achieve this:

if ( Sentry::getUser()->hasAnyAccess(['system']) )
{
echo 'has access to system';
}

but i keep getting the following error:

Sentry::getUser()->hasAnyAccess(['system']); //this hits error: Call to a member function hasAnyAccess() on a non-object 
3

There are 3 best solutions below

0
On

The way Sentry2 does authentication is not compatible with Laravel's built in authentication system. As far as I understand Laravel's built in authentication system and Sentry2 set different session keys to store the details of the logged in user. So it is not possible to use Sentry2 to pull up the details about laravel authenticated user. But, given that you use the same database table for both User model and the model used with Sentry2 this should work.

    Sentry::findById(Auth::user()->id)->hasAnyAccess(['system'])

If you want to use Sentry in built in auth compatible way you may want to check this package.

https://bitbucket.org/hampel/sentry-auth-driver-for-laravel

0
On

If you interested there is a laravel package that integrates sentry with an admin panel: https://github.com/intrip/laravel-authentication-acl

0
On

The problem here is that Laravel Auth and Sentry do not use the same variables to store user information, so when you authenticate with Laravel's Auth::attempt(), Sentry::getUser() will still return null.

The opposite is also true - if you authenticate with Sentry::authenticate(), Auth::user() does not return an object.

The simplest solution is to do an all-or-nothing approach to authentication; either swap out the Laravel portions for Sentry, or vice verse.

One way I've implemented role based ACL in native Laravel is by adding a roles table to the database which has a name column on it + a pivot table, then adding this access checking code to my User driver. That code allows me to check ACLs via syntax like Auth::user()->is(["admin", "publisher"]), etc.