2014-12-22 13:54:50 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-06-25 09:43:55 +00:00
|
|
|
* @author Joas Schilling <nickvergessen@owncloud.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Tom Needham <tom@owncloud.com>
|
2014-12-22 13:54:50 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
2014-12-22 13:54:50 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
2014-12-22 13:54:50 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2014-12-22 13:54:50 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 10:44:34 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2014-12-22 13:54:50 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2014-12-22 13:54:50 +00:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
namespace OCA\Provisioning_API;
|
|
|
|
|
|
|
|
use \OC_OCS_Result;
|
|
|
|
use \OC_SubAdmin;
|
|
|
|
|
|
|
|
class Groups{
|
|
|
|
|
2015-07-25 12:32:49 +00:00
|
|
|
/** @var \OCP\IGroupManager */
|
|
|
|
private $groupManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \OCP\IGroupManager $groupManager
|
|
|
|
*/
|
|
|
|
public function __construct(\OCP\IGroupManager $groupManager) {
|
|
|
|
$this->groupManager = $groupManager;
|
|
|
|
}
|
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
/**
|
|
|
|
* returns a list of groups
|
|
|
|
*/
|
2015-07-25 12:32:49 +00:00
|
|
|
public function getGroups($parameters){
|
2014-12-22 13:54:50 +00:00
|
|
|
$search = !empty($_GET['search']) ? $_GET['search'] : '';
|
|
|
|
$limit = !empty($_GET['limit']) ? $_GET['limit'] : null;
|
|
|
|
$offset = !empty($_GET['offset']) ? $_GET['offset'] : null;
|
2015-07-25 12:32:49 +00:00
|
|
|
|
|
|
|
$groups = $this->groupManager->search($search, $limit, $offset);
|
|
|
|
$groups = array_map(function($group) {
|
|
|
|
return $group->getGID();
|
|
|
|
}, $groups);
|
|
|
|
|
|
|
|
return new OC_OCS_Result(['groups' => $groups]);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns an array of users in the group specified
|
|
|
|
*/
|
2015-07-25 12:32:49 +00:00
|
|
|
public function getGroup($parameters){
|
2014-12-22 13:54:50 +00:00
|
|
|
// Check the group exists
|
2015-07-25 12:32:49 +00:00
|
|
|
if(!$this->groupManager->groupExists($parameters['groupid'])){
|
2015-04-18 08:30:02 +00:00
|
|
|
return new OC_OCS_Result(null, \OCP\API::RESPOND_NOT_FOUND, 'The requested group could not be found');
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
// Check subadmin has access to this group
|
2015-07-25 12:32:49 +00:00
|
|
|
if($this->groupManager->isAdmin(\OC_User::getUser())
|
2014-12-22 13:54:50 +00:00
|
|
|
|| in_array($parameters['groupid'], \OC_SubAdmin::getSubAdminsGroups(\OC_User::getUser()))){
|
2015-07-25 12:32:49 +00:00
|
|
|
$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]);
|
2014-12-22 13:54:50 +00:00
|
|
|
} else {
|
2015-04-18 08:30:02 +00:00
|
|
|
return new OC_OCS_Result(null, \OCP\API::RESPOND_UNAUTHORISED, 'User does not have access to specified group');
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* creates a new group
|
|
|
|
*/
|
2015-07-25 12:32:49 +00:00
|
|
|
public function addGroup($parameters){
|
2014-12-22 13:54:50 +00:00
|
|
|
// Validate name
|
|
|
|
$groupid = isset($_POST['groupid']) ? $_POST['groupid'] : '';
|
|
|
|
if( preg_match( '/[^a-zA-Z0-9 _\.@\-]/', $groupid ) || empty($groupid)){
|
2015-04-09 10:36:10 +00:00
|
|
|
\OCP\Util::writeLog('provisioning_api', 'Attempt made to create group using invalid characters.', \OCP\Util::ERROR);
|
2014-12-22 13:54:50 +00:00
|
|
|
return new OC_OCS_Result(null, 101, 'Invalid group name');
|
|
|
|
}
|
|
|
|
// Check if it exists
|
2015-07-25 12:32:49 +00:00
|
|
|
if($this->groupManager->groupExists($groupid)){
|
2014-12-22 13:54:50 +00:00
|
|
|
return new OC_OCS_Result(null, 102);
|
|
|
|
}
|
2015-07-25 12:32:49 +00:00
|
|
|
$this->groupManager->createGroup($groupid);
|
|
|
|
return new OC_OCS_Result(null, 100);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
|
2015-07-25 12:32:49 +00:00
|
|
|
public function deleteGroup($parameters){
|
2014-12-22 13:54:50 +00:00
|
|
|
// Check it exists
|
2015-07-25 12:32:49 +00:00
|
|
|
if(!$this->groupManager->grouExists($parameters['groupid'])){
|
2014-12-22 13:54:50 +00:00
|
|
|
return new OC_OCS_Result(null, 101);
|
2015-07-25 12:32:49 +00:00
|
|
|
} else if($parameters['groupid'] === 'admin' || !$this->groupManger->get($parameters['groupid'])->delete()){
|
2014-12-22 13:54:50 +00:00
|
|
|
// Cannot delete admin group
|
|
|
|
return new OC_OCS_Result(null, 102);
|
|
|
|
} else {
|
|
|
|
return new OC_OCS_Result(null, 100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-25 12:32:49 +00:00
|
|
|
public function getSubAdminsOfGroup($parameters) {
|
2014-12-22 13:54:50 +00:00
|
|
|
$group = $parameters['groupid'];
|
|
|
|
// Check group exists
|
2015-07-25 12:32:49 +00:00
|
|
|
if(!$this->groupManager->groupExists($group)) {
|
2014-12-22 13:54:50 +00:00
|
|
|
return new OC_OCS_Result(null, 101, 'Group does not exist');
|
|
|
|
}
|
|
|
|
// Go
|
|
|
|
if(!$subadmins = OC_Subadmin::getGroupsSubAdmins($group)) {
|
|
|
|
return new OC_OCS_Result(null, 102, 'Unknown error occured');
|
|
|
|
} else {
|
|
|
|
return new OC_OCS_Result($subadmins);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|