Passed
Pull Request — master (#487)
by René
04:26
created

SystemController::getSiteUsersAndGroups()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 41
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 30
nc 4
nop 5
dl 0
loc 41
ccs 0
cts 39
cp 0
crap 56
rs 8.5066
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]>
4
 *
5
 * @author René Gieling <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Polls\Controller;
25
26
use OCP\AppFramework\Controller;
27
use OCP\AppFramework\Http;
28
use OCP\AppFramework\Http\DataResponse;
29
30
use OCP\IGroupManager;
31
use OCP\IUser;
32
use OCP\IUserManager;
33
use OCP\IConfig;
34
use OCP\IRequest;
35
36
class SystemController extends Controller {
37
38
	private $systemConfig;
39
40
	/**
41
	 * PageController constructor.
42
	 * @param String $appName
43
	 * @param IConfig $systemConfig
44
	 * @param IRequest $request
45
	 * @param IGroupManager $groupManager
46
	 * @param IUserManager $userManager
47
	 */
48
	public function __construct(
49
		$appName,
50
		IGroupManager $groupManager,
51
		IUserManager $userManager,
52
		IConfig $systemConfig,
53
		IRequest $request
54
	) {
55
		parent::__construct($appName, $request);
56
		$this->systemConfig = $systemConfig;
57
		$this->groupManager = $groupManager;
0 ignored issues
show
Bug Best Practice introduced by
The property groupManager does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
58
		$this->userManager = $userManager;
0 ignored issues
show
Bug Best Practice introduced by
The property userManager does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
59
	}
60
61
	/**
62
	 * Get the endor  name of the installation ('ownCloud' or 'Nextcloud')
63
	 * @NoAdminRequired
64
	 * @return String
65
	 */
66
	private function getVendor() {
67
		require \OC::$SERVERROOT . '/version.php';
68
69
		/** @var string $vendor */
70
		return (string) $vendor;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $vendor seems to be never defined.
Loading history...
71
	}
72
73
	/**
74
	 * Get a list of NC users and groups
75
	 * @NoAdminRequired
76
	 * @return DataResponse
77
	 */
78
	public function getSiteUsersAndGroups($query = '', $getGroups = true, $getUsers = true, $skipGroups = array(), $skipUsers = array()) {
79
		$list = array();
80
		$data = array();
81
		if ($getGroups) {
82
			$groups = $this->groupManager->search($query);
83
			foreach ($groups as $group) {
84
				if (!in_array($group->getGID(), $skipGroups)) {
85
					$list[] = [
86
						'id' => $group->getGID(),
87
						'user' => $group->getGID(),
88
						'type' => 'group',
89
						'desc' => 'group',
90
						'icon' => 'icon-group',
91
						'displayName' => $group->getGID(),
92
						'avatarURL' => ''
93
					];
94
				}
95
			}
96
		}
97
98
		if ($getUsers) {
99
			$users = $this->userManager->searchDisplayName($query);
100
			foreach ($users as $user) {
101
				if (!in_array($user->getUID(), $skipUsers)) {
102
					$list[] = [
103
						'id' => $user->getUID(),
104
						'user' => $user->getUID(),
105
						'type' => 'user',
106
						'desc' => 'user',
107
						'icon' => 'icon-user',
108
						'displayName' => $user->getDisplayName(),
109
						'avatarURL' => '',
110
						'lastLogin' => $user->getLastLogin(),
111
						'cloudId' => $user->getCloudId()
112
					];
113
				}
114
			}
115
		}
116
117
		$data['siteusers'] = $list;
118
		return new DataResponse($data, Http::STATUS_OK);
119
	}
120
121
	/**
122
	 * Get some system informations
123
	 * @NoAdminRequired
124
	 * @return DataResponse
125
	 */
126
	public function getSystem() {
127
		$userId = \OC::$server->getUserSession()->getUser()->getUID();
128
		$data['system'] = [
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
129
			'versionArray' => \OCP\Util::getVersion(),
130
			'version' => implode('.', \OCP\Util::getVersion()),
131
			'vendor' => $this->getVendor(),
132
			'language' => $this->systemConfig->getUserValue($userId, 'core', 'lang')
133
		];
134
135
		return new DataResponse($data, Http::STATUS_OK);
136
	}
137
}
138