Completed
Push — master ( 214ab6...38189a )
by René
06:22 queued 11s
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() {
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
	 * @return DataResponse
102
	 */
103
	public function getSiteUsersAndGroups($query = '', $getGroups = true, $getUsers = true, $getContacts = true, $skipGroups = array(), $skipUsers = array()) {
104
		$list = array();
105
		if ($getGroups) {
106
			$groups = $this->groupManager->search($query);
107
			foreach ($groups as $group) {
108
				if (!in_array($group->getGID(), $skipGroups)) {
109
					$list[] = [
110
						'id' => $group->getGID(),
111
						'user' => $group->getGID(),
112
						'type' => 'group',
113
						'desc' => 'group',
114
						'icon' => 'icon-group',
115
						'displayName' => $group->getGID(),
116
						'avatarURL' => ''
117
					];
118
				}
119
			}
120
		}
121
122
		if ($getUsers) {
123
			$users = $this->userManager->searchDisplayName($query);
124
			foreach ($users as $user) {
125
				if (!in_array($user->getUID(), $skipUsers)) {
126
					$list[] = [
127
						'id' => $user->getUID(),
128
						'user' => $user->getUID(),
129
						'type' => 'user',
130
						'desc' => 'user',
131
						'icon' => 'icon-user',
132
						'displayName' => $user->getDisplayName(),
133
						'avatarURL' => '',
134
						'lastLogin' => $user->getLastLogin(),
135
						'cloudId' => $user->getCloudId()
136
					];
137
				}
138
			}
139
		}
140
141
		$cm = \OC::$server->getContactsManager();
142
143
144
		if ($getContacts && $cm->isEnabled()) {
145
			$result = $cm->search($query, array('FN', 'EMAIL', 'ORG', 'CATEGORIES'));
146
			$receivers = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $receivers is dead and can be removed.
Loading history...
147
148
			// $this->logger->error(json_encode($result));
149
			foreach ($result as $r) {
150
				$group = '';
151
				$org = '';
152
153
				if (!array_key_exists('isLocalSystemBook',$r)) {
154
155
					if (array_key_exists('CATEGORIES',$r)) {
156
						$group = ' (Groups: ' . $r['CATEGORIES'] . ')';
157
					}
158
159
					if (array_key_exists('ORG',$r)) {
160
						$org = ' ('. $r['ORG'] . ')' ;
161
					}
162
163
					$list[] = [
164
						'id' => $r['EMAIL'],
165
						'user' => $r['FN'] . $org,
166
						'type' => 'mail',
167
						'desc' => $r['EMAIL'][0] . $group,
168
						'icon' => 'icon-mail',
169
						'displayName' => $r['FN'] . ' ' . $org,
170
						'avatarURL' => $r['PHOTO'],
171
						'lastLogin' => '',
172
						'cloudId' => ''
173
					];
174
				}
175
			//
176
			// 	if (!is_array($email)) {
177
			// 		$email = array($email);
178
			// 	}
179
			//
180
			// // loop through all email addresses of this contact
181
			// 	foreach ($email as $e) {
182
			// 		$displayName = $fn . ' <$e>';
183
			// 		$list[] = array(
184
			// 			'id' => $id,
185
			// 			'label' => $displayName,
186
			// 			'value' => $displayName);
187
			// 	}
188
			}
189
190
		}
191
192
193
194
195
		return new DataResponse([
196
			'siteusers' => $list
197
		], Http::STATUS_OK);
198
	}
199
200
	/**
201
	 * Get a list of NC users and groups
202
	 * @NoCSRFRequired
203
	 * @NoAdminRequired
204
	 * @PublicPage
205
	 * @return DataResponse
206
	 */
207
208
	public static function getContact($term) {
209
		$cm = \OC::$server->getContactsManager();
210
		// The API is not active -> nothing to do
211
		if (!$cm->isEnabled()) {
212
			return array();
213
		}
214
215
		$result = $cm->search($term, array('FN', 'EMAIL'));
216
		$receivers = array();
217
218
		foreach ($result as $r) {
219
			$id = $r['id'];
220
			$fn = $r['FN'];
221
			$email = $r['EMAIL'];
222
223
			if (!is_array($email)) {
224
				$email = array($email);
225
			}
226
227
		// loop through all email addresses of this contact
228
			foreach ($email as $e) {
229
				$displayName = $fn . ' <$e>';
230
				$receivers[] = array(
231
					'id' => $id,
232
					'label' => $displayName,
233
					'value' => $displayName);
234
			}
235
		}
236
237
		return $receivers;
238
	}
239
240
241
242
243
244
245
246
247
248
	/**
249
	 * Validate it the user name is reservrd
250
	 * return false, if this username already exists as a user or as
251
	 * a participant of the poll
252
	 * @NoCSRFRequired
253
	 * @NoAdminRequired
254
	 * @PublicPage
255
	 * @return DataResponse
256
	 */
257
	public function validatePublicUsername($pollId, $userName) {
258
		$list = array();
259
260
		$groups = $this->groupManager->search('');
261
		foreach ($groups as $group) {
262
			$list[] = [
263
				'id' => $group->getGID(),
264
				'user' => $group->getGID(),
265
				'type' => 'group',
266
				'displayName' => $group->getGID(),
267
			];
268
		}
269
270
		$users = $this->userManager->searchDisplayName('');
271
		foreach ($users as $user) {
272
			$list[] = [
273
				'id' => $user->getUID(),
274
				'user' => $user->getUID(),
275
				'type' => 'user',
276
				'displayName' => $user->getDisplayName(),
277
			];
278
		}
279
280
		$votes = $this->voteMapper->findParticipantsByPoll($pollId);
281
		foreach ($votes as $vote) {
282
			if ($vote->getUserId() !== '' && $vote->getUserId() !== null ) {
283
				$list[] = [
284
					'id' => $vote->getUserId(),
285
					'user' => $vote->getUserId(),
286
					'type' => 'participant',
287
					'displayName' => $vote->getUserId(),
288
				];
289
			}
290
		}
291
292
		$shares = $this->shareMapper->findByPoll($pollId);
293
		foreach ($shares as $share) {
294
			if ($share->getUserId() !== '' && $share->getUserId() !== null ) {
295
				$list[] = [
296
					'id' => $share->getUserId(),
297
					'user' => $share->getUserId(),
298
					'type' => 'share',
299
					'displayName' => $share->getUserId(),
300
				];
301
			}
302
		}
303
304
		foreach ($list as $element) {
305
			if (strtolower(trim($userName)) === strtolower(trim($element['id'])) || strtolower(trim($userName)) === strtolower(trim($element['displayName']))) {
306
				return new DataResponse([
307
					'result' => false
308
				], Http::STATUS_FORBIDDEN);
309
			}
310
		}
311
312
		return new DataResponse([
313
			'result' => true,
314
			'list' => $list
315
		], Http::STATUS_OK);
316
	}
317
318
319
	/**
320
	 * Get some system informations
321
	 * @NoAdminRequired
322
	 * @return DataResponse
323
	 */
324
	public function getSystem() {
325
		$data = array();
326
327
		$data['system'] = [
328
			'versionArray' => \OCP\Util::getVersion(),
329
			'version' => implode('.', \OCP\Util::getVersion()),
330
			'vendor' => $this->getVendor(),
331
			'language' => $this->systemConfig->getUserValue($this->userId, 'core', 'lang')
332
		];
333
334
		return new DataResponse($data, Http::STATUS_OK);
335
	}
336
}
337