Airship\Cabin\Bridge\Blueprint\Permissions::buildGroupTree PHP Method

buildGroupTree() public method

Build a hierarchical tree containing all of the groups' permissions
public buildGroupTree ( string $cabin, integer $contextId, array $actions = [], integer $parentId, array $inherited = [], integer $depth ) : array
$cabin string Which cabin does this apply to?
$contextId integer Context IDs (see permissions sql)
$actions array Actions in Scope [ [id => label], [id2 => label2] ]
$parentId integer The group whose children we are evaluating
$inherited array
$depth integer the depth of our recursive search
return array
    public function buildGroupTree(string $cabin, int $contextId, array $actions = [], int $parentId = 0, array $inherited = [], int $depth = 0) : array
    {
        if ($depth > self::MAX_RECURSE_DEPTH) {
            return [];
        }
        if (empty($cabin) || empty($actions) || empty($contextId)) {
            return [];
        }
        // This is the tree we are building...
        $tree = [];
        // Let's grab all the groups with a specific ancestor
        if (empty($parentId)) {
            $groups = $this->db->run(\Airship\queryStringRoot('security.permissions.groups_null', $this->db->getDriver()));
        } else {
            $groups = $this->db->run(\Airship\queryStringRoot('security.permissions.groups_inherits', $this->db->getDriver()), $parentId);
        }
        // Nothing? Return an empty array
        if (empty($groups)) {
            return [];
        }
        // Let's safely construct a parameter to use to concatenate to the query
        $act_keys = \array_keys($actions);
        $_acts = $this->db->escapeValueSet($act_keys, 'int');
        $baseline = [];
        foreach (\array_values($actions) as $a) {
            $baseline[$a] = false;
        }
        // Now let's build the tree
        foreach ($groups as $grp) {
            $grp['perms'] = $baseline;
            $grp['inherit'] = $inherited;
            // Let's grab the permission data for this particular group
            $sqlQuery = \Airship\queryStringRoot('security.permissions.groups_qs', $this->db->getDriver(), ['actionids' => $_acts]);
            $pdata = $this->db->run($sqlQuery, $cabin, $contextId, $grp['groupid']);
            foreach ($pdata as $perm) {
                $grp['perms'][$perm['label']] = true;
                $grp['inherit'][$perm['label']] = true;
            }
            // Pass onto the next generation
            $grp['children'] = self::buildGroupTree($cabin, (int) $contextId, $actions, (int) $grp['groupid'], $grp['inherit'], $depth + 1);
            // Append this branch
            $tree[] = $grp;
        }
        // With any luck, we now have a hierarchical array to play with in
        // our template.
        return $tree;
    }

Usage Example

Example #1
0
 /**
  * @route crew/permissions/{string}/context/{id}
  *
  * @param string $cabin
  * @param string $contextId
  */
 public function editContext(string $cabin, string $contextId)
 {
     $contextId = (int) $contextId;
     if (!\in_array($cabin, $this->getCabinNamespaces())) {
         \Airship\redirect($this->airship_cabin_prefix . '/crew/permissions');
     }
     $context = $this->perms->getContext($contextId, $cabin);
     if (empty($context)) {
         \Airship\redirect($this->airship_cabin_prefix . '/crew/permissions' . $cabin);
     }
     // Handle post data
     $post = $this->post(new SaveContextFilter());
     if (!empty($post)) {
         if ($this->perms->saveContext($cabin, $contextId, $post)) {
             \Airship\redirect($this->airship_cabin_prefix . '/crew/permissions/' . $cabin . '/context/' . $contextId, ['msg' => 'saved']);
         }
     }
     // Okay,
     $actions = $this->perms->getActionNames($cabin);
     $groupPerms = $this->perms->buildGroupTree($cabin, $contextId, $actions);
     $userPerms = $this->perms->buildUserList($cabin, $contextId, $actions);
     $users = [];
     foreach ($userPerms as $userid => $userPerm) {
         $userid = (int) $userid;
         $users[$userid] = $this->users->getUserAccount($userid, true);
         unset($users[$userid]['password']);
     }
     if (!empty($_GET['msg'])) {
         if ($_GET['msg'] === 'saved') {
             $this->storeLensVar('message', \__('Your changes have been saved.'));
         }
     }
     $this->lens('perms/context', ['actions' => $actions, 'cabin' => $cabin, 'context' => $context, 'permissions' => $groupPerms, 'userperms' => $userPerms, 'users' => $users]);
 }