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) 2018, 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
|
|
|
* unencode any encoded "/" or "%" that are intended to be literally part |
58
|
|
|
* of the group name. |
59
|
|
|
* |
60
|
|
|
* @param string $groupId |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
private function unencodeGroupId($groupId) { |
64
|
|
|
return \strtr($groupId, ['%25' => '%', '%2F' => '/']); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* returns a list of groups |
69
|
|
|
* |
70
|
|
|
* @param array $parameters |
71
|
|
|
* @return OC_OCS_Result |
72
|
|
|
*/ |
73
|
|
|
public function getGroups($parameters) { |
|
|
|
|
74
|
|
|
$search = $this->request->getParam('search', ''); |
75
|
|
|
$limit = $this->request->getParam('limit'); |
76
|
|
|
$offset = $this->request->getParam('offset'); |
77
|
|
|
|
78
|
|
|
if ($limit !== null) { |
79
|
|
|
$limit = (int)$limit; |
80
|
|
|
} |
81
|
|
|
if ($offset !== null) { |
82
|
|
|
$offset = (int)$offset; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$groups = $this->groupManager->search($search, $limit, $offset, 'management'); |
86
|
|
|
$groups = \array_map(function ($group) { |
87
|
|
|
/** @var IGroup $group */ |
88
|
|
|
return $group->getGID(); |
89
|
|
|
}, $groups); |
90
|
|
|
|
91
|
|
|
return new OC_OCS_Result(['groups' => $groups]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* returns an array of users in the group specified |
96
|
|
|
* |
97
|
|
|
* @param array $parameters |
98
|
|
|
* @return OC_OCS_Result |
99
|
|
|
*/ |
100
|
|
|
public function getGroup($parameters) { |
101
|
|
|
// Check if user is logged in |
102
|
|
|
$user = $this->userSession->getUser(); |
103
|
|
|
if ($user === null) { |
104
|
|
|
return new OC_OCS_Result(null, \OCP\API::RESPOND_UNAUTHORISED); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$groupId = $this->unencodeGroupId($parameters['groupid']); |
108
|
|
|
|
109
|
|
|
// Check the group exists |
110
|
|
|
if (!$this->groupManager->groupExists($groupId)) { |
111
|
|
|
return new OC_OCS_Result(null, \OCP\API::RESPOND_NOT_FOUND, 'The requested group could not be found'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$isSubadminOfGroup = false; |
115
|
|
|
$group = $this->groupManager->get($groupId); |
116
|
|
|
if ($group !== null) { |
117
|
|
|
$isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminofGroup($user, $group); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// Check subadmin has access to this group |
121
|
|
|
if ($this->groupManager->isAdmin($user->getUID()) |
122
|
|
|
|| $isSubadminOfGroup) { |
123
|
|
|
$users = $this->groupManager->get($groupId)->getUsers(); |
124
|
|
|
$users = \array_map(function ($user) { |
125
|
|
|
/** @var IUser $user */ |
126
|
|
|
return $user->getUID(); |
127
|
|
|
}, $users); |
128
|
|
|
$users = \array_values($users); |
129
|
|
|
return new OC_OCS_Result(['users' => $users]); |
130
|
|
|
} |
131
|
|
|
return new OC_OCS_Result(null, \OCP\API::RESPOND_UNAUTHORISED, 'User does not have access to specified group'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* creates a new group |
136
|
|
|
* |
137
|
|
|
* @param array $parameters |
138
|
|
|
* @return OC_OCS_Result |
139
|
|
|
*/ |
140
|
|
|
public function addGroup($parameters) { |
|
|
|
|
141
|
|
|
// Validate name |
142
|
|
|
$groupId = $this->request->getParam('groupid', ''); |
143
|
|
|
if (($groupId === '') || $groupId === null || ($groupId === false)) { |
144
|
|
|
\OCP\Util::writeLog('provisioning_api', 'Group name not supplied', \OCP\Util::ERROR); |
145
|
|
|
return new OC_OCS_Result(null, 101, 'Invalid group name'); |
146
|
|
|
} |
147
|
|
|
// Check if it exists |
148
|
|
|
if ($this->groupManager->groupExists($groupId)) { |
149
|
|
|
return new OC_OCS_Result(null, 102); |
150
|
|
|
} |
151
|
|
|
$user = $this->userSession->getUser(); |
152
|
|
|
if ($user === null) { |
153
|
|
|
return new OC_OCS_Result(null, 102); |
154
|
|
|
} |
155
|
|
|
// Only admin has got privilege to create group |
156
|
|
|
if ($this->groupManager->isAdmin($user->getUID())) { |
157
|
|
|
$this->groupManager->createGroup($groupId); |
158
|
|
|
return new OC_OCS_Result(null, 100); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return new OC_OCS_Result(null, 997); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param array $parameters |
166
|
|
|
* @return OC_OCS_Result |
167
|
|
|
*/ |
168
|
|
|
public function deleteGroup($parameters) { |
169
|
|
|
$groupId = $this->unencodeGroupId($parameters['groupid']); |
170
|
|
|
// Check it exists |
171
|
|
|
if (!$this->groupManager->groupExists($groupId)) { |
172
|
|
|
return new OC_OCS_Result(null, 101); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
if ($groupId === 'admin' || !$this->groupManager->get($groupId)->delete()) { |
176
|
|
|
// Cannot delete admin group |
177
|
|
|
return new OC_OCS_Result(null, 102); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return new OC_OCS_Result(null, 100); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param array $parameters |
185
|
|
|
* @return OC_OCS_Result |
186
|
|
|
*/ |
187
|
|
|
public function getSubAdminsOfGroup($parameters) { |
188
|
|
|
$group = $this->unencodeGroupId($parameters['groupid']); |
189
|
|
|
// Check group exists |
190
|
|
|
$targetGroup = $this->groupManager->get($group); |
191
|
|
|
if ($targetGroup === null) { |
192
|
|
|
return new OC_OCS_Result(null, 101, 'Group does not exist'); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup); |
196
|
|
|
// New class returns IUser[] so convert back |
197
|
|
|
$uids = []; |
198
|
|
|
foreach ($subadmins as $user) { |
199
|
|
|
$uids[] = $user->getUID(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return new OC_OCS_Result($uids); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.