Sentry/Sentinel if user is in role

T2theC picture T2theC · Aug 18, 2014 · Viewed 12k times · Source

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

Answer

Suhayb El Wardany picture Suhayb El Wardany · Aug 18, 2014

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.