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

AndyB

Well-known member
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)
  }
}
 
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';
           }
         }
       }
     }
 
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();
 
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();
     }
 
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()
 
Hi tyteen4a03,

How would I use this command to verify if the user has the permission to view deleted posts and threads?
if (XenForo_Permission::hasPermission($userPermissionArray, "profilePosts", "viewDeleted"))

I strongly recommend you to dig into the code - much faster than posting here.
 
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)
 
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'];
 
Top Bottom