1. 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?

Discussion in 'XenForo Development Discussions' started by AndyB, Oct 4, 2013.

  1. AndyB

    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)
      }
    }
     
  2. AndyB

    AndyB Well-Known Member

    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';
               }
             }
           }
         }
     
  3. Jeremy

    Jeremy Well-Known Member

    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();
     
    euantor likes this.
  4. AndyB

    AndyB Well-Known Member

    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();
         }
     
  5. tyteen4a03

    tyteen4a03 Well-Known Member

    XenForo_Permissions::has(Content)Permission()
     
    Jeremy likes this.
  6. AndyB

    AndyB Well-Known Member

    Hi tyteen4a03,

    How would I use this command to verify if the user has the permission to view deleted posts and threads?
     
  7. tyteen4a03

    tyteen4a03 Well-Known Member

    if (XenForo_Permission::hasPermission($userPermissionArray, "profilePosts", "viewDeleted"))

    I strongly recommend you to dig into the code - much faster than posting here.
     
    Jeremy and euantor like this.
  8. Jeremy

    Jeremy Well-Known Member

    Since it's a Boolean, your conditional can be simplified:
    PHP:
    if(!$viewDeleted)
     
    Marcus likes this.
  9. AndyB

    AndyB Well-Known Member

    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'];
     
  10. Daniel Hood

    Daniel Hood Well-Known Member

    What about
    PHP:
    if (!XenForo_Visitor::getInstance()->hasPermission('forum''viewDeleted'))
    Lol. So many possible ways.
     
    HWS and Marcus like this.

Share This Page