Check permission from UserID
![alan0209](https://secure.gravatar.com/avatar/ae3b42909bec2c662c8436f36072fa11/?default=https%3A%2F%2Fvanillicon.com%2Ff1dca2437ba5b092c54a3590f7d4d6e2_100.png&rating=g&size=100)
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
-
hgtonight MVP
He is saying it is important to check the permissions a user has rather than the roles assigned to them when doing most things.
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
5
Answers
Take alook at the RoleModels function GetByUserID
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
stolen from Comment_206707![:stuck_out_tongue: :stuck_out_tongue:](https://open.vanillaforums.com/plugins/emojiextender/emoji/little/tongue.png)
I also found I could just pull the roles from
@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
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.
He is saying it is important to check the permissions a user has rather than the roles assigned to them when doing most things.
Search first
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
Note one of the problem is roles, is you assume that the name/id will always be the same. But you may change it. This will cause a decoupling.
Even superficial things, what is role without permissions?
grep is your friend.
Ah OK so something like this then?
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: :stuck_out_tongue:](https://open.vanillaforums.com/plugins/emojiextender/emoji/little/tongue.png)
Sorry I have appear to confused.
Permission checks usually concern the requester only.
So this concern roles.
grep is your friend.
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.
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![:) :)](https://open.vanillaforums.com/plugins/emojiextender/emoji/little/smile.png)
My shop | About Me
@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
and hopefully won't have to think about photos again for a while![:) :)](https://open.vanillaforums.com/plugins/emojiextender/emoji/little/smile.png)
Cheers
Alan
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.