9

I am trying to run a check to see if the current user is in a certain role with Cartalyst's Sentinel/Sentry package.

I have found Sentinel::inRole('admin') but can't seem to get it to work. I want something like this:

if(Sentinel::getUser()->hasRole('admin'){ // do some stuff }

I've searched and searched for this and can't find an example to work with. Should I be looking more into permissions or something?

Hopefully someone out there can point me in the right direction.

Cheers

T2theC
  • 542
  • 3
  • 10
  • 23

3 Answers3

13

You can use inRole here, Sentinel::getUser()->inRole('role_slug');. Of course this would only work if a user is logged in.

You might wanna change it to something like this to prevent calling inRole on a non object.


    if ($user = Sentinel::getUser())
    {
        if ($user->inRole('administrator'))
        {
            // Your logic
        }
    }

Hope that helps.

  • Thanks Suhayb. I have tried that and am getting and error (Used in a blade template: @if(Sentinel::getUser()->inRole('mangers') || Sentinel::getUser()->inRole('admin') ). I am getting this error: Call to undefined method Illuminate\Database\Query\Builder::inRole(). Any ideas? Thanks very much for helping. – T2theC Aug 19 '14 at 13:54
  • I'm also facing the same issue. I can get the logged user from the controller. But I cannot get the logged user in the blade php. I mean in the front end. How to check the logged user permissions in the front end. – Manula Thantriwatte May 27 '16 at 06:25
2

Have your User model extend Cartalyst's 'EloquentUser' model. This model has a getRoles() function that will return the roles for that user. Each user also has an inRole( $role ) function that will return true if the user has the role specified in the parameter.

Extention would look like this:

use Cartalyst\Sentinel\Users\EloquentUser;

class User extends EloquentUser implements ... {
    ....
}

In my opinion this is the cleanest way to accomplish what you want.

If you don't want to do that you could check permissions instead and have your permissions configured like a role

$admin = Sentinel::create()

$admin->permissions = [
    'admin' => true,
];

$admin->save();

Then check the user's permissions instead of checking their role

if($admin->hasAccess('admin') { // do some stuff }

Hope this helps.

-Josh E

jerney
  • 2,187
  • 1
  • 19
  • 31
  • The extension will allow you to check if any user (logged in or not) is part of a certain role. It's also worth noting that Cartalyst's API documentation on Sentinel (on their website) could be better, so if you can't find what you're looking for on their website, their source code is a good place to look. – jerney Aug 20 '14 at 04:07
  • Bravo! This is definitely the cleanest method. Also, it may be that you wish to check whether or not a certain user, who is *not logged-in*, is in a given role. In such cases, you may do this: `User::find(1)->inRole('admin')`, where `1` is the user's ID and `'admin'` is the role.. – Ben Johnson Nov 03 '17 at 13:34
0

I first check if authorized and if so then watch what role

so works

@if(!Sentinel::guest())
   @if(Sentinel::inRole('user'))
      <ul class="nav navbar-nav navbar-right">
         <li><a href="logout">{{trans('master.logout')}}</a></li>
      </ul>
   @endif
@endif
Alex
  • 85
  • 1
  • 4
  • When I check the Sentinel::check() in the front-end it says false. But when I check it from the controller it's true. I want to know whether how to get the Sentinel user object to the front-end. – Manula Thantriwatte May 27 '16 at 06:27