Jump to content

user hasRole


Frank Vèssia
 Share

Recommended Posts

I cannot understand why i can't get role of user.

$userid = 40;
$role  = $roles->get("premium");
$uid = (int) $userid;
$u = $users->get($uid);
if ($u->hasRole($role)){
 ...stuff
}

i get a fatal error:

Method NullPage::hasRole does not exist or is not callable in this context

Link to comment
Share on other sites

This line failed to load a user:

$u = $users->get($uid);

User ID 40 doesn't exist in the system because anytime PW returns a NullPage that means it didn't find what you were looking for. The best way to check if you got a NullPage is to see if the returned has an ID. So your last section of code should be something like this:

if ($u->id && $u->hasRole($role)){
  ...stuff
}

Btw, ID 40 is the default ID for the 'guest' user account, so it would be unusual if that really is the ID of the user and it didn't load.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...