Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Try Vanilla Forums Cloud product
After February 6, this site will no longer have Facebook, Twitter, or OpenID sign-in options. Read our announcement about social media SSO support in 2.8 for more info.

Make sure you have a current, valid email address set in your profile and set a password so you can login without it. If you get locked out after that time, you can choose "Forgot Password" to fix it as long as a valid email is on your account.

Check if a user is in a given role

edited February 2012 in Development

Hi,
What is the easiest way to check if a user belongs to a given role. For example in my application i want a developers role, which they will enter after agreeing to some terms. So certain pages I only want to show if they belong to that role. How would i go about doing this.

Thanks,
Gary Mardell.

Comments

  • edited February 2012

    It appears its not the done thing in Vanilla, and roles are purely to define groups of permissions. So I will use that method, however for anyone wanting code to do this, there may be a better way:

    $Session = Gdn::Session();      
    $UserModel = Gdn::UserModel();      
    $Roles = $UserModel->GetRoles($Session->UserID);
    $RoleNames = ConsolidateArrayValuesByKey($Roles, 'Name');       
    if(in_array('Member', $RoleNames)) {
        $HasMemberRole = TRUE;
    }
    
  • jspautschjspautsch Themester ✭✭✭

    I think you're going about this the wrong way. You want to check if someone has a certain permission, not role. You use the role to assign that permission, and then just check the permission when viewing the page, which is really simple.

    For example:

    if (Gdn::Session()->CheckPermission('Vanilla.Categories.Manage'))
        //do stuff
Sign In or Register to comment.