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

Commit 3683bae

Browse files
author
Michael Grauer
committed
ENH: refs #953. added group.list.users and tests.
1 parent 1a91935 commit 3683bae

File tree

2 files changed

+129
-1
lines changed

2 files changed

+129
-1
lines changed

modules/api/controllers/components/ApiComponent.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2959,7 +2959,7 @@ function groupAddUser($args)
29592959
}
29602960

29612961
/**
2962-
* Remove a user to a group, returns 'success' => 'true' on success, requires
2962+
* Remove a user from a group, returns 'success' => 'true' on success, requires
29632963
* admin privileges on the community associated with the group.
29642964
* @param group_id the group to remove the user from
29652965
* @param user_id the user to remove from the group
@@ -3044,4 +3044,44 @@ function groupRemove($args)
30443044
return array('success' => 'true');
30453045
}
30463046

3047+
/**
3048+
* list the users for a group, requires admin privileges on the group
3049+
* @param group_id id of group
3050+
* @return array users => a list of user ids mapped to user emails
3051+
*/
3052+
function groupListUsers($args)
3053+
{
3054+
$this->_validateParams($args, array('group_id'));
3055+
3056+
$userDao = $this->_getUser($args);
3057+
if(!$userDao)
3058+
{
3059+
throw new Exception('You must be logged in to list users in a group', MIDAS_INVALID_POLICY);
3060+
}
3061+
3062+
$groupId = $args['group_id'];
3063+
$groupModel = MidasLoader::loadModel('Group');
3064+
$group = $groupModel->load($groupId);
3065+
if($group == false)
3066+
{
3067+
throw new Exception('This group does not exist', MIDAS_INVALID_PARAMETER);
3068+
}
3069+
3070+
$communityModel = MidasLoader::loadModel('Community');
3071+
if(!$communityModel->policyCheck($group->getCommunity(), $userDao, MIDAS_POLICY_ADMIN))
3072+
{
3073+
throw new Zend_Exception("Community Admin permissions required.", MIDAS_INVALID_POLICY);
3074+
}
3075+
3076+
$users = $group->getUsers();
3077+
$userIdsToEmail = array();
3078+
foreach($users as $user)
3079+
{
3080+
$userIdsToEmail[$user->getUserId()] = $user->getEmail();
3081+
}
3082+
return array('users' => $userIdsToEmail);
3083+
}
3084+
3085+
3086+
30473087
} // end class

modules/api/tests/controllers/ApiCallGroupMethodsTest.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,92 @@ public function testGroupAddRemove()
165165
$addedGroup = $groupModel->load($addedGroupId);
166166
$this->assertFalse($addedGroup, "group should have been removed but remains");
167167
}
168+
169+
/** Test adding and removing a group */
170+
public function testGroupListUsers()
171+
{
172+
$validCommunityId = 2001;
173+
$invalidCommunityId = -10;
174+
$commAdminGroupId = 3003;
175+
$invalidGroupId = -10;
176+
177+
$communityModel = MidasLoader::loadModel('Community');
178+
$comm2001 = $communityModel->load('2001');
179+
$userModel = MidasLoader::loadModel('User');
180+
$commMemberId = '4';
181+
$commModeratorId = '5';
182+
$commAdminId = '6';
183+
$commMember = $userModel->load($commMemberId);
184+
$commModerator = $userModel->load($commModeratorId);
185+
$commAdmin = $userModel->load($commAdminId);
186+
187+
// add in an anonymous user to non admins
188+
$invalidUsers = array($commMember, $commModerator, false);
189+
190+
// group list users
191+
192+
$groupListMethod = "midas.group.list.users";
193+
$requiredParams = array(
194+
array('name' => 'group_id', 'valid' => $commAdminGroupId, 'invalid' => $invalidGroupId));
195+
196+
$this->exerciseInvalidCases($groupListMethod, $commAdmin, $invalidUsers, $requiredParams);
197+
198+
$this->resetAll();
199+
$this->params['token'] = $this->_loginAsUser($commAdmin);
200+
$this->params['method'] = $groupListMethod;
201+
$this->params['group_id'] = $commAdminGroupId;
202+
$resp = $this->_callJsonApi();
203+
$this->_assertStatusOk($resp);
204+
205+
$users = $resp->data->users;
206+
$users = (array)$users;
207+
$this->assertEquals(1, sizeof($users), 'users should only have one entry');
208+
foreach($users as $id => $email)
209+
{
210+
$this->assertEquals($id, $commAdminId, 'users should have commAdminId as an entry');
211+
}
212+
213+
// add some users, test again
214+
215+
$groupModel = MidasLoader::loadModel('Group');
216+
$commAdminGroup = $groupModel->load($commAdminGroupId);
217+
$groupModel->addUser($commAdminGroup, $commMember);
218+
$groupModel->addUser($commAdminGroup, $commModerator);
219+
220+
$this->resetAll();
221+
$this->params['token'] = $this->_loginAsUser($commAdmin);
222+
$this->params['method'] = $groupListMethod;
223+
$this->params['group_id'] = $commAdminGroupId;
224+
$resp = $this->_callJsonApi();
225+
$this->_assertStatusOk($resp);
226+
$users = $resp->data->users;
227+
$users = (array)$users;
228+
$this->assertEquals(3, sizeof($users), 'users should have 3 entries');
229+
$members = array($commAdminId, $commMemberId, $commModeratorId);
230+
foreach($users as $id => $email)
231+
{
232+
$this->assertTrue(in_array($id, $members), 'users should have '.$id.' as an entry');
233+
}
234+
235+
// remove some users, test again
236+
$groupModel->removeUser($commAdminGroup, $commMember);
237+
$groupModel->removeUser($commAdminGroup, $commModerator);
238+
239+
$this->resetAll();
240+
$this->params['token'] = $this->_loginAsUser($commAdmin);
241+
$this->params['method'] = $groupListMethod;
242+
$this->params['group_id'] = $commAdminGroupId;
243+
$resp = $this->_callJsonApi();
244+
$this->_assertStatusOk($resp);
245+
246+
$users = $resp->data->users;
247+
$users = (array)$users;
248+
$this->assertEquals(1, sizeof($users), 'users should only have one entry');
249+
foreach($users as $id => $email)
250+
{
251+
$this->assertEquals($id, $commAdminId, 'users should have commAdminId as an entry');
252+
}
253+
254+
}
255+
168256
}

0 commit comments

Comments
 (0)