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

Commit 63baf30

Browse files
author
Michael Grauer
committed
ENH: refs #953. Added group.add.user, group.remove.user and tests.
1 parent 222c83e commit 63baf30

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed

modules/api/controllers/components/ApiComponent.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2902,4 +2902,72 @@ function metadataQualifiersList($args)
29022902
return $metadataModel->getMetaDataQualifiers($type, $element);
29032903
}
29042904

2905+
/**
2906+
* helper function to validate args of methods for adding or removing
2907+
* users from groups.
2908+
* @param type $args
2909+
* @return type
2910+
*/
2911+
protected function _validateGroupUserChangeParams($args)
2912+
{
2913+
$this->_validateParams($args, array('group_id', 'user_id'));
2914+
2915+
$userDao = $this->_getUser($args);
2916+
if(!$userDao)
2917+
{
2918+
throw new Exception('You must be logged in to add a user to a group', MIDAS_INVALID_POLICY);
2919+
}
2920+
2921+
$groupId = $args['group_id'];
2922+
$groupModel = MidasLoader::loadModel('Group');
2923+
$group = $groupModel->load($groupId);
2924+
if($group == false)
2925+
{
2926+
throw new Exception('This group does not exist', MIDAS_INVALID_PARAMETER);
2927+
}
2928+
2929+
$communityModel = MidasLoader::loadModel('Community');
2930+
if(!$communityModel->policyCheck($group->getCommunity(), $userDao, MIDAS_POLICY_ADMIN))
2931+
{
2932+
throw new Zend_Exception("Community Admin permissions required.", MIDAS_INVALID_POLICY);
2933+
}
2934+
2935+
$groupUserId = $args['user_id'];
2936+
$userModel = MidasLoader::loadModel('User');
2937+
$groupUser = $userModel->load($groupUserId);
2938+
if($groupUser == false)
2939+
{
2940+
throw new Exception('This user does not exist', MIDAS_INVALID_PARAMETER);
2941+
}
2942+
2943+
return array($groupModel, $group, $groupUser);
2944+
}
2945+
2946+
/**
2947+
* Add a user to a group, returns 'success' => 'true' on success, requires
2948+
* admin privileges on the community associated with the group.
2949+
* @param group_id the group to add the user to
2950+
* @param user_id the user to add to the group
2951+
*/
2952+
function groupAddUser($args)
2953+
{
2954+
list($groupModel, $group, $addedUser) = $this->_validateGroupUserChangeParams($args);
2955+
$groupModel->addUser($group, $addedUser);
2956+
return array('success' => 'true');
2957+
}
2958+
2959+
/**
2960+
* Remove a user to a group, returns 'success' => 'true' on success, requires
2961+
* admin privileges on the community associated with the group.
2962+
* @param group_id the group to remove the user from
2963+
* @param user_id the user to remove from the group
2964+
*/
2965+
function groupRemoveUser($args)
2966+
{
2967+
list($groupModel, $group, $removedUser) = $this->_validateGroupUserChangeParams($args);
2968+
$groupModel->removeUser($group, $removedUser);
2969+
return array('success' => 'true');
2970+
}
2971+
2972+
29052973
} // end class
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
5+
All rights reserved.
6+
More information http://www.kitware.com
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0.txt
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
=========================================================================*/
20+
require_once BASE_PATH . '/modules/api/tests/controllers/ApiCallMethodsTest.php';
21+
/** Tests the functionality of the web API Group methods */
22+
class ApiCallGroupMethodsTest extends ApiCallMethodsTest
23+
{
24+
/** set up tests */
25+
public function setUp()
26+
{
27+
parent::setUp();
28+
}
29+
30+
/** Test adding and removing a user from a group */
31+
public function testGroupUserAddRemove()
32+
{
33+
$addMethod = "midas.group.add.user";
34+
$removeMethod = "midas.group.remove.user";
35+
$methods = array($addMethod, $removeMethod);
36+
37+
$communityModel = MidasLoader::loadModel('Community');
38+
$comm2001 = $communityModel->load('2001');
39+
$userModel = MidasLoader::loadModel('User');
40+
$commMember = $userModel->load('4');
41+
$commModerator = $userModel->load('5');
42+
$commAdmin = $userModel->load('6');
43+
$nonModerators = array($commMember);
44+
$nonAdmins = array($commMember, $commModerator);
45+
$moderators = array($commModerator, $commAdmin);
46+
47+
$validGroupId = '3004';
48+
$invalidGroupId = '-10';
49+
$validUserId = '2';
50+
$invalidUserId = '-10';
51+
52+
// test all the failure cases
53+
foreach($methods as $method)
54+
{
55+
// Try anonymously first
56+
$this->resetAll();
57+
$this->params['method'] = $method;
58+
$this->params['group_id'] = $validGroupId;
59+
$this->params['user_id'] = $validUserId;
60+
$resp = $this->_callJsonApi();
61+
$this->_assertStatusFail($resp, MIDAS_INVALID_POLICY);
62+
63+
// an invalid group
64+
$this->resetAll();
65+
$this->params['token'] = $this->_loginAsUser($commAdmin);
66+
$this->params['method'] = $method;
67+
$this->params['group_id'] = $invalidGroupId;
68+
$this->params['user_id'] = $validUserId;
69+
$resp = $this->_callJsonApi();
70+
$this->_assertStatusFail($resp, MIDAS_INVALID_PARAMETER);
71+
72+
// an invalid user
73+
$this->resetAll();
74+
$this->params['token'] = $this->_loginAsUser($commAdmin);
75+
$this->params['method'] = $method;
76+
$this->params['group_id'] = $validGroupId;
77+
$this->params['user_id'] = $invalidUserId;
78+
$resp = $this->_callJsonApi();
79+
$this->_assertStatusFail($resp, MIDAS_INVALID_PARAMETER);
80+
81+
// as a non admin
82+
foreach($nonAdmins as $nonAdmin)
83+
{
84+
$this->resetAll();
85+
$this->params['token'] = $this->_loginAsUser($nonAdmin);
86+
$this->params['method'] = $method;
87+
$this->params['group_id'] = $validGroupId;
88+
$this->params['user_id'] = $validUserId;
89+
$resp = $this->_callJsonApi();
90+
$this->_assertStatusFail($resp, MIDAS_INVALID_POLICY);
91+
}
92+
}
93+
94+
// ensure the user isn't already in the group
95+
$groupModel = MidasLoader::loadModel('Group');
96+
$changedUser = $userModel->load($validUserId);
97+
$group = $groupModel->load($validGroupId);
98+
$this->assertFalse($groupModel->userInGroup($changedUser, $group), "This user is not expected to be in the group");
99+
100+
// add the user to the group
101+
$this->resetAll();
102+
$this->params['token'] = $this->_loginAsUser($commAdmin);
103+
$this->params['method'] = $addMethod;
104+
$this->params['group_id'] = $validGroupId;
105+
$this->params['user_id'] = $validUserId;
106+
$resp = $this->_callJsonApi();
107+
$this->_assertStatusOk($resp);
108+
109+
// ensure the user is now in the group
110+
$this->assertTrue($groupModel->userInGroup($changedUser, $group), "This user is expected to be in the group");
111+
112+
// remove the user from the group
113+
$this->resetAll();
114+
$this->params['token'] = $this->_loginAsUser($commAdmin);
115+
$this->params['method'] = $removeMethod;
116+
$this->params['group_id'] = $validGroupId;
117+
$this->params['user_id'] = $validUserId;
118+
$resp = $this->_callJsonApi();
119+
$this->_assertStatusOk($resp);
120+
121+
$this->assertFalse($groupModel->userInGroup($changedUser, $group), "This user is not expected to be in the group");
122+
}
123+
124+
125+
}

modules/api/tests/controllers/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ add_midas_test( ApiIndexControllerTest ApiIndexControllerTest.php )
33
add_midas_test( ApiCallUserMethodsTest ApiCallUserMethodsTest.php )
44
add_midas_test( ApiCallItemMethodsTest ApiCallItemMethodsTest.php )
55
add_midas_test( ApiCallFolderMethodsTest ApiCallFolderMethodsTest.php )
6+
add_midas_test( ApiCallGroupMethodsTest ApiCallGroupMethodsTest.php )
67
add_midas_test( ApiCallCommunityMethodsTest ApiCallCommunityMethodsTest.php )
78
add_midas_test( ApiCallMiscMethodsTest ApiCallMiscMethodsTest.php )

0 commit comments

Comments
 (0)