Completed
Push — master ( 66fc24...2137ab )
by René
10s
created

PageController::gotoPoll()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 37
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 32
nc 9
nop 1
dl 0
loc 37
rs 9.0968
c 0
b 0
f 0
ccs 0
cts 29
cp 0
crap 30
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]>
4
 *
5
 * @author Vinzenz Rosenkranz <[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 OCA\Polls\Db\Comment;
27
use OCA\Polls\Db\CommentMapper;
28
use OCA\Polls\Db\Date;
29
use OCA\Polls\Db\DateMapper;
30
use OCA\Polls\Db\Event;
31
use OCA\Polls\Db\EventMapper;
32
use OCA\Polls\Db\Notification;
33
use OCA\Polls\Db\NotificationMapper;
34
use OCA\Polls\Db\Participation;
35
use OCA\Polls\Db\ParticipationMapper;
36
use OCA\Polls\Db\ParticipationText;
37
use OCA\Polls\Db\ParticipationTextMapper;
38
use OCA\Polls\Db\Text;
39
use OCA\Polls\Db\TextMapper;
40
use OCP\AppFramework\Controller;
41
use OCP\AppFramework\Db\DoesNotExistException;
42
use OCP\AppFramework\Http\ContentSecurityPolicy;
43
use OCP\AppFramework\Http\JSONResponse;
44
use OCP\AppFramework\Http\RedirectResponse;
45
use OCP\AppFramework\Http\TemplateResponse;
46
use OCP\IAvatarManager;
47
use OCP\IGroupManager;
48
use OCP\IL10N;
49
use OCP\ILogger;
50
use OCP\IRequest;
51
use OCP\IURLGenerator;
52
use OCP\IUserManager;
53
use OCP\Mail\IMailer;
54
use OCP\Security\ISecureRandom;
55
use OCP\User;
56
use OCP\Util;
57
58
class PageController extends Controller {
59
60
	private $userId;
61
	private $commentMapper;
62
	private $dateMapper;
63
	private $eventMapper;
64
	private $notificationMapper;
65
	private $participationMapper;
66
	private $participationTextMapper;
67
	private $textMapper;
68
	private $urlGenerator;
69
	private $userMgr;
70
	private $avatarManager;
71
	private $logger;
72
	private $trans;
73
	private $groupManager;
74
75
	/**
76
	 * PageController constructor.
77
	 * @param string $appName
78
	 * @param IRequest $request
79
	 * @param IUserManager $userMgr
80
	 * @param IGroupManager $groupManager
81
	 * @param IAvatarManager $avatarManager
82
	 * @param ILogger $logger
83
	 * @param IL10N $trans
84
	 * @param IURLGenerator $urlGenerator
85
	 * @param string $userId
86
	 * @param CommentMapper $commentMapper
87
	 * @param DateMapper $dateMapper
88
	 * @param EventMapper $eventMapper
89
	 * @param NotificationMapper $notificationMapper
90
	 * @param ParticipationMapper $ParticipationMapper
91
	 * @param ParticipationTextMapper $ParticipationTextMapper
92
	 * @param TextMapper $textMapper
93
	 */
94 1
	public function __construct(
95
		$appName,
96
		IRequest $request,
97
		IUserManager $userMgr,
98
		IGroupManager $groupManager,
99
		IAvatarManager $avatarManager,
100
		ILogger $logger,
101
		IL10N $trans,
102
		IURLGenerator $urlGenerator,
103
		$userId,
104
		CommentMapper $commentMapper,
105
		DateMapper $dateMapper,
106
		EventMapper $eventMapper,
107
		NotificationMapper $notificationMapper,
108
		ParticipationMapper $ParticipationMapper,
109
		ParticipationTextMapper $ParticipationTextMapper,
110
		TextMapper $textMapper
111
	) {
112 1
		parent::__construct($appName, $request);
113 1
		$this->userMgr = $userMgr;
114 1
		$this->groupManager = $groupManager;
115 1
		$this->avatarManager = $avatarManager;
116 1
		$this->logger = $logger;
117 1
		$this->trans = $trans;
118 1
		$this->urlGenerator = $urlGenerator;
119 1
		$this->userId = $userId;
120 1
		$this->commentMapper = $commentMapper;
121 1
		$this->dateMapper = $dateMapper;
122 1
		$this->eventMapper = $eventMapper;
123 1
		$this->notificationMapper = $notificationMapper;
124 1
		$this->participationMapper = $ParticipationMapper;
125 1
		$this->participationTextMapper = $ParticipationTextMapper;
126 1
		$this->textMapper = $textMapper;
127 1
	}
128
129
	/**
130
	 * @NoAdminRequired
131
	 * @NoCSRFRequired
132
	 */
133 1
	public function index() {
134 1
		$polls = $this->eventMapper->findAllForUserWithInfo($this->userId);
135 1
		$comments = $this->commentMapper->findDistinctByUser($this->userId);
136 1
		$partic = $this->participationMapper->findDistinctByUser($this->userId);
137 1
		$particText = $this->participationTextMapper->findDistinctByUser($this->userId);
138 1
		$response = new TemplateResponse('polls', 'main.tmpl', [
139 1
			'polls' => $polls,
140 1
			'comments' => $comments,
141 1
			'participations' => $partic,
142 1
			'participations_text' => $particText,
143 1
			'userId' => $this->userId,
144 1
			'userMgr' => $this->userMgr,
145 1
			'urlGenerator' => $this->urlGenerator
146
		]);
147 1
		$csp = new ContentSecurityPolicy();
148 1
		$response->setContentSecurityPolicy($csp);
149 1
		return $response;
150
	}
151
152
	/**
153
	 * @param int $pollId
154
	 * @param string $from
155
	 */
156
	private function sendNotifications($pollId, $from) {
157
		$poll = $this->eventMapper->find($pollId);
158
		$notifications = $this->notificationMapper->findAllByPoll($pollId);
159
		foreach ($notifications as $notification) {
160
			if ($from === $notification->getUserId()) {
161
				continue;
162
			}
163
			$email = \OC::$server->getConfig()->getUserValue($notification->getUserId(), 'settings', 'email');
164
			if ($email === null || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
165
				continue;
166
			}
167
			$url = $this->urlGenerator->getAbsoluteURL(
168
				$this->urlGenerator->linkToRoute('polls.page.goto_poll',
169
					array('hash' => $poll->getHash()))
170
			);
171
172
			$recUser = $this->userMgr->get($notification->getUserId());
173
			$sendUser = $this->userMgr->get($from);
174
			$rec = '';
175
			if ($recUser !== null) {
176
				$rec = $recUser->getDisplayName();
177
			}
178
			$sender = $from;
179
			if ($sendUser !== null) {
180
				$sender = $sendUser->getDisplayName();
181
			}
182
			$msg = $this->trans->t('Hello %s,<br/><br/><strong>%s</strong> participated in the poll \'%s\'.<br/><br/>To go directly to the poll, you can use this <a href="%s">link</a>',
183
				array(
184
					$rec,
185
					$sender,
186
					$poll->getTitle(),
187
					$url
188
				));
189
190
			$msg .= '<br/><br/>';
191
192
			$toName = $this->userMgr->get($notification->getUserId())->getDisplayName();
193
			$subject = $this->trans->t('Polls App - New Activity');
194
			$fromAddress = Util::getDefaultEmailAddress('no-reply');
195
			$fromName = $this->trans->t('Polls App') . ' (' . $from . ')';
196
197
			try {
198
				/** @var IMailer $mailer */
199
				$mailer = \OC::$server->getMailer();
200
				/** @var \OC\Mail\Message $message */
201
				$message = $mailer->createMessage();
202
				$message->setSubject($subject);
203
				$message->setFrom(array($fromAddress => $fromName));
204
				$message->setTo(array($email => $toName));
205
				$message->setHtmlBody($msg);
206
				$mailer->send($message);
207
			} catch (\Exception $e) {
208
				$message = 'Error sending mail to: ' . $toName . ' (' . $email . ')';
209
				Util::writeLog('polls', $message, Util::ERROR);
210
			}
211
		}
212
	}
213
214
	/**
215
	 * @NoAdminRequired
216
	 * @NoCSRFRequired
217
	 * @PublicPage
218
	 * @param string $hash
219
	 * @return TemplateResponse
220
	 */
221
	public function gotoPoll($hash) {
222
		try {
223
			$poll = $this->eventMapper->findByHash($hash);
224
		} catch (DoesNotExistException $e) {
225
			return new TemplateResponse('polls', 'no.acc.tmpl', []);
226
		}
227
		if ($poll->getType() === 0) {
228
			$dates = $this->dateMapper->findByPoll($poll->getId());
229
			$votes = $this->participationMapper->findByPoll($poll->getId());
230
			$participants = $this->participationMapper->findParticipantsByPoll($poll->getId());
231
		} else {
232
			$dates = $this->textMapper->findByPoll($poll->getId());
233
			$votes = $this->participationTextMapper->findByPoll($poll->getId());
234
			$participants = $this->participationTextMapper->findParticipantsByPoll($poll->getId());
235
		}
236
		$comments = $this->commentMapper->findByPoll($poll->getId());
237
		try {
238
			$notification = $this->notificationMapper->findByUserAndPoll($poll->getId(), $this->userId);
239
		} catch (DoesNotExistException $e) {
240
			$notification = null;
241
		}
242
		if ($this->hasUserAccess($poll)) {
243
			return new TemplateResponse('polls', 'goto.tmpl', [
244
				'poll' => $poll,
245
				'dates' => $dates,
246
				'comments' => $comments,
247
				'votes' => $votes,
248
				'participants' => $participants,
249
				'notification' => $notification,
250
				'userId' => $this->userId,
251
				'userMgr' => $this->userMgr,
252
				'urlGenerator' => $this->urlGenerator,
253
				'avatarManager' => $this->avatarManager
254
			]);
255
		} else {
256
			User::checkLoggedIn();
257
			return new TemplateResponse('polls', 'no.acc.tmpl', []);
258
		}
259
	}
260
261
	/**
262
	 * @NoAdminRequired
263
	 * @NoCSRFRequired
264
	 * @param int $pollId
265
	 * @return TemplateResponse|RedirectResponse
266
	 */
267
	public function deletePoll($pollId) {
268
		$pollToDelete = $this->eventMapper->find($pollId);
269
		if ($this->userId !== $pollToDelete->getOwner()) {
270
			return new TemplateResponse('polls', 'no.delete.tmpl');
271
		}
272
		$poll = new Event();
273
		$poll->setId($pollId);
274
		$this->eventMapper->delete($poll);
275
		$this->textMapper->deleteByPoll($pollId);
276
		$this->dateMapper->deleteByPoll($pollId);
277
		$this->participationMapper->deleteByPoll($pollId);
278
		$this->participationTextMapper->deleteByPoll($pollId);
279
		$this->commentMapper->deleteByPoll($pollId);
280
		$url = $this->urlGenerator->linkToRoute('polls.page.index');
281
		return new RedirectResponse($url);
282
	}
283
284
	/**
285
	 * @NoAdminRequired
286
	 * @NoCSRFRequired
287
	 * @param string $hash
288
	 * @return TemplateResponse
289
	 */
290
	public function editPoll($hash) {
291
		$poll = $this->eventMapper->findByHash($hash);
292
		if ($this->userId !== $poll->getOwner()) {
293
			return new TemplateResponse('polls', 'no.create.tmpl');
294
		}
295
		if ($poll->getType() === 0) {
296
			$dates = $this->dateMapper->findByPoll($poll->getId());
297
		} else {
298
			$dates = $this->textMapper->findByPoll($poll->getId());
299
		}
300
		return new TemplateResponse('polls', 'create.tmpl', [
301
			'poll' => $poll,
302
			'dates' => $dates,
303
			'userId' => $this->userId,
304
			'userMgr' => $this->userMgr,
305
			'urlGenerator' => $this->urlGenerator
306
		]);
307
	}
308
309
	/**
310
	 * @NoAdminRequired
311
	 * @NoCSRFRequired
312
	 * @param int $pollId
313
	 * @param string $pollType
314
	 * @param string $pollTitle
315
	 * @param string $pollDesc
316
	 * @param string $userId
317
	 * @param string $chosenDates
318
	 * @param int $expireTs
319
	 * @param string $accessType
320
	 * @param string $accessValues
321
	 * @param bool $isAnonymous
322
	 * @param bool $hideNames
323
	 * @return RedirectResponse
324
	 */
325
	public function updatePoll(
326
		$pollId,
327
		$pollType,
328
		$pollTitle,
329
		$pollDesc,
330
		$userId,
0 ignored issues
show
Unused Code introduced by
The parameter $userId is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

330
		/** @scrutinizer ignore-unused */ $userId,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
331
		$chosenDates,
332
		$expireTs,
333
		$accessType,
334
		$accessValues,
335
		$isAnonymous,
336
		$hideNames
337
	) {
338
339
340
		$event = $this->eventMapper->find($pollId);
341
		$event->setTitle($pollTitle);
342
		$event->setDescription($pollDesc);
343
		$event->setIsAnonymous($isAnonymous ? 1 : 0);
344
		$event->setFullAnonymous($isAnonymous && $hideNames ? 1 : 0);
345
346
		if ($accessType === 'select') {
347
			if (isset($accessValues)) {
348
				$accessValues = json_decode($accessValues);
349
				if ($accessValues !== null) {
350
					$groups = array();
351
					$users = array();
352
					if ($accessValues->groups !== null) {
353
						$groups = $accessValues->groups;
354
					}
355
					if ($accessValues->users !== null) {
356
						$users = $accessValues->users;
357
					}
358
					$accessType = '';
359
					foreach ($groups as $gid) {
360
						$accessType .= $gid . ';';
361
					}
362
					foreach ($users as $uid) {
363
						$accessType .= $uid . ';';
364
					}
365
				}
366
			}
367
		}
368
		$event->setAccess($accessType);
369
		/** @var string[] $chosenDates */
370
		$chosenDates = json_decode($chosenDates);
0 ignored issues
show
Bug introduced by
$chosenDates of type string[] is incompatible with the type string expected by parameter $json of json_decode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

370
		$chosenDates = json_decode(/** @scrutinizer ignore-type */ $chosenDates);
Loading history...
371
372
		$expire = null;
373
		if ($expireTs !== 0 && $expireTs !== '') {
374
			$expire = date('Y-m-d H:i:s', $expireTs);
375
		}
376
		$event->setExpire($expire);
377
378
		$this->dateMapper->deleteByPoll($pollId);
379
		$this->textMapper->deleteByPoll($pollId);
380
		if ($pollType === 'event') {
381
			$event->setType(0);
382
			$this->eventMapper->update($event);
383
			sort($chosenDates);
384
			foreach ($chosenDates as $el) {
385
				$date = new Date();
386
				$date->setPollId($pollId);
387
				$date->setDt(date('Y-m-d H:i:s', $el));
388
				$this->dateMapper->insert($date);
389
			}
390
		} else {
391
			$event->setType(1);
392
			$this->eventMapper->update($event);
393
			foreach ($chosenDates as $el) {
394
				$text = new Text();
395
				$text->setPollId($pollId);
396
				$text->setText($el);
397
				$this->textMapper->insert($text);
398
			}
399
		}
400
		$url = $this->urlGenerator->linkToRoute('polls.page.index');
401
		return new RedirectResponse($url);
402
	}
403
404
	/**
405
	 * @NoAdminRequired
406
	 * @NoCSRFRequired
407
	 */
408
	public function createPoll() {
409
		return new TemplateResponse('polls', 'create.tmpl',
410
			['userId' => $this->userId, 'userMgr' => $this->userMgr, 'urlGenerator' => $this->urlGenerator]);
411
	}
412
413
	/**
414
	 * @NoAdminRequired
415
	 * @NoCSRFRequired
416
	 * @param string $pollType
417
	 * @param string $pollTitle
418
	 * @param string $pollDesc
419
	 * @param string $userId
420
	 * @param string $chosenDates
421
	 * @param int $expireTs
422
	 * @param string $accessType
423
	 * @param string $accessValues
424
	 * @param bool $isAnonymous
425
	 * @param bool $hideNames
426
	 * @return RedirectResponse
427
	 */
428
	public function insertPoll(
429
		$pollType,
430
		$pollTitle,
431
		$pollDesc,
432
		$userId,
433
		$chosenDates,
434
		$expireTs,
435
		$accessType,
436
		$accessValues,
437
		$isAnonymous,
438
		$hideNames
439
	) {
440
		$event = new Event();
441
		$event->setTitle($pollTitle);
442
		$event->setDescription($pollDesc);
443
		$event->setOwner($userId);
444
		$event->setCreated(date('Y-m-d H:i:s'));
445
		$event->setHash(\OC::$server->getSecureRandom()->generate(
446
			16,
447
			ISecureRandom::CHAR_DIGITS .
448
			ISecureRandom::CHAR_LOWER .
449
			ISecureRandom::CHAR_UPPER
450
		));
451
		$event->setIsAnonymous($isAnonymous ? 1 : 0);
452
		$event->setFullAnonymous($isAnonymous && $hideNames ? 1 : 0);
453
454
		if ($accessType === 'select') {
455
			if (isset($accessValues)) {
456
				$accessValues = json_decode($accessValues);
457
				if ($accessValues !== null) {
458
					$groups = array();
459
					$users = array();
460
					if ($accessValues->groups !== null) {
461
						$groups = $accessValues->groups;
462
					}
463
					if ($accessValues->users !== null) {
464
						$users = $accessValues->users;
465
					}
466
					$accessType = '';
467
					foreach ($groups as $gid) {
468
						$accessType .= $gid . ';';
469
					}
470
					foreach ($users as $uid) {
471
						$accessType .= $uid . ';';
472
					}
473
				}
474
			}
475
		}
476
		$event->setAccess($accessType);
477
		/** @var string[] $chosenDates */
478
		$chosenDates = json_decode($chosenDates);
0 ignored issues
show
Bug introduced by
$chosenDates of type string[] is incompatible with the type string expected by parameter $json of json_decode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

478
		$chosenDates = json_decode(/** @scrutinizer ignore-type */ $chosenDates);
Loading history...
479
480
		$expire = null;
481
		if ($expireTs !== 0 && $expireTs !== '') {
482
			$expire = date('Y-m-d H:i:s', $expireTs);
483
		}
484
		$event->setExpire($expire);
485
486
		if ($pollType === 'event') {
487
			$event->setType(0);
488
			$ins = $this->eventMapper->insert($event);
489
			$pollId = $ins->getId();
490
			sort($chosenDates);
491
			foreach ($chosenDates as $el) {
492
				$date = new Date();
493
				$date->setPollId($pollId);
494
				$date->setDt(date('Y-m-d H:i:s', $el));
495
				$this->dateMapper->insert($date);
496
			}
497
		} else {
498
			$event->setType(1);
499
			$ins = $this->eventMapper->insert($event);
500
			$pollId = $ins->getId();
501
			$cnt = 1;
502
			foreach ($chosenDates as $el) {
503
				$text = new Text();
504
				$text->setPollId($pollId);
505
				$text->setText($el . '_' . $cnt);
506
				$this->textMapper->insert($text);
507
				$cnt++;
508
			}
509
		}
510
		$url = $this->urlGenerator->linkToRoute('polls.page.index');
511
		return new RedirectResponse($url);
512
	}
513
514
	/**
515
	 * @NoAdminRequired
516
	 * @NoCSRFRequired
517
	 * @PublicPage
518
	 * @param int $pollId
519
	 * @param string $userId
520
	 * @param string $types
521
	 * @param string $dates
522
	 * @param bool $receiveNotifications
523
	 * @param bool $changed
524
	 * @return RedirectResponse
525
	 */
526
	public function insertVote($pollId, $userId, $types, $dates, $receiveNotifications, $changed) {
527
		if ($this->userId !== null) {
528
			if ($receiveNotifications) {
529
				try {
530
					//check if user already set notification for this poll
531
					$this->notificationMapper->findByUserAndPoll($pollId, $userId);
532
				} catch (DoesNotExistException $e) {
533
					//insert if not exist
534
					$not = new Notification();
535
					$not->setUserId($userId);
536
					$not->setPollId($pollId);
537
					$this->notificationMapper->insert($not);
538
				}
539
			} else {
540
				try {
541
					//delete if entry is in db
542
					$not = $this->notificationMapper->findByUserAndPoll($pollId, $userId);
543
					$this->notificationMapper->delete($not);
544
				} catch (DoesNotExistException $e) {
545
					//doesn't exist in db, nothing to do
546
				}
547
			}
548
		}
549
		$poll = $this->eventMapper->find($pollId);
550
		if ($changed) {
551
			$dates = json_decode($dates);
552
			$types = json_decode($types);
553
			$count_dates = count($dates);
554
			if ($poll->getType() === 0) {
555
				$this->participationMapper->deleteByPollAndUser($pollId, $userId);
556
			} else {
557
				$this->participationTextMapper->deleteByPollAndUser($pollId, $userId);
558
			}
559
			for ($i = 0; $i < $count_dates; $i++) {
560
				if ($poll->getType() === 0) {
561
					$part = new Participation();
562
					$part->setPollId($pollId);
563
					$part->setUserId($userId);
564
					$part->setDt(date('Y-m-d H:i:s', $dates[$i]));
565
					$part->setType($types[$i]);
566
					$this->participationMapper->insert($part);
567
				} else {
568
					$part = new ParticipationText();
569
					$part->setPollId($pollId);
570
					$part->setUserId($userId);
571
					$part->setText($dates[$i]);
572
					$part->setType($types[$i]);
573
					$this->participationTextMapper->insert($part);
574
				}
575
576
			}
577
			$this->sendNotifications($pollId, $userId);
578
		}
579
		$hash = $poll->getHash();
580
		$url = $this->urlGenerator->linkToRoute('polls.page.goto_poll', ['hash' => $hash]);
581
		return new RedirectResponse($url);
582
	}
583
584
	/**
585
	 * @NoAdminRequired
586
	 * @NoCSRFRequired
587
	 * @PublicPage
588
	 * @param int $pollId
589
	 * @param string $userId
590
	 * @param string $commentBox
591
	 * @return JSONResponse
592
	 */
593
	public function insertComment($pollId, $userId, $commentBox) {
594
		$comment = new Comment();
595
		$comment->setPollId($pollId);
596
		$comment->setUserId($userId);
597
		$comment->setComment($commentBox);
598
		$comment->setDt(date('Y-m-d H:i:s'));
599
		$this->commentMapper->insert($comment);
600
		$this->sendNotifications($pollId, $userId);
601
		$displayName = $userId;
602
		$user = $this->userMgr->get($userId);
603
		if ($user !== null) {
604
			$displayName = $user->getDisplayName();
605
		}
606
		return new JSONResponse(array(
607
			'comment' => $commentBox,
608
			'date' => date('Y-m-d H:i:s'),
609
			'userId' => $userId,
610
			'displayName' => $displayName
611
		));
612
	}
613
614
	/**
615
	 * @NoAdminRequired
616
	 * @NoCSRFRequired
617
	 * @param string $searchTerm
618
	 * @param string $groups
619
	 * @param string $users
620
	 * @return array
621
	 */
622
	public function search($searchTerm, $groups, $users) {
623
		return array_merge($this->searchForGroups($searchTerm, $groups), $this->searchForUsers($searchTerm, $users));
624
	}
625
626
	/**
627
	 * @NoAdminRequired
628
	 * @NoCSRFRequired
629
	 * @param string $searchTerm
630
	 * @param string $groups
631
	 * @return array
632
	 */
633
	public function searchForGroups($searchTerm, $groups) {
634
		$selectedGroups = json_decode($groups);
635
		$groups = $this->groupManager->search($searchTerm);
636
		$gids = array();
637
		$sgids = array();
638
		foreach ($selectedGroups as $sg) {
639
			$sgids[] = str_replace('group_', '', $sg);
640
		}
641
		foreach ($groups as $g) {
642
			$gids[] = $g->getGID();
643
		}
644
		$diffGids = array_diff($gids, $sgids);
645
		$gids = array();
646
		foreach ($diffGids as $g) {
647
			$gids[] = ['gid' => $g, 'isGroup' => true];
648
		}
649
		return $gids;
650
	}
651
652
	/**
653
	 * @NoAdminRequired
654
	 * @NoCSRFRequired
655
	 * @param string $searchTerm
656
	 * @param string $users
657
	 * @return array
658
	 */
659
	public function searchForUsers($searchTerm, $users) {
660
		$selectedUsers = json_decode($users);
661
		Util::writeLog('polls', print_r($selectedUsers, true), Util::ERROR);
662
		$userNames = $this->userMgr->searchDisplayName($searchTerm);
663
		$users = array();
664
		$sUsers = array();
665
		foreach ($selectedUsers as $su) {
666
			$sUsers[] = str_replace('user_', '', $su);
667
		}
668
		foreach ($userNames as $u) {
669
			$alreadyAdded = false;
670
			foreach ($sUsers as &$su) {
671
				if ($su === $u->getUID()) {
672
					unset($su);
673
					$alreadyAdded = true;
674
					break;
675
				}
676
			}
677
			if (!$alreadyAdded) {
678
				$users[] = array('uid' => $u->getUID(), 'displayName' => $u->getDisplayName(), 'isGroup' => false);
679
			} else {
680
				continue;
681
			}
682
		}
683
		return $users;
684
	}
685
686
	/**
687
	 * @NoAdminRequired
688
	 * @NoCSRFRequired
689
	 * @param string $username
690
	 * @return string
691
	 */
692
	public function getDisplayName($username) {
693
		return $this->userMgr->get($username)->getDisplayName();
694
	}
695
696
	/**
697
	 * @return \OCP\IGroup[]
698
	 */
699
	private function getGroups() {
700
		if (class_exists('\OC_Group')) {
701
			// Nextcloud <= 11, ownCloud
702
			return \OC_Group::getUserGroups($this->userId);
0 ignored issues
show
Bug introduced by
The type OC_Group was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
703
		}
704
		// Nextcloud >= 12
705
		$groups = $this->groupManager->getUserGroups(\OC::$server->getUserSession()->getUser());
706
		return array_map(function ($group) {
707
			return $group->getGID();
708
		}, $groups);
709
	}
710
711
	/**
712
	 * @param Event $poll
713
	 * @return bool
714
	 */
715
	private function hasUserAccess($poll) {
716
		$access = $poll->getAccess();
717
		$owner = $poll->getOwner();
718
		if ($access === 'public' || $access === 'hidden') {
719
			return true;
720
		}
721
		if ($this->userId === null) {
722
			return false;
723
		}
724
		if ($access === 'registered') {
725
			return true;
726
		}
727
		if ($owner === $this->userId) {
728
			return true;
729
		}
730
		Util::writeLog('polls', $this->userId, Util::ERROR);
731
		$userGroups = $this->getGroups();
732
		$arr = explode(';', $access);
733
		foreach ($arr as $item) {
734
			if (strpos($item, 'group_') === 0) {
735
				$grp = substr($item, 6);
736
				foreach ($userGroups as $userGroup) {
737
					if ($userGroup === $grp) {
738
						return true;
739
					}
740
				}
741
			} else {
742
				if (strpos($item, 'user_') === 0) {
743
					$usr = substr($item, 5);
744
					if ($usr === $this->userId) {
745
						return true;
746
					}
747
				}
748
			}
749
		}
750
		return false;
751
	}
752
}
753