1<?php
2/**
3 * @author Joas Schilling <coding@schilljs.com>
4 * @author Lukas Reschke <lukas@statuscode.ch>
5 * @author Morris Jobke <hey@morrisjobke.de>
6 * @author Thomas Müller <thomas.mueller@tmit.eu>
7 *
8 * @copyright Copyright (c) 2018, 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
25namespace OC\Settings\Controller;
26
27use OC\AppFramework\Http;
28use OC\Group\MetaData;
29use OCP\AppFramework\Controller;
30use OCP\AppFramework\Http\DataResponse;
31use OCP\IGroupManager;
32use OCP\IL10N;
33use OCP\IRequest;
34use OCP\IUserSession;
35
36/**
37 * @package OC\Settings\Controller
38 */
39class GroupsController extends Controller {
40	/** @var IGroupManager */
41	private $groupManager;
42	/** @var IL10N */
43	private $l10n;
44	/** @var IUserSession */
45	private $userSession;
46	/** @var bool */
47	private $isAdmin;
48
49	/**
50	 * @param string $appName
51	 * @param IRequest $request
52	 * @param IGroupManager $groupManager
53	 * @param IUserSession $userSession
54	 * @param bool $isAdmin
55	 * @param IL10N $l10n
56	 */
57	public function __construct(
58		$appName,
59		IRequest $request,
60		IGroupManager $groupManager,
61		IUserSession $userSession,
62		$isAdmin,
63		IL10N $l10n
64	) {
65		parent::__construct($appName, $request);
66		$this->groupManager = $groupManager;
67		$this->userSession = $userSession;
68		$this->isAdmin = $isAdmin;
69		$this->l10n = $l10n;
70	}
71
72	/**
73	 * @NoAdminRequired
74	 *
75	 * @param string $pattern
76	 * @param bool $filterGroups
77	 * @param int $sortGroups
78	 * @return DataResponse
79	 */
80	public function index($pattern = '', $filterGroups = false, $sortGroups = MetaData::SORT_USERCOUNT) {
81		$groupPattern = $filterGroups ? $pattern : '';
82
83		$groupsInfo = new MetaData(
84			$this->userSession->getUser()->getUID(),
85			$this->isAdmin,
86			$this->groupManager,
87			$this->userSession
88		);
89		$groupsInfo->setSorting($sortGroups);
90		list($adminGroups, $groups) = $groupsInfo->get($groupPattern, $pattern);
91
92		return new DataResponse(
93			[
94				'data' => ['adminGroups' => $adminGroups, 'groups' => $groups]
95			]
96		);
97	}
98
99	/**
100	 * @param string $id
101	 * @return DataResponse
102	 */
103	public function create($id) {
104		if ($this->groupManager->groupExists($id)) {
105			return new DataResponse(
106				[
107					'message' => (string)$this->l10n->t('Group already exists.')
108				],
109				Http::STATUS_CONFLICT
110			);
111		}
112		if ($this->groupManager->createGroup($id)) {
113			return new DataResponse(
114				[
115					'groupname' => $id
116				],
117				Http::STATUS_CREATED
118			);
119		}
120
121		return new DataResponse(
122			[
123				'status' => 'error',
124				'data' => [
125					'message' => (string)$this->l10n->t('Unable to add group.')
126				]
127			],
128			Http::STATUS_FORBIDDEN
129		);
130	}
131
132	/**
133	 * @param string $id
134	 * @return DataResponse
135	 */
136	public function destroy($id) {
137		$group = $this->groupManager->get(\urldecode($id));
138		if ($group) {
139			if ($group->delete()) {
140				return new DataResponse(
141					[
142						'status' => 'success',
143						'data' => [
144							'groupname' => $id
145						]
146					],
147					Http::STATUS_NO_CONTENT
148				);
149			}
150		}
151		return new DataResponse(
152			[
153				'status' => 'error',
154				'data' => [
155					'message' => (string)$this->l10n->t('Unable to delete group.')
156				],
157			],
158			Http::STATUS_FORBIDDEN
159		);
160	}
161
162	/**
163	 * Get available groups for assigning and removing via WebUI.
164	 *
165	 * @NoAdminRequired
166	 *
167	 * @return DataResponse
168	 */
169	public function getAssignableAndRemovableGroups() {
170		$assignableGroups = [];
171		$removableGroups = [];
172
173		foreach ($this->groupManager->getBackends() as $backend) {
174			$groups = $backend->getGroups();
175			if ($backend->implementsActions($backend::ADD_TO_GROUP)) {
176				\array_push($assignableGroups, ...$groups);
177			}
178			if ($backend->implementsActions($backend::REMOVE_FROM_GROUP)) {
179				\array_push($removableGroups, ...$groups);
180			}
181		}
182
183		return new DataResponse(
184			[
185				'data' => [
186					'assignableGroups' => $assignableGroups,
187					'removableGroups' => $removableGroups,
188				],
189				Http::STATUS_OK
190			]
191		);
192	}
193}
194