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

Commit 2b0f454

Browse files
author
Michael Grauer
committed
ENH: refs #953. Refactor to create helper method to list permissions.
1 parent bc420ab commit 2b0f454

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

modules/api/controllers/components/ApiComponent.php

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,41 @@ protected function _setFolderPrivacy($folder, $privacyCode)
836836
}
837837
}
838838

839+
840+
/**
841+
* helper function to return listing of permissions for a resource.
842+
* @return A list with three keys: privacy, user, group; privacy will be the
843+
resource's privacy string [Public|Private]; user will be a list of
844+
(user_id, policy, email); group will be a list of (group_id, policy, name).
845+
policy for user and group will be a policy string [Admin|Write|Read].
846+
*/
847+
protected function _listResourcePermissions($policyStatus, $userPolicies, $groupPolicies)
848+
{
849+
$privacyStrings = array(MIDAS_PRIVACY_PUBLIC => "Public", MIDAS_PRIVACY_PRIVATE => "Private");
850+
$privilegeStrings = array(MIDAS_POLICY_ADMIN => "Admin", MIDAS_POLICY_WRITE => "Write", MIDAS_POLICY_READ => "Read");
851+
852+
$return = array('privacy' => $privacyStrings[$policyStatus]);
853+
854+
$userPoliciesOutput = array();
855+
foreach($userPolicies as $userPolicy)
856+
{
857+
$user = $userPolicy->getUser();
858+
$userPoliciesOutput[] = array('user_id' => $user->getUserId(), 'policy' => $privilegeStrings[$userPolicy->getPolicy()], 'email' => $user->getEmail());
859+
}
860+
$return['user'] = $userPoliciesOutput;
861+
862+
$groupPoliciesOutput = array();
863+
foreach($groupPolicies as $groupPolicy)
864+
{
865+
$group = $groupPolicy->getGroup();
866+
$groupPoliciesOutput[] = array('group_id' => $group->getGroupId(), 'policy' => $privilegeStrings[$groupPolicy->getPolicy()], 'name' => $group->getName());
867+
}
868+
$return['group'] = $groupPoliciesOutput;
869+
870+
return $return;
871+
}
872+
873+
839874
/**
840875
* Create a folder or update an existing one if one exists by the uuid passed.
841876
* If a folder is requested to be created with the same parentid and name as
@@ -1124,8 +1159,6 @@ public function folderListPermissions($args)
11241159
$userDao = $this->_getUser($args);
11251160

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

1142-
$privacyStrings = array(MIDAS_PRIVACY_PUBLIC => "Public", MIDAS_PRIVACY_PRIVATE => "Private");
1143-
$privilegeStrings = array(MIDAS_POLICY_ADMIN => "Admin", MIDAS_POLICY_WRITE => "Write", MIDAS_POLICY_READ => "Read");
1144-
1145-
$return = array('privacy' => $privacyStrings[$folderpolicygroupModel->computePolicyStatus($folder)]);
1146-
1147-
$userPolicies = $folder->getFolderpolicyuser();
1148-
$userPoliciesOutput = array();
1149-
foreach($userPolicies as $userPolicy)
1150-
{
1151-
$user = $userPolicy->getUser();
1152-
$userPoliciesOutput[] = array('user_id' => $user->getUserId(), 'policy' => $privilegeStrings[$userPolicy->getPolicy()], 'email' => $user->getEmail());
1153-
}
1154-
$return['user'] = $userPoliciesOutput;
1155-
1156-
$groupPolicies = $folder->getFolderpolicygroup();
1157-
$groupPoliciesOutput = array();
1158-
foreach($groupPolicies as $groupPolicy)
1159-
{
1160-
$group = $groupPolicy->getGroup();
1161-
$groupPoliciesOutput[] = array('group_id' => $group->getGroupId(), 'policy' => $privilegeStrings[$groupPolicy->getPolicy()], 'name' => $group->getName());
1162-
}
1163-
$return['group'] = $groupPoliciesOutput;
1164-
1165-
return $return;
1175+
return $this->_listResourcePermissions($folderpolicygroupModel->computePolicyStatus($folder), $folder->getFolderpolicyuser(), $folder->getFolderpolicygroup());
11661176
}
11671177

11681178
/**

0 commit comments

Comments
 (0)