Completed
Push — master ( 85fefc...4fcdbb )
by Thomas
09:37
created

GroupsController::isAdmin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Joas Schilling <[email protected]>
4
 * @author Lukas Reschke <[email protected]>
5
 * @author Morris Jobke <[email protected]>
6
 * @author Thomas Müller <[email protected]>
7
 *
8
 * @copyright Copyright (c) 2017, ownCloud GmbH
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OC\Settings\Controller;
26
27
use OC\AppFramework\Http;
28
use OC\Group\MetaData;
29
use OCP\AppFramework\Controller;
30
use OCP\AppFramework\Http\DataResponse;
31
use OCP\IGroupManager;
32
use OCP\IL10N;
33
use OCP\IRequest;
34
use OCP\IUserSession;
35
use OCP\IUser;
36
37
/**
38
 * @package OC\Settings\Controller
39
 */
40
class GroupsController extends Controller {
41
	/** @var IGroupManager */
42
	private $groupManager;
43
	/** @var IL10N */
44
	private $l10n;
45
	/** @var IUserSession */
46
	private $userSession;
47
48
	/**
49
	 * @param string $appName
50
	 * @param IRequest $request
51
	 * @param IGroupManager $groupManager
52
	 * @param IUserSession $userSession
53
	 * @param IL10N $l10n
54
	 */
55
	public function __construct($appName,
56
								IRequest $request,
57
								IGroupManager $groupManager,
58
								IUserSession $userSession,
59
								IL10N $l10n) {
60
		parent::__construct($appName, $request);
61
		$this->groupManager = $groupManager;
62
		$this->userSession = $userSession;
63
		$this->l10n = $l10n;
64
	}
65
66
	/**
67
	 * @NoAdminRequired
68
	 *
69
	 * @param string $pattern
70
	 * @param bool $filterGroups
71
	 * @param int $sortGroups
72
	 * @return DataResponse
73
	 */
74
	public function index($pattern = '', $filterGroups = false, $sortGroups = MetaData::SORT_USERCOUNT) {
75
		$groupPattern = $filterGroups ? $pattern : '';
76
77
		$groupsInfo = new MetaData(
78
			$this->userSession->getUser()->getUID(),
79
			$this->isAdmin(),
80
			$this->groupManager,
81
			$this->userSession
82
		);
83
		$groupsInfo->setSorting($sortGroups);
84
		list($adminGroups, $groups) = $groupsInfo->get($groupPattern, $pattern);
85
86
		return new DataResponse(
87
			[
88
				'data' => ['adminGroups' => $adminGroups, 'groups' => $groups]
89
			]
90
		);
91
	}
92
93
	/**
94
	 * @param string $id
95
	 * @return DataResponse
96
	 */
97
	public function create($id) {
98
		if($this->groupManager->groupExists($id)) {
99
			return new DataResponse(
100
				[
101
					'message' => (string)$this->l10n->t('Group already exists.')
102
				],
103
				Http::STATUS_CONFLICT
104
			);
105
		}
106
		if($this->groupManager->createGroup($id)) {
107
			return new DataResponse(
108
				[
109
					'groupname' => $id
110
				],
111
				Http::STATUS_CREATED
112
			);
113
		}
114
115
		return new DataResponse(
116
			[
117
				'status' => 'error',
118
				'data' => [
119
					'message' => (string)$this->l10n->t('Unable to add group.')
120
				]
121
			],
122
			Http::STATUS_FORBIDDEN
123
		);
124
	}
125
126
	/**
127
	 * @param string $id
128
	 * @return DataResponse
129
	 */
130
	public function destroy($id) {
131
		$group = $this->groupManager->get($id);
132
		if ($group) {
133
			if ($group->delete()) {
134
				return new DataResponse(
135
					[
136
						'status' => 'success',
137
						'data' => [
138
							'groupname' => $id
139
						]
140
					],
141
					Http::STATUS_NO_CONTENT
142
				);
143
			}
144
		}
145
		return new DataResponse(
146
			[
147
				'status' => 'error',
148
				'data' => [
149
					'message' => (string)$this->l10n->t('Unable to delete group.')
150
				],
151
			],
152
			Http::STATUS_FORBIDDEN
153
		);
154
	}
155
156
	/**
157
	 * Check if current user (active and not in incognito mode)
158
	 * is an admin
159
	 *
160
	 * @return bool
161
	 */
162 View Code Duplication
	private function isAdmin() {
163
		// Get current user (active and not in incognito mode)
164
		$user = $this->userSession->getUser();
165
		if(!is_null($user)) {
166
			return $this->groupManager->isAdmin($user->getUID());
167
		}
168
		return false;
169
	}
170
}
171