Completed
Push — master ( c1632c...027069 )
by Joas
22:03 queued 08:37
created

GroupsController::getGroup()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 18
nc 5
nop 1
dl 0
loc 28
rs 8.439
c 1
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Lukas Reschke <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 * @author Roeland Jago Douma <[email protected]>
8
 * @author Tom Needham <[email protected]>
9
 *
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\Controller;
27
28
use OCP\AppFramework\Http\DataResponse;
29
use OCP\AppFramework\OCS\OCSException;
30
use OCP\AppFramework\OCSController;
31
use OCP\IGroup;
32
use OCP\IGroupManager;
33
use OCP\IRequest;
34
use OCP\IUserSession;
35
use OCP\IUser;
36
37
38
class GroupsController extends OCSController {
39
40
	/** @var IGroupManager */
41
	private $groupManager;
42
43
	/** @var IUserSession */
44
	private $userSession;
45
46
	/**
47
	 * @param string $appName
48
	 * @param IRequest $request
49
	 * @param IGroupManager $groupManager
50
	 * @param IUserSession $userSession
51
	 */
52
	public function __construct(
53
			$appName,
54
			IRequest $request,
55
			IGroupManager $groupManager,
56
			IUserSession $userSession) {
57
		parent::__construct($appName, $request);
58
59
		$this->groupManager = $groupManager;
60
		$this->userSession = $userSession;
61
	}
62
63
	/**
64
	 * returns a list of groups
65
	 *
66
	 * @NoAdminRequired
67
	 *
68
	 * @param string $search
69
	 * @param int $limit
70
	 * @param int $offset
71
	 * @return DataResponse
72
	 */
73
	public function getGroups($search = '', $limit = null, $offset = null) {
74
		if ($limit !== null) {
75
			$limit = (int)$limit;
76
		}
77
		if ($offset !== null) {
78
			$offset = (int)$offset;
79
		}
80
81
		$groups = $this->groupManager->search($search, $limit, $offset);
82
		$groups = array_map(function($group) {
83
			/** @var IGroup $group */
84
			return $group->getGID();
85
		}, $groups);
86
87
		return new DataResponse(['groups' => $groups]);
88
	}
89
90
	/**
91
	 * returns an array of users in the group specified
92
	 *
93
	 * @NoAdminRequired
94
	 *
95
	 * @param string $groupId
96
	 * @return DataResponse
97
	 * @throws OCSException
98
	 */
99
	public function getGroup($groupId) {
100
		$user = $this->userSession->getUser();
101
102
		// Check the group exists
103
		if(!$this->groupManager->groupExists($groupId)) {
104
			throw new OCSException('The requested group could not be found', \OCP\API::RESPOND_NOT_FOUND);
105
		}
106
107
		$isSubadminOfGroup = false;
108
		$group = $this->groupManager->get($groupId);
109
		if ($group !== null) {
110
			$isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminofGroup($user, $group);
111
		}
112
113
		// Check subadmin has access to this group
114
		if($this->groupManager->isAdmin($user->getUID())
115
		   || $isSubadminOfGroup) {
116
			$users = $this->groupManager->get($groupId)->getUsers();
117
			$users =  array_map(function($user) {
118
				/** @var IUser $user */
119
				return $user->getUID();
120
			}, $users);
121
			$users = array_values($users);
122
			return new DataResponse(['users' => $users]);
123
		} else {
124
			throw new OCSException('User does not have access to specified group', \OCP\API::RESPOND_UNAUTHORISED);
125
		}
126
	}
127
128
	/**
129
	 * creates a new group
130
	 *
131
	 * @NoAdminRequired
132
	 *
133
	 * @param string $groupid
134
	 * @return DataResponse
135
	 * @throws OCSException
136
	 */
137
	public function addGroup($groupid) {
138
		// Validate name
139
		if(empty($groupid)){
140
			\OCP\Util::writeLog('provisioning_api', 'Group name not supplied', \OCP\Util::ERROR);
141
			throw new OCSException('Invalid group name', 101);
142
		}
143
		// Check if it exists
144
		if($this->groupManager->groupExists($groupid)){
145
			throw new OCSException('', 102);
146
		}
147
		$this->groupManager->createGroup($groupid);
148
		return new DataResponse();
149
	}
150
151
	/**
152
	 * @param string $groupId
153
	 * @return DataResponse
154
	 * @throws OCSException
155
	 */
156
	public function deleteGroup($groupId) {
157
		// Check it exists
158
		if(!$this->groupManager->groupExists($groupId)){
159
			throw new OCSException('', 101);
160
		} else if($groupId === 'admin' || !$this->groupManager->get($groupId)->delete()){
161
			// Cannot delete admin group
162
			throw new OCSException('', 102);
163
		}
164
165
		return new DataResponse(null, 100);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array|object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
166
	}
167
168
	/**
169
	 * @param string $groupId
170
	 * @return DataResponse
171
	 * @throws OCSException
172
	 */
173
	public function getSubAdminsOfGroup($groupId) {
174
		// Check group exists
175
		$targetGroup = $this->groupManager->get($groupId);
176
		if($targetGroup === null) {
177
			throw new OCSException('Group does not exist', 101);
178
		}
179
180
		$subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup);
181
		// New class returns IUser[] so convert back
182
		$uids = [];
183
		foreach ($subadmins as $user) {
184
			$uids[] = $user->getUID();
185
		}
186
187
		return new DataResponse($uids);
188
	}
189
190
}
191