diff options
-rw-r--r-- | apps/provisioning_api/appinfo/routes.php | 4 | ||||
-rw-r--r-- | apps/provisioning_api/lib/groups.php | 55 | ||||
-rw-r--r-- | apps/provisioning_api/tests/groupstest.php | 75 |
3 files changed, 78 insertions, 56 deletions
diff --git a/apps/provisioning_api/appinfo/routes.php b/apps/provisioning_api/appinfo/routes.php index c25dba72263..1fe9ce73108 100644 --- a/apps/provisioning_api/appinfo/routes.php +++ b/apps/provisioning_api/appinfo/routes.php @@ -44,7 +44,9 @@ API::register('delete', '/cloud/users/{userid}/subadmins', [$users, 'removeSubAd API::register('get', '/cloud/users/{userid}/subadmins', [$users, 'getUserSubAdminGroups'], 'provisioning_api', API::ADMIN_AUTH); // Groups -$groups = new \OCA\Provisioning_API\Groups(); +$groups = new \OCA\Provisioning_API\Groups( + \OC::$server->getGroupManager() +); API::register('get', '/cloud/groups', [$groups, 'getGroups'], 'provisioning_api', API::SUBADMIN_AUTH); API::register('post', '/cloud/groups', [$groups, 'addGroup'], 'provisioning_api', API::SUBADMIN_AUTH); API::register('get', '/cloud/groups/{groupid}', [$groups, 'getGroup'], 'provisioning_api', API::SUBADMIN_AUTH); diff --git a/apps/provisioning_api/lib/groups.php b/apps/provisioning_api/lib/groups.php index 81a5a6e5c30..4151eaff940 100644 --- a/apps/provisioning_api/lib/groups.php +++ b/apps/provisioning_api/lib/groups.php @@ -24,33 +24,53 @@ namespace OCA\Provisioning_API; use \OC_OCS_Result; -use \OC_Group; use \OC_SubAdmin; class Groups{ + /** @var \OCP\IGroupManager */ + private $groupManager; + + /** + * @param \OCP\IGroupManager $groupManager + */ + public function __construct(\OCP\IGroupManager $groupManager) { + $this->groupManager = $groupManager; + } + /** * returns a list of groups */ - public static function getGroups($parameters){ + public function getGroups($parameters){ $search = !empty($_GET['search']) ? $_GET['search'] : ''; $limit = !empty($_GET['limit']) ? $_GET['limit'] : null; $offset = !empty($_GET['offset']) ? $_GET['offset'] : null; - return new OC_OCS_Result(array('groups' => OC_Group::getGroups($search, $limit, $offset))); + + $groups = $this->groupManager->search($search, $limit, $offset); + $groups = array_map(function($group) { + return $group->getGID(); + }, $groups); + + return new OC_OCS_Result(['groups' => $groups]); } /** * returns an array of users in the group specified */ - public static function getGroup($parameters){ + public function getGroup($parameters){ // Check the group exists - if(!OC_Group::groupExists($parameters['groupid'])){ + if(!$this->groupManager->groupExists($parameters['groupid'])){ return new OC_OCS_Result(null, \OCP\API::RESPOND_NOT_FOUND, 'The requested group could not be found'); } // Check subadmin has access to this group - if(\OC_User::isAdminUser(\OC_User::getUser()) + if($this->groupManager->isAdmin(\OC_User::getUser()) || in_array($parameters['groupid'], \OC_SubAdmin::getSubAdminsGroups(\OC_User::getUser()))){ - return new OC_OCS_Result(array('users' => OC_Group::usersInGroup($parameters['groupid']))); + $users = $this->groupManager->get($parameters['groupid'])->getUsers(); + $users = array_map(function($user) { + return $user->getUID(); + }, $users); + $users = array_values($users); + return new OC_OCS_Result(['users' => $users]); } else { return new OC_OCS_Result(null, \OCP\API::RESPOND_UNAUTHORISED, 'User does not have access to specified group'); } @@ -59,7 +79,7 @@ class Groups{ /** * creates a new group */ - public static function addGroup($parameters){ + public function addGroup($parameters){ // Validate name $groupid = isset($_POST['groupid']) ? $_POST['groupid'] : ''; if( preg_match( '/[^a-zA-Z0-9 _\.@\-]/', $groupid ) || empty($groupid)){ @@ -67,21 +87,18 @@ class Groups{ return new OC_OCS_Result(null, 101, 'Invalid group name'); } // Check if it exists - if(OC_Group::groupExists($groupid)){ + if($this->groupManager->groupExists($groupid)){ return new OC_OCS_Result(null, 102); } - if(OC_Group::createGroup($groupid)){ - return new OC_OCS_Result(null, 100); - } else { - return new OC_OCS_Result(null, 103); - } + $this->groupManager->createGroup($groupid); + return new OC_OCS_Result(null, 100); } - public static function deleteGroup($parameters){ + public function deleteGroup($parameters){ // Check it exists - if(!OC_Group::groupExists($parameters['groupid'])){ + if(!$this->groupManager->grouExists($parameters['groupid'])){ return new OC_OCS_Result(null, 101); - } else if($parameters['groupid'] == 'admin' || !OC_Group::deleteGroup($parameters['groupid'])){ + } else if($parameters['groupid'] === 'admin' || !$this->groupManger->get($parameters['groupid'])->delete()){ // Cannot delete admin group return new OC_OCS_Result(null, 102); } else { @@ -89,10 +106,10 @@ class Groups{ } } - public static function getSubAdminsOfGroup($parameters) { + public function getSubAdminsOfGroup($parameters) { $group = $parameters['groupid']; // Check group exists - if(!OC_Group::groupExists($group)) { + if(!$this->groupManager->groupExists($group)) { return new OC_OCS_Result(null, 101, 'Group does not exist'); } // Go diff --git a/apps/provisioning_api/tests/groupstest.php b/apps/provisioning_api/tests/groupstest.php index b8b02790698..5a757151bb4 100644 --- a/apps/provisioning_api/tests/groupstest.php +++ b/apps/provisioning_api/tests/groupstest.php @@ -25,17 +25,25 @@ namespace OCA\Provisioning_API\Tests; class GroupsTest extends TestCase { + + protected function setup() { + parent::setup(); + + $this->userManager = \OC::$server->getUserManager(); + $this->groupManager = \OC::$server->getGroupManager(); + $this->api = new \OCA\Provisioning_API\Groups($this->groupManager); + } + public function testGetGroupAsUser() { $users = $this->generateUsers(2); self::loginAsUser($users[0]); - $group = $this->getUniqueID(); - \OC_Group::createGroup($group); - \OC_Group::addToGroup($users[1], $group); + $group = $this->groupManager->createGroup($this->getUniqueID()); + $group->addUser($this->userManager->get($users[1])); - $result = \OCA\provisioning_api\Groups::getGroup(array( - 'groupid' => $group, + $result = $this->api->getGroup(array( + 'groupid' => $group->getGID(), )); $this->assertInstanceOf('OC_OCS_Result', $result); @@ -49,15 +57,14 @@ class GroupsTest extends TestCase { $users = $this->generateUsers(2); self::loginAsUser($users[0]); - $group = $this->getUniqueID(); - \OC_Group::createGroup($group); - \OC_Group::addToGroup($users[0], $group); - \OC_Group::addToGroup($users[1], $group); + $group = $this->groupManager->createGroup($this->getUniqueID()); + $group->addUser($this->userManager->get($users[0])); + $group->addUser($this->userManager->get($users[1])); - \OC_SubAdmin::createSubAdmin($users[0], $group); + \OC_SubAdmin::createSubAdmin($users[0], $group->getGID()); - $result = \OCA\provisioning_api\Groups::getGroup(array( - 'groupid' => $group, + $result = $this->api->getGroup(array( + 'groupid' => $group->getGID(), )); $this->assertInstanceOf('OC_OCS_Result', $result); @@ -78,17 +85,15 @@ class GroupsTest extends TestCase { $users = $this->generateUsers(2); self::loginAsUser($users[0]); - $group = $this->getUniqueID(); - \OC_Group::createGroup($group); - $group2 = $this->getUniqueID(); - \OC_Group::createGroup($group2); - \OC_Group::addToGroup($users[1], $group); - \OC_Group::addToGroup($users[0], $group2); + $group1 = $this->groupManager->createGroup($this->getUniqueID()); + $group2 = $this->groupManager->createGroup($this->getUniqueID()); + $group1->addUser($this->userManager->get($users[1])); + $group2->addUser($this->userManager->get($users[0])); - \OC_SubAdmin::createSubAdmin($users[0], $group2); + \OC_SubAdmin::createSubAdmin($users[0], $group2->getGID()); - $result = \OCA\provisioning_api\Groups::getGroup(array( - 'groupid' => $group, + $result = $this->api->getGroup(array( + 'groupid' => $group1->getGID(), )); $this->assertInstanceOf('OC_OCS_Result', $result); @@ -102,14 +107,13 @@ class GroupsTest extends TestCase { $users = $this->generateUsers(2); self::loginAsUser($users[0]); - $group = $this->getUniqueID(); - \OC_Group::createGroup($group); + $group = $this->groupManager->createGroup($this->getUniqueID()); - \OC_Group::addToGroup($users[1], $group); - \OC_Group::addToGroup($users[0], 'admin'); + $group->addUser($this->userManager->get($users[1])); + $this->groupManager->get('admin')->addUser($this->userManager->get($users[0])); - $result = \OCA\provisioning_api\Groups::getGroup(array( - 'groupid' => $group, + $result = $this->api->getGroup(array( + 'groupid' => $group->getGID(), )); $this->assertInstanceOf('OC_OCS_Result', $result); @@ -122,23 +126,22 @@ class GroupsTest extends TestCase { $user1 = $this->generateUsers(); $user2 = $this->generateUsers(); self::loginAsUser($user1); - \OC_Group::addToGroup($user1, 'admin'); - $group1 = $this->getUniqueID(); - \OC_Group::createGroup($group1); - \OC_SubAdmin::createSubAdmin($user2, $group1); - $result = \OCA\provisioning_api\Groups::getSubAdminsOfGroup(array( - 'groupid' => $group1, + $this->groupManager->get('admin')->addUser($this->userManager->get($user1)); + $group1 = $this->groupManager->createGroup($this->getUniqueID()); + \OC_SubAdmin::createSubAdmin($user2, $group1->getGID()); + $result = $this->api->getSubAdminsOfGroup(array( + 'groupid' => $group1->getGID(), )); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); $data = $result->getData(); $this->assertEquals($user2, reset($data)); - \OC_Group::deleteGroup($group1); + $group1->delete(); $user1 = $this->generateUsers(); self::loginAsUser($user1); - \OC_Group::addToGroup($user1, 'admin'); - $result = \OCA\provisioning_api\Groups::getSubAdminsOfGroup(array( + $this->groupManager->get('admin')->addUser($this->userManager->get($user1)); + $result = $this->api->getSubAdminsOfGroup(array( 'groupid' => $this->getUniqueID(), )); $this->assertInstanceOf('OC_OCS_Result', $result); |