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.
Options

How to check for existence of a permission

R_JR_J Ex-FanboyMunich Admin
edited February 2014 in Vanilla 2.0 - 2.8

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 found
public 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

  • Options
    hgtonighthgtonight ∞ · New Moderator
    Answer ✓

    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 like PermissionColumns() 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.

Answers

  • Options
    hgtonighthgtonight ∞ · New Moderator
    Answer ✓

    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 like PermissionColumns() 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.

  • Options
    R_JR_J Ex-Fanboy Munich Admin

    Perfect! PermissionColumns is the $PermissionArray I've needed

  • Options
    R_JR_J Ex-Fanboy Munich Admin

    Just for completeness: in_array will not work, but that does the job
    array_key_exists($PermissionName, Gdn::PermissionModel()->PermissionColumns())

  • Options

    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.

Sign In or Register to comment.