Completed
Push — master ( 6a9e02...1b0cda )
by René
03:08 queued 03:03
created

SystemController::getSystem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 11
ccs 0
cts 9
cp 0
crap 2
rs 10
c 1
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
use OCA\Polls\Db\Share;
36
use OCA\Polls\Db\ShareMapper;
37
use OCA\Polls\Db\Vote;
38
use OCA\Polls\Db\VoteMapper;
39
use OCP\ILogger;
40
41
42
class SystemController extends Controller {
43
44
	private $userId;
45
	private $logger;
46
	private $systemConfig;
47
	private $groupManager;
48
	private $userManager;
49
	private $voteMapper;
50
	private $shareMapper;
51
52
	/**
53
	 * PageController constructor.
54
	 * @param string $appName
55
	 * @param $userId
56
	 * @param IRequest $request
57
	 * @param ILogger $logger
58
	 * @param IConfig $systemConfig
59
	 * @param IGroupManager $groupManager
60
	 * @param IUserManager $userManager
61
	 * @param VoteMapper $voteMapper
62
	 * @param ShareMapper $shareMapper
63
	 */
64
	public function __construct(
65
		string $appName,
66
		$UserId,
67
		IRequest $request,
68
		ILogger $logger,
69
		IConfig $systemConfig,
70
		IGroupManager $groupManager,
71
		IUserManager $userManager,
72
		VoteMapper $voteMapper,
73
		ShareMapper $shareMapper
74
	) {
75
		parent::__construct($appName, $request);
76
		$this->voteMapper = $voteMapper;
77
		$this->shareMapper = $shareMapper;
78
		$this->logger = $logger;
79
		$this->userId = $UserId;
80
		$this->systemConfig = $systemConfig;
81
		$this->groupManager = $groupManager;
82
		$this->userManager = $userManager;
83
	}
84
85
	/**
86
	 * Get the endor  name of the installation ('ownCloud' or 'Nextcloud')
87
	 * @NoAdminRequired
88
	 * @return String
89
	 */
90
	private function getVendor() {
0 ignored issues
show
Unused Code introduced by
The method getVendor() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
91
		require \OC::$SERVERROOT . '/version.php';
92
93
		/** @var string $vendor */
94
		return (string) $vendor;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $vendor seems to be never defined.
Loading history...
95
	}
96
97
	/**
98
	 * Get a list of NC users and groups
99
	 * @NoCSRFRequired
100
	 * @NoAdminRequired
101
	 * @param string $query
102
	 * @param bool $getGroups - search in groups
103
	 * @param bool $getUsers - search in site users
104
	 * @param bool $getContacts - search in contacs
105
	 * @param array $skipGroups - group names to skip in return array
106
	 * @param array $skipUsers - user names to skip in return array
107
	 * @return DataResponse
108
	 */
109
	public function getSiteUsersAndGroups($query = '', $getGroups = true, $getUsers = true, $getContacts = true, $skipGroups = array(), $skipUsers = array()) {
110
		$list = array();
111
		if ($getGroups) {
112
			$groups = $this->groupManager->search($query);
113
			foreach ($groups as $group) {
114
				if (!in_array($group->getGID(), $skipGroups)) {
115
					$list[] = [
116
						'id' => $group->getGID(),
117
						'user' => $group->getGID(),
118
						'organisation' => '',
119
						'displayName' => $group->getGID(),
120
						'emailAddress' => '',
121
						'desc' => 'Group',
122
						'type' => 'group',
123
						'icon' => 'icon-group',
124
						'avatarURL' => '',
125
						'avatar' => '',
126
						'lastLogin' => '',
127
						'cloudId' => ''
128
129
					];
130
				}
131
			}
132
		}
133
134
		if ($getUsers) {
135
			$users = $this->userManager->searchDisplayName($query);
136
			foreach ($users as $user) {
137
				if (!in_array($user->getUID(), $skipUsers)) {
138
					$list[] = [
139
						'id' => $user->getUID(),
140
						'user' => $user->getUID(),
141
						'displayName' => $user->getDisplayName(),
142
						'organisation' => '',
143
						'emailAddress' => $user->getEMailAddress(),
144
						'desc' => 'User',
145
						'type' => 'user',
146
						'icon' => 'icon-user',
147
						'avatarURL' => '',
148
						'avatar' => '',
149
						'lastLogin' => $user->getLastLogin(),
150
						'cloudId' => $user->getCloudId()
151
					];
152
				}
153
			}
154
		}
155
156
		$contactsManager = \OC::$server->getContactsManager();
157
158
159
		if ($getContacts && $contactsManager->isEnabled()) {
160
			$contacts = $contactsManager->search($query, array('FN', 'EMAIL', 'ORG', 'CATEGORIES'));
161
			$receivers = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $receivers is dead and can be removed.
Loading history...
162
163
			// $this->logger->error(json_encode($contacts));
164
			foreach ($contacts as $contact) {
165
				$group = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $group is dead and can be removed.
Loading history...
166
				$org = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $org is dead and can be removed.
Loading history...
167
168
				if (!array_key_exists('isLocalSystemBook', $contact) && array_key_exists('EMAIL', $contact)) {
169
170
					$emailAdresses = $contact['EMAIL'];
171
172
					if (!is_array($emailAdresses)) {
173
						$emailAdresses = array($emailAdresses);
174
					} else {
175
						// take the first eMail address for now
176
						$emailAdresses = array($emailAdresses[0]);
177
					}
178
179
					foreach ($emailAdresses as $emailAddress) {
180
						$list[] = [
181
							'id' => $contact['UID'],
182
							'user' => $contact['FN'],
183
							'displayName' => $contact['FN'],
184
							'organisation' => $contact['ORG'],
185
							'emailAddress' => $emailAddress,
186
							'desc' => 'Contact',
187
							'type' => 'contact',
188
							'icon' => 'icon-mail',
189
							'avatarURL' => '',
190
							'avatar' => $contact['PHOTO'],
191
							'lastLogin' => '',
192
							'cloudId' => ''
193
						];
194
					}
195
196
				}
197
			}
198
199
		}
200
201
		return new DataResponse([
202
			'siteusers' => $list
203
		], Http::STATUS_OK);
204
	}
205
206
	/**
207
	 * Validate it the user name is reservrd
208
	 * return false, if this username already exists as a user or as
209
	 * a participant of the poll
210
	 * @NoCSRFRequired
211
	 * @NoAdminRequired
212
	 * @PublicPage
213
	 * @return DataResponse
214
	 */
215
	public function validatePublicUsername($pollId, $userName) {
216
		$list = array();
217
218
		$groups = $this->groupManager->search('');
219
		foreach ($groups as $group) {
220
			$list[] = [
221
				'id' => $group->getGID(),
222
				'user' => $group->getGID(),
223
				'type' => 'group',
224
				'displayName' => $group->getGID(),
225
			];
226
		}
227
228
		$users = $this->userManager->searchDisplayName('');
229
		foreach ($users as $user) {
230
			$list[] = [
231
				'id' => $user->getUID(),
232
				'user' => $user->getUID(),
233
				'type' => 'user',
234
				'displayName' => $user->getDisplayName(),
235
			];
236
		}
237
238
		$votes = $this->voteMapper->findParticipantsByPoll($pollId);
239
		foreach ($votes as $vote) {
240
			if ($vote->getUserId() !== '' && $vote->getUserId() !== null ) {
241
				$list[] = [
242
					'id' => $vote->getUserId(),
243
					'user' => $vote->getUserId(),
244
					'type' => 'participant',
245
					'displayName' => $vote->getUserId(),
246
				];
247
			}
248
		}
249
250
		$shares = $this->shareMapper->findByPoll($pollId);
251
		foreach ($shares as $share) {
252
			if ($share->getUserId() !== '' && $share->getUserId() !== null ) {
253
				$list[] = [
254
					'id' => $share->getUserId(),
255
					'user' => $share->getUserId(),
256
					'type' => 'share',
257
					'displayName' => $share->getUserId(),
258
				];
259
			}
260
		}
261
262
		foreach ($list as $element) {
263
			if (strtolower(trim($userName)) === strtolower(trim($element['id'])) || strtolower(trim($userName)) === strtolower(trim($element['displayName']))) {
264
				return new DataResponse([
265
					'result' => false
266
				], Http::STATUS_FORBIDDEN);
267
			}
268
		}
269
270
		return new DataResponse([
271
			'result' => true,
272
			'list' => $list
273
		], Http::STATUS_OK);
274
	}
275
	//
276
	//
277
	// /**
278
	//  * Get some system informations
279
	//  * @NoAdminRequired
280
	//  * @return DataResponse
281
	//  */
282
	// public function getSystem() {
283
	// 	$data = array();
284
	//
285
	// 	$data['system'] = [
286
	// 		'versionArray' => \OCP\Util::getVersion(),
287
	// 		'version' => implode('.', \OCP\Util::getVersion()),
288
	// 		'vendor' => $this->getVendor(),
289
	// 		'language' => $this->systemConfig->getUserValue($this->userId, 'core', 'lang')
290
	// 	];
291
	//
292
	// 	return new DataResponse($data, Http::STATUS_OK);
293
	// }
294
}
295