Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Check permission from UserID

I know I can check if the current user is an admin using something like

$IsAdmin = Gdn::Session()->CheckPermission('Garden.Users.Edit');

Can I perform a similar check based on a UserID rather than the active session? I am writing a plugin that assigns different avatars based on user roles. The issue I have at the moment is getting the right image to show when I visit a users profile page (at the moment it shows the image for the logged on user rather than the profile I am visiting).

This is in Vanilla 2.1.3

Best Answers

Answers

  • I am writing a plugin that assigns different avatars based on user roles.

    Ok this is an exception to the rule you check the permission not the role. Where is concern access and control always check the permission not the role.

    Where it concern superficial things, you can check the role. But more than one role can be applied.

    grep is your friend.

  • Thanks R_J. I came up with

    $RoleModel = new RoleModel();
    $Roles = $RoleModel->GetByUserID($Sender->User->UserID)->Result(DATASET_TYPE_ARRAY);
    

    stolen from Comment_206707 :stuck_out_tongue:

    I also found I could just pull the roles from

    $Roles = $Sender->Data["UserRoles"];
    

    @x00 I'm not sure I follow you there? I'm just trying to see if a user is an Admin so I can display the appropriate photo so it that OK to check roles?

    Cheers

  • I'm not sure I follow you there? I'm just trying to see if a user is an Admin so I can display the appropriate photo so it that OK to check roles?

    It is ok.

    If the roles already in the context of the hook, then do not re query them, especially if you are looping this is bad even with built in caching.

    grep is your friend.

  • Ah OK so something like this then?

    $PermissionModel = new PermissionModel();
    $Perms = $PermissionModel->GetUserPermissions($Sender->User->UserID);
    
     foreach ($Perms as $Perm) {
        if ($Perm["Garden.Users.Edit"]) { $IsAdmin = True; }
    }
    
  • LincLinc Detroit Admin

    If your goal is to display an image per role, I suggest adding a "Photo" column to the Role table, adding it to the Edit Role form, and then simply using the data in Role.Photo wherever you want to display it. No need to involve permissions at all.

  • @Linc said:
    If your goal is to display an image per role, I suggest adding a "Photo" column to the Role table, adding it to the Edit Role form, and then simply using the data in Role.Photo wherever you want to display it. No need to involve permissions at all.

    Just when I was happy that I had it all working :stuck_out_tongue:

  • Sorry I have appear to confused.

    Permission checks usually concern the requester only.

    So this concern roles.

    grep is your friend.

  • peregrineperegrine MVP
    edited September 2014

    I think it has to do with roles.

    however the original problem he encountered - he was checking session userid and role , instead of sender userid and role.

    and then his question and issue morphed.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • businessdadbusinessdad Stealth contributor MVP

    @Linc said:
    If your goal is to display an image per role, I suggest adding a "Photo" column to the Role table

    If you like the "path of CSS adventure", you can also add a RoleCSS field and enter all photos, styles and anything you like. Customisation ÜberCharge :)

  • alan0209alan0209 New
    edited September 2014

    @peregrine Good job I'm made out of plasticine then (though you probably have to be over a certain age to get that reference :) )

    Anyway, thanks everyone for all the input. I don't think the approach really matters in my scenario and I was able to achieve what i had in mind with the input from this thread (using either roles or permissions). My site will only have a couple of admins and I want them to get one photo and everyone else (who will simply be members) to get another. That is now working a treat and so I've set

    $Configuration['Garden']['Profile']['EditPhotos'] = FALSE;
    

    and hopefully won't have to think about photos again for a while :)

    Cheers

    Alan

  • LincLinc Detroit Admin

    @businessdad said:
    If you like the "path of CSS adventure", you can also add a RoleCSS field and enter all photos, styles and anything you like. Customisation ÜberCharge :)

    The Role Title addon would do that for you.

  • @alan0209 the path does matter becuase you don't wan to do unnecessarily taxing operations especially in a loop.

    grep is your friend.

Sign In or Register to comment.