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

saveContext() public method

Saves a permission context. This affects the context itself as well as the whitelist.
public saveContext ( string $cabin, integer $contextId, array $post ) : boolean
$cabin string Which Cabin?
$contextId integer Which context?
$post array POST data
return boolean
    public function saveContext(string $cabin, int $contextId, array $post) : bool
    {
        $actions = $this->getActionNames($cabin);
        $actionIds = \array_flip($actions);
        $post['group_perms'] = $this->permBoolean($post['group_perms'] ?? [], $actions);
        $post['user_perms'] = $this->permBoolean($post['user_perms'] ?? [], $actions);
        $groupPerms = $this->getGroupRulesForContextSave($actions, $contextId);
        $userPerms = $this->getUserRulesForContextSave($actions, $contextId);
        // Sort then diff group permissions:
        \ksort($groupPerms);
        \ksort($post['group_perms']);
        $groupInsert = \Airship\array_multi_diff($post['group_perms'], $groupPerms);
        $groupDelete = \Airship\array_multi_diff($groupPerms, $post['group_perms']);
        // Sort then diff user permissions:
        \ksort($userPerms);
        \ksort($post['user_perms']);
        $userInsert = \Airship\array_multi_diff($post['user_perms'], $userPerms);
        $userDelete = \Airship\array_multi_diff($userPerms, $post['user_perms']);
        $this->db->beginTransaction();
        // Update the locator
        $this->db->update('airship_perm_contexts', ['locator' => $post['context']], ['cabin' => $cabin, 'contextid' => $contextId]);
        // Insert then delete rules based on the changes made:
        // Insert new group rules:
        foreach ($groupInsert as $group => $inserts) {
            foreach ($inserts as $lbl => $val) {
                if ($val) {
                    $this->db->insert('airship_perm_rules', ['context' => $contextId, 'groupid' => $group, 'action' => $actionIds[$lbl]]);
                }
            }
        }
        // Delete old group rules:
        foreach ($groupDelete as $group => $deletes) {
            foreach ($deletes as $lbl => $val) {
                if ($val) {
                    $this->db->delete('airship_perm_rules', ['context' => $contextId, 'groupid' => $group, 'action' => $actionIds[$lbl]]);
                }
            }
        }
        // Insert new user rules:
        foreach ($userInsert as $user => $inserts) {
            foreach ($inserts as $lbl => $val) {
                if ($val) {
                    $this->db->insert('airship_perm_rules', ['context' => $contextId, 'userid' => $user, 'action' => $actionIds[$lbl]]);
                }
            }
        }
        // Delete old user rules:
        foreach ($userDelete as $user => $deletes) {
            foreach ($deletes as $lbl => $val) {
                if ($val) {
                    $this->db->delete('airship_perm_rules', ['context' => $contextId, 'userid' => $user, 'action' => $actionIds[$lbl]]);
                }
            }
        }
        return $this->db->commit();
    }

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]);
 }