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

Commit 59f7a16

Browse files
author
Michael Grauer
committed
ENH: refs #953. added community.list.groups and tests.
1 parent 3683bae commit 59f7a16

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

modules/api/controllers/components/ApiComponent.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3045,7 +3045,8 @@ function groupRemove($args)
30453045
}
30463046

30473047
/**
3048-
* list the users for a group, requires admin privileges on the group
3048+
* list the users for a group, requires admin privileges on the community
3049+
* assiated with the group
30493050
* @param group_id id of group
30503051
* @return array users => a list of user ids mapped to user emails
30513052
*/
@@ -3082,6 +3083,41 @@ function groupListUsers($args)
30823083
return array('users' => $userIdsToEmail);
30833084
}
30843085

3086+
/**
3087+
* list the groups for a community, requires admin privileges on the community
3088+
* @param community_id id of community
3089+
* @return array groups => a list of group ids mapped to group names
3090+
*/
3091+
function communityListGroups($args)
3092+
{
3093+
$this->_validateParams($args, array('community_id'));
3094+
3095+
$userDao = $this->_getUser($args);
3096+
if(!$userDao)
3097+
{
3098+
throw new Exception('You must be logged in to list groups in a community', MIDAS_INVALID_POLICY);
3099+
}
3100+
3101+
$communityId = $args['community_id'];
3102+
$communityModel = MidasLoader::loadModel('Community');
3103+
$community = $communityModel->load($communityId);
3104+
if(!$community)
3105+
{
3106+
throw new Exception('Invalid community_id', MIDAS_INVALID_PARAMETER);
3107+
}
3108+
if(!$communityModel->policyCheck($community, $userDao, MIDAS_POLICY_ADMIN))
3109+
{
3110+
throw new Zend_Exception("Community Admin permissions required.", MIDAS_INVALID_POLICY);
3111+
}
3112+
3113+
$groups = $community->getGroups();
3114+
$groupIdsToName = array();
3115+
foreach($groups as $group)
3116+
{
3117+
$groupIdsToName[$group->getGroupId()] = $group->getName();
3118+
}
3119+
return array('groups' => $groupIdsToName);
3120+
}
30853121

30863122

30873123
} // end class

modules/api/tests/controllers/ApiCallCommunityMethodsTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,48 @@ public function testCommunityGet()
275275
$this->assertEquals($resp->data->community_id, '2000');
276276
}
277277

278+
/** Test listing the groups in a community */
279+
public function testCommunityListGroups()
280+
{
281+
$validCommunityId = 2001;
282+
$invalidCommunityId = -10;
283+
284+
$communityModel = MidasLoader::loadModel('Community');
285+
$comm2001 = $communityModel->load($validCommunityId);
286+
$userModel = MidasLoader::loadModel('User');
287+
$commMemberId = '4';
288+
$commModeratorId = '5';
289+
$commAdminId = '6';
290+
$commMember = $userModel->load($commMemberId);
291+
$commModerator = $userModel->load($commModeratorId);
292+
$commAdmin = $userModel->load($commAdminId);
293+
294+
// add in an anonymous user to non admins
295+
$invalidUsers = array($commMember, $commModerator, false);
296+
297+
// community list groups
298+
299+
$communityListMethod = "midas.community.list.groups";
300+
$requiredParams = array(
301+
array('name' => 'community_id', 'valid' => $validCommunityId, 'invalid' => $invalidCommunityId));
302+
303+
$this->exerciseInvalidCases($communityListMethod, $commAdmin, $invalidUsers, $requiredParams);
304+
305+
$this->resetAll();
306+
$this->params['token'] = $this->_loginAsUser($commAdmin);
307+
$this->params['method'] = $communityListMethod;
308+
$this->params['community_id'] = $validCommunityId;
309+
$resp = $this->_callJsonApi();
310+
$this->_assertStatusOk($resp);
311+
$groups = (array)$resp->data->groups;
312+
$this->assertEquals(3, sizeof($groups), 'groups should have 3 entries');
313+
314+
$expectedGroups = array('3003', '3004', '3005');
315+
foreach($groups as $id => $name)
316+
{
317+
$this->assertTrue(in_array($id, $expectedGroups), 'id should have been in expectedGroups');
318+
}
319+
}
320+
321+
278322
}

modules/api/tests/controllers/ApiCallGroupMethodsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public function testGroupAddRemove()
166166
$this->assertFalse($addedGroup, "group should have been removed but remains");
167167
}
168168

169-
/** Test adding and removing a group */
169+
/** Test listing the users in a group */
170170
public function testGroupListUsers()
171171
{
172172
$validCommunityId = 2001;

0 commit comments

Comments
 (0)