• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

How do I extract a single variable from this permissions array?

AndyB

Well-known member
#1
I would like to extract the viewDeleted value from this $permissions array:

PHP:
array(6) {
  ["profilePost"]=>
  array(14) {
    ["deleteAny"]=>
    bool(true)
    ["hardDeleteAny"]=>
    bool(true)
    ["undelete"]=>
    bool(true)
    ["editAny"]=>
    bool(true)
    ["approveUnapprove"]=>
    bool(true)
    ["viewModerated"]=>
    bool(true)
    ["viewDeleted"]=>
    bool(true)
    ["view"]=>
    bool(false)
    ["like"]=>
    bool(false)
    ["manageOwn"]=>
    bool(false)
    ["post"]=>
    bool(false)
    ["deleteOwn"]=>
    bool(false)
    ["editOwn"]=>
    bool(false)
    ["warn"]=>
    bool(true)
  }
  ["avatar"]=>
  array(2) {
    ["maxFileSize"]=>
    int(-1)
    ["allowed"]=>
    bool(true)
  }
  ["general"]=>
  array(17) {
    ["editSignature"]=>
    bool(true)
    ["view"]=>
    bool(true)
    ["viewNode"]=>
    bool(true)
    ["viewIps"]=>
    bool(true)
    ["bypassUserPrivacy"]=>
    bool(true)
    ["viewProfile"]=>
    bool(true)
    ["search"]=>
    bool(true)
    ["cleanSpam"]=>
    bool(true)
    ["maxTaggedUsers"]=>
    int(0)
    ["viewWarning"]=>
    bool(true)
    ["warn"]=>
    bool(true)
    ["manageWarning"]=>
    bool(true)
    ["editProfile"]=>
    bool(true)
    ["editCustomTitle"]=>
    bool(true)
    ["followModerationRules"]=>
    bool(true)
    ["bypassFloodCheck"]=>
    bool(false)
    ["report"]=>
    bool(false)
  }
  ["forum"]=>
  array(26) {
    ["stickUnstickThread"]=>
    bool(true)
    ["manageAnyThread"]=>
    bool(true)
    ["viewOthers"]=>
    bool(true)
    ["viewContent"]=>
    bool(true)
    ["like"]=>
    bool(true)
    ["postThread"]=>
    bool(true)
    ["postReply"]=>
    bool(true)
    ["deleteOwnThread"]=>
    bool(true)
    ["deleteOwnPost"]=>
    bool(true)
    ["editOwnPost"]=>
    bool(true)
    ["editOwnPostTimeLimit"]=>
    int(-1)
    ["editOwnThreadTitle"]=>
    bool(true)
    ["viewAttachment"]=>
    bool(true)
    ["uploadAttachment"]=>
    bool(true)
    ["votePoll"]=>
    bool(true)
    ["approveUnapprove"]=>
    bool(true)
    ["viewModerated"]=>
    bool(true)
    ["lockUnlockThread"]=>
    bool(true)
    ["viewDeleted"]=>
    bool(true)
    ["hardDeleteAnyPost"]=>
    bool(true)
    ["editAnyPost"]=>
    bool(true)
    ["hardDeleteAnyThread"]=>
    bool(true)
    ["deleteAnyThread"]=>
    bool(true)
    ["deleteAnyPost"]=>
    bool(true)
    ["undelete"]=>
    bool(true)
    ["warn"]=>
    bool(true)
  }
  ["conversation"]=>
  array(7) {
    ["start"]=>
    bool(true)
    ["editAnyPost"]=>
    bool(true)
    ["alwaysInvite"]=>
    bool(true)
    ["uploadAttachment"]=>
    bool(true)
    ["editOwnPost"]=>
    bool(true)
    ["editOwnPostTimeLimit"]=>
    int(1440)
    ["maxRecipients"]=>
    int(15)
  }
  ["signature"]=>
  array(14) {
    ["basicText"]=>
    bool(true)
    ["extendedText"]=>
    bool(true)
    ["align"]=>
    bool(true)
    ["list"]=>
    bool(true)
    ["image"]=>
    bool(true)
    ["link"]=>
    bool(true)
    ["media"]=>
    bool(true)
    ["block"]=>
    bool(true)
    ["maxPrintable"]=>
    int(-1)
    ["maxLinks"]=>
    int(-1)
    ["maxLines"]=>
    int(-1)
    ["maxImages"]=>
    int(-1)
    ["maxSmilies"]=>
    int(-1)
    ["maxTextSize"]=>
    int(-1)
  }
}
 

AndyB

Well-known member
#2
Got it.

PHP:
    $permissions = XenForo_Visitor::getInstance()->getPermissions();

     foreach ($permissions as $k => $v)
     {
       $array = $v;
       
       foreach ($array as $k => $v)
       {
         if ($k == 'viewDeleted')
         {
           if ($v == false)
           {
             $viewDeleted = 'false';
           }
           if ($v == true)
           {
             $viewDeleted = 'true';
           }
         }
       }
     }
 

Jeremy

Well-known member
#3
For one, your for loops will fumble and fail. Do not overwrite variables. Secondly, you should google and read up on arrays, because it's completely accessible without a loop:
PHP:
$array['profilePost']['viewDeleted'];
Thirdly, XenForo has a function to check permissions and if someone has it. I don't know it off the top of my head (class at least), but it's hasPermission();
 

AndyB

Well-known member
#4
Hi Jeremy,

Thank you for offering assistance.

This works great!

PHP:
    $permissions = XenForo_Visitor::getInstance()->getPermissions();
    
     $viewDeleted = $permissions['forum']['viewDeleted'];
    
     if ($viewDeleted == false)
     {
       throw $this->getNoPermissionResponseException();
     }
 

tyteen4a03

Well-known member
#5
For one, your for loops will fumble and fail. Do not overwrite variables. Secondly, you should google and read up on arrays, because it's completely accessible without a loop:
PHP:
$array['profilePost']['viewDeleted'];
Thirdly, XenForo has a function to check permissions and if someone has it. I don't know it off the top of my head (class at least), but it's hasPermission();
XenForo_Permissions::has(Content)Permission()
 

Jeremy

Well-known member
#8
Hi Jeremy,

Thank you for offering assistance.

This works great!

PHP:
    $permissions = XenForo_Visitor::getInstance()->getPermissions();
   
     $viewDeleted = $permissions['forum']['viewDeleted'];
   
     if ($viewDeleted == false)
     {
       throw $this->getNoPermissionResponseException();
     }
Since it's a Boolean, your conditional can be simplified:
PHP:
if(!$viewDeleted)
 

AndyB

Well-known member
#9
Thank you, tyteen4a03 and Jeremy for your assistance.

Here is the code I will use:

PHP:
     $permissions = XenForo_Visitor::getInstance()->getPermissions();
     $viewDeleted = XenForo_Permission::hasPermission($permissions, "forum", "viewDeleted");
  
     if (!$viewDeleted)
     {
       throw $this->getNoPermissionResponseException();
     }
Which is better to use?

$viewDeleted = XenForo_Permission::hasPermission($permissions, "forum", "viewDeleted");

or

$viewDeleted = $permissions['forum']['viewDeleted'];