Completed
Push — master ( fde08a...208e38 )
by Blizzz
17:49
created

GroupsController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 11

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
c 0
b 0
f 0
nc 1
nop 6
dl 12
loc 12
rs 9.4285
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Lukas Reschke <[email protected]>
7
 * @author Morris Jobke <[email protected]>
8
 *
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\IGroup;
32
use OCP\IGroupManager;
33
use OCP\IL10N;
34
use OCP\IRequest;
35
use OCP\IUserSession;
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
	/** @var bool */
48
	private $isAdmin;
49
50
	/**
51
	 * @param string $appName
52
	 * @param IRequest $request
53
	 * @param IGroupManager $groupManager
54
	 * @param IUserSession $userSession
55
	 * @param bool $isAdmin
56
	 * @param IL10N $l10n
57
	 */
58 View Code Duplication
	public function __construct($appName,
59
								IRequest $request,
60
								IGroupManager $groupManager,
61
								IUserSession $userSession,
62
								$isAdmin,
63
								IL10N $l10n) {
64
		parent::__construct($appName, $request);
65
		$this->groupManager = $groupManager;
66
		$this->userSession = $userSession;
67
		$this->isAdmin = $isAdmin;
68
		$this->l10n = $l10n;
69
	}
70
71
	/**
72
	 * @NoAdminRequired
73
	 *
74
	 * @param string $pattern
75
	 * @param bool $filterGroups
76
	 * @param int $sortGroups
77
	 * @return DataResponse
78
	 */
79
	public function index($pattern = '', $filterGroups = false, $sortGroups = MetaData::SORT_USERCOUNT) {
80
		$groupPattern = $filterGroups ? $pattern : '';
81
82
		$groupsInfo = new MetaData(
83
			$this->userSession->getUser()->getUID(),
84
			$this->isAdmin,
85
			$this->groupManager,
86
			$this->userSession
87
		);
88
		$groupsInfo->setSorting($sortGroups);
89
		list($adminGroups, $groups) = $groupsInfo->get($groupPattern, $pattern);
90
91
		return new DataResponse(
92
			array(
93
				'data' => array('adminGroups' => $adminGroups, 'groups' => $groups)
94
			)
95
		);
96
	}
97
98
	/**
99
	 * @PasswordConfirmationRequired
100
	 * @param string $id
101
	 * @return DataResponse
102
	 */
103
	public function create($id) {
104
		if($this->groupManager->groupExists($id)) {
105
			return new DataResponse(
106
				array(
107
					'message' => (string)$this->l10n->t('Group already exists.')
108
				),
109
				Http::STATUS_CONFLICT
110
			);
111
		}
112
		$group = $this->groupManager->createGroup($id);
113
		if($group instanceof IGroup) {
114
			return new DataResponse(['groupname' => $group->getDisplayName()], Http::STATUS_CREATED);
115
		}
116
117
		return new DataResponse(
118
			array(
119
				'status' => 'error',
120
				'data' => array(
121
					'message' => (string)$this->l10n->t('Unable to add group.')
122
				)
123
			),
124
			Http::STATUS_FORBIDDEN
125
		);
126
	}
127
128
	/**
129
	 * @PasswordConfirmationRequired
130
	 * @param string $id
131
	 * @return DataResponse
132
	 */
133
	public function destroy($id) {
134
		$group = $this->groupManager->get($id);
135
		if ($group) {
136
			if ($group->delete()) {
137
				return new DataResponse(
138
					array(
139
						'status' => 'success',
140
						'data' => ['groupname' => $group->getDisplayName()]
141
					),
142
					Http::STATUS_NO_CONTENT
143
				);
144
			}
145
		}
146
		return new DataResponse(
147
			array(
148
				'status' => 'error',
149
				'data' => array(
150
					'message' => (string)$this->l10n->t('Unable to delete group.')
151
				),
152
			),
153
			Http::STATUS_FORBIDDEN
154
		);
155
	}
156
157
}
158