Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Commit

Permalink
ENH: refs #953. Refactor to create helper method to list permissions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Grauer committed Feb 22, 2013
1 parent bc420ab commit 2b0f454
Showing 1 changed file with 36 additions and 26 deletions.
62 changes: 36 additions & 26 deletions modules/api/controllers/components/ApiComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,41 @@ protected function _setFolderPrivacy($folder, $privacyCode)
}
}


/**
* helper function to return listing of permissions for a resource.
* @return A list with three keys: privacy, user, group; privacy will be the
resource's privacy string [Public|Private]; user will be a list of
(user_id, policy, email); group will be a list of (group_id, policy, name).
policy for user and group will be a policy string [Admin|Write|Read].
*/
protected function _listResourcePermissions($policyStatus, $userPolicies, $groupPolicies)
{
$privacyStrings = array(MIDAS_PRIVACY_PUBLIC => "Public", MIDAS_PRIVACY_PRIVATE => "Private");
$privilegeStrings = array(MIDAS_POLICY_ADMIN => "Admin", MIDAS_POLICY_WRITE => "Write", MIDAS_POLICY_READ => "Read");

$return = array('privacy' => $privacyStrings[$policyStatus]);

$userPoliciesOutput = array();
foreach($userPolicies as $userPolicy)
{
$user = $userPolicy->getUser();
$userPoliciesOutput[] = array('user_id' => $user->getUserId(), 'policy' => $privilegeStrings[$userPolicy->getPolicy()], 'email' => $user->getEmail());
}
$return['user'] = $userPoliciesOutput;

$groupPoliciesOutput = array();
foreach($groupPolicies as $groupPolicy)
{
$group = $groupPolicy->getGroup();
$groupPoliciesOutput[] = array('group_id' => $group->getGroupId(), 'policy' => $privilegeStrings[$groupPolicy->getPolicy()], 'name' => $group->getName());
}
$return['group'] = $groupPoliciesOutput;

return $return;
}


/**
* Create a folder or update an existing one if one exists by the uuid passed.
* If a folder is requested to be created with the same parentid and name as
Expand Down Expand Up @@ -1124,8 +1159,6 @@ public function folderListPermissions($args)
$userDao = $this->_getUser($args);

$folderpolicygroupModel = MidasLoader::loadModel('Folderpolicygroup');
$groupModel = MidasLoader::loadModel('Group');
$anonymousGroup = $groupModel->load(MIDAS_GROUP_ANONYMOUS_KEY);
$folderModel = MidasLoader::loadModel('Folder');
$folderId = $args['folder_id'];
$folder = $folderModel->load($folderId);
Expand All @@ -1139,30 +1172,7 @@ public function folderListPermissions($args)
throw new Exception("Admin privileges required on the folder to list permissions.", MIDAS_INVALID_POLICY);
}

$privacyStrings = array(MIDAS_PRIVACY_PUBLIC => "Public", MIDAS_PRIVACY_PRIVATE => "Private");
$privilegeStrings = array(MIDAS_POLICY_ADMIN => "Admin", MIDAS_POLICY_WRITE => "Write", MIDAS_POLICY_READ => "Read");

$return = array('privacy' => $privacyStrings[$folderpolicygroupModel->computePolicyStatus($folder)]);

$userPolicies = $folder->getFolderpolicyuser();
$userPoliciesOutput = array();
foreach($userPolicies as $userPolicy)
{
$user = $userPolicy->getUser();
$userPoliciesOutput[] = array('user_id' => $user->getUserId(), 'policy' => $privilegeStrings[$userPolicy->getPolicy()], 'email' => $user->getEmail());
}
$return['user'] = $userPoliciesOutput;

$groupPolicies = $folder->getFolderpolicygroup();
$groupPoliciesOutput = array();
foreach($groupPolicies as $groupPolicy)
{
$group = $groupPolicy->getGroup();
$groupPoliciesOutput[] = array('group_id' => $group->getGroupId(), 'policy' => $privilegeStrings[$groupPolicy->getPolicy()], 'name' => $group->getName());
}
$return['group'] = $groupPoliciesOutput;

return $return;
return $this->_listResourcePermissions($folderpolicygroupModel->computePolicyStatus($folder), $folder->getFolderpolicyuser(), $folder->getFolderpolicygroup());
}

/**
Expand Down

0 comments on commit 2b0f454

Please sign in to comment.