Completed
Push — master ( 0e72e3...e806a9 )
by Individual IT
13:06 queued 03:04
created

Groups   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 152
rs 10
c 0
b 0
f 0
wmc 22
lcom 1
cbo 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getGroups() 0 20 3
B getGroup() 0 34 6
A deleteGroup() 0 11 4
A getSubAdminsOfGroup() 0 17 3
B addGroup() 0 14 5
1
<?php
2
/**
3
 * @author Joas Schilling <[email protected]>
4
 * @author Lukas Reschke <[email protected]>
5
 * @author Morris Jobke <[email protected]>
6
 * @author Roeland Jago Douma <[email protected]>
7
 * @author Tom Needham <[email protected]>
8
 *
9
 * @copyright Copyright (c) 2017, ownCloud GmbH
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
26
namespace OCA\Provisioning_API;
27
28
use \OC_OCS_Result;
29
use OCP\IGroup;
30
use OCP\IUser;
31
32
class Groups{
33
34
	/** @var \OCP\IGroupManager */
35
	private $groupManager;
36
37
	/** @var \OCP\IUserSession */
38
	private $userSession;
39
40
	/** @var \OCP\IRequest */
41
	private $request;
42
43
	/**
44
	 * @param \OCP\IGroupManager $groupManager
45
	 * @param \OCP\IUserSession $userSession
46
	 * @param \OCP\IRequest $request
47
	 */
48
	public function __construct(\OCP\IGroupManager $groupManager,
49
								\OCP\IUserSession $userSession,
50
								\OCP\IRequest $request) {
51
		$this->groupManager = $groupManager;
52
		$this->userSession = $userSession;
53
		$this->request = $request;
54
	}
55
56
	/**
57
	 * returns a list of groups
58
	 *
59
	 * @param array $parameters
60
	 * @return OC_OCS_Result
61
	 */
62
	public function getGroups($parameters) {
0 ignored issues
show
Unused Code introduced by
The parameter $parameters is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
		$search = $this->request->getParam('search', '');
64
		$limit = $this->request->getParam('limit');
65
		$offset = $this->request->getParam('offset');
66
67
		if ($limit !== null) {
68
			$limit = (int)$limit;
69
		}
70
		if ($offset !== null) {
71
			$offset = (int)$offset;
72
		}
73
74
		$groups = $this->groupManager->search($search, $limit, $offset, 'management');
75
		$groups = array_map(function($group) {
76
			/** @var IGroup $group */
77
			return $group->getGID();
78
		}, $groups);
79
80
		return new OC_OCS_Result(['groups' => $groups]);
81
	}
82
83
	/**
84
	 * returns an array of users in the group specified
85
	 *
86
	 * @param array $parameters
87
	 * @return OC_OCS_Result
88
	 */
89
	public function getGroup($parameters) {
90
		// Check if user is logged in
91
		$user = $this->userSession->getUser();
92
		if ($user === null) {
93
			return new OC_OCS_Result(null, \OCP\API::RESPOND_UNAUTHORISED);
94
		}
95
96
		$groupId = $parameters['groupid'];
97
98
		// Check the group exists
99
		if(!$this->groupManager->groupExists($groupId)) {
100
			return new OC_OCS_Result(null, \OCP\API::RESPOND_NOT_FOUND, 'The requested group could not be found');
101
		}
102
103
		$isSubadminOfGroup = false;
104
		$group = $this->groupManager->get($groupId);
105
		if ($group !== null) {
106
			$isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminofGroup($user, $group);
107
		}
108
109
		// Check subadmin has access to this group
110
		if($this->groupManager->isAdmin($user->getUID())
111
		   || $isSubadminOfGroup) {
112
			$users = $this->groupManager->get($groupId)->getUsers();
113
			$users =  array_map(function($user) {
114
				/** @var IUser $user */
115
				return $user->getUID();
116
			}, $users);
117
			$users = array_values($users);
118
			return new OC_OCS_Result(['users' => $users]);
119
		} else {
120
			return new OC_OCS_Result(null, \OCP\API::RESPOND_UNAUTHORISED, 'User does not have access to specified group');
121
		}
122
	}
123
124
	/**
125
	 * creates a new group
126
	 *
127
	 * @param array $parameters
128
	 * @return OC_OCS_Result
129
	 */
130
	public function addGroup($parameters) {
0 ignored issues
show
Unused Code introduced by
The parameter $parameters is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
		// Validate name
132
		$groupId = $this->request->getParam('groupid', '');
133
		if(($groupId === '') || is_null($groupId) || ($groupId === false)){
134
			\OCP\Util::writeLog('provisioning_api', 'Group name not supplied', \OCP\Util::ERROR);
135
			return new OC_OCS_Result(null, 101, 'Invalid group name');
136
		}
137
		// Check if it exists
138
		if($this->groupManager->groupExists($groupId)){
139
			return new OC_OCS_Result(null, 102);
140
		}
141
		$this->groupManager->createGroup($groupId);
142
		return new OC_OCS_Result(null, 100);
143
	}
144
145
	/**
146
	 * @param array $parameters
147
	 * @return OC_OCS_Result
148
	 */
149
	public function deleteGroup($parameters) {
150
		// Check it exists
151
		if(!$this->groupManager->groupExists($parameters['groupid'])){
152
			return new OC_OCS_Result(null, 101);
153
		} else if($parameters['groupid'] === 'admin' || !$this->groupManager->get($parameters['groupid'])->delete()){
154
			// Cannot delete admin group
155
			return new OC_OCS_Result(null, 102);
156
		} else {
157
			return new OC_OCS_Result(null, 100);
158
		}
159
	}
160
161
	/**
162
	 * @param array $parameters
163
	 * @return OC_OCS_Result
164
	 */
165
	public function getSubAdminsOfGroup($parameters) {
166
		$group = $parameters['groupid'];
167
		// Check group exists
168
		$targetGroup = $this->groupManager->get($group);
169
		if($targetGroup === null) {
170
			return new OC_OCS_Result(null, 101, 'Group does not exist');
171
		}
172
173
		$subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup);
174
		// New class returns IUser[] so convert back
175
		$uids = [];
176
		foreach ($subadmins as $user) {
177
			$uids[] = $user->getUID();
178
		}
179
180
		return new OC_OCS_Result($uids);
181
	}
182
183
}
184