How to check for existence of a permission
I create a permission $PermissionName
in Vanilla 2.0 dynamically and now want to show "you do not have permission" if (in_array($PermissionName, $PermissionArray) || !$Session->CheckPermission($PermissionName))
.
$Session->CheckPermission($PermissionName) shows false if PermissionName does not exist and so I have to check for the existance of a permission called PermissionName.
My problem is that I have no $PermissionArray to check my PermissionName against. I've looked at the PermissionModel and foundpublic function GetPermissions($RoleID, $LimitToSuffix = '')
If the RoleID would be in $Session, I'd be happy, but it is not
So I'd either need a Vanilla function that replaces my in_array($PermissionName, $PermissionArray)
or I need a way to get $PermissionArray.
The only way I see by now would be:
1. get the user from $Session
2. get user roles with RoleModels GetByUserID
3. loop through roles, call GetPermissions from PermissionModel and concat the results to $PermissionArray
4. do my in_array($PermissionName, $PermissionArray) check
But that would be around ten lines of code for something that I assume is a function somewhere in the dashboard application.
Best Answer
-
hgtonight MVP
I get a full list of permissions with the following code in Yaga:
$PermissionModel = new PermissionModel(); $Permissions = $PermissionModel->PermissionColumns(); unset($Permissions['PermissionID']); $PermissionKeys = array_keys($Permissions); $PermissionList = array_combine($PermissionKeys, $PermissionKeys);
This spits out a nice array ready for
Gdn_Form::Dropdown()
, but you only really need the first two lines. I haven't tested this on 2.0, but it looks likePermissionColumns()
exists in 2.0.18.10.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.
3
Answers
I get a full list of permissions with the following code in Yaga:
This spits out a nice array ready for
Gdn_Form::Dropdown()
, but you only really need the first two lines. I haven't tested this on 2.0, but it looks likePermissionColumns()
exists in 2.0.18.10.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.
Perfect! PermissionColumns is the $PermissionArray I've needed
Just for completeness: in_array will not work, but that does the job
array_key_exists($PermissionName, Gdn::PermissionModel()->PermissionColumns())
can you change existance to existence in the title and discussion. so when I search for "existence permission" intuitively. I will find it. Also maybe this could go in tutorials as well.
I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.