How to check whether a user has permissions to a category
I know how to check permission with the checkPermission() function for a permission set by a plugin through "RegisterPermissions" (e.g. Plugins.Preview.View and checkPermission('Plugins.Preview.View')).
What it is not obvious to me how I check whether the user has permission to add or View discussions in a specific category (or for that matter other category-related permissions like add/view comments).
Can it be done via checkPermission and what parameters do I pass to it?
If not, how else can I check for these permissions?
Best Answer
-
R_J Admin
The function checkPermission is a "shortcut" for the method checkPermission which is in class session. The shortcut function takes too few arguments to do what you like, so you need to use it like that:
// If you already have an existing discussion. $permissionCategoryID = $discussion->PermissionCategoryID; $hasPermission = Gdn::session()->checkPermission( 'Vanilla.Discussions.View', true, 'Category', $permissionCategoryID );
This method returns a boolean value and you can decide what to do. If you want to simulate the way Vanilla behaves when users have not enough rights, use a similar method which redirects users in case of missing rights:
// $sender must be derived from class controller (which should nearly everytime be the case) $sender->permission( 'Vanilla.Discussions.View', true, 'Category', $permissionCategoryID );
6
Answers
Yes, This is an example:
❌ ✊ ♥. ¸. ••. ¸♥¸. ••. ¸♥ ✊ ❌
I am sorry, as I stated, I already know how to check permissions for plugins. What I am asking is how to check for permission to add discussions, view discussions, add comments, and view comments in a specific category.
Here are most of the configs for vanilla... you can make your own permissions to be followed or you can use Vanilla configs ... look at plugins that use permissions to get a clue. If you know how to check for permissions in a plugin then you already know everything you need..
Look at plugins that do this there already are several...
❌ ✊ ♥. ¸. ••. ¸♥¸. ••. ¸♥ ✊ ❌
The function checkPermission is a "shortcut" for the method checkPermission which is in class session. The shortcut function takes too few arguments to do what you like, so you need to use it like that:
This method returns a boolean value and you can decide what to do. If you want to simulate the way Vanilla behaves when users have not enough rights, use a similar method which redirects users in case of missing rights:
Thank you both for asnwering. @R_J - This is very helpful. I will give this a try and see what I come up with;-). Much appreciated!