Completed
Branch vue-create (f8dd48)
by René
04:49
created

PageController::insertPoll()   D

Complexity

Conditions 16
Paths 56

Size

Total Lines 82
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 272

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 16
eloc 54
c 2
b 0
f 0
nc 56
nop 10
dl 0
loc 82
ccs 0
cts 52
cp 0
crap 272
rs 4.9422

How to fix   Long Method    Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\Event;
29
use OCA\Polls\Db\EventMapper;
30
use OCA\Polls\Db\Notification;
31
use OCA\Polls\Db\NotificationMapper;
32
use OCA\Polls\Db\Options;
33
use OCA\Polls\Db\OptionsMapper;
34
use OCA\Polls\Db\Votes;
35
use OCA\Polls\Db\VotesMapper;
36
use OCP\AppFramework\Controller;
37
use OCP\AppFramework\Db\DoesNotExistException;
38
use OCP\AppFramework\Http\ContentSecurityPolicy;
39
use OCP\AppFramework\Http\JSONResponse;
40
use OCP\AppFramework\Http\RedirectResponse;
41
use OCP\AppFramework\Http\TemplateResponse;
42
use OCP\IAvatarManager;
43
use OCP\IGroupManager;
44
use OCP\IL10N;
45
use OCP\ILogger;
46
use OCP\IRequest;
47
use OCP\IURLGenerator;
48
use OCP\IUserManager;
49
use OCP\Mail\IMailer;
50
use OCP\Security\ISecureRandom;
51
use OCP\User; //To do: replace according to API
52
use OCP\Util;
53
54
class PageController extends Controller {
55
56
	private $userId;
57
	private $commentMapper;
58
	private $eventMapper;
59
	private $notificationMapper;
60
	private $optionsMapper;
61
	private $votesMapper;
62
	private $urlGenerator;
63
	private $userMgr;
64
	private $avatarManager;
65
	private $logger;
66
	private $trans;
67
	private $groupManager;
68
69
	/**
70
	 * PageController constructor.
71
	 * @param string $appName
72
	 * @param IRequest $request
73
	 * @param IUserManager $userMgr
74
	 * @param IGroupManager $groupManager
75
	 * @param IAvatarManager $avatarManager
76
	 * @param ILogger $logger
77
	 * @param IL10N $trans
78
	 * @param IURLGenerator $urlGenerator
79
	 * @param string $userId
80
	 * @param CommentMapper $commentMapper
81
	 * @param EventMapper $eventMapper
82
	 * @param NotificationMapper $notificationMapper
83
	 * @param OptionsMapper $optionsMapper
84
	 * @param VotesMapper $VotesMapper
85
	 */
86 1
	public function __construct(
87
		$appName,
88
		IRequest $request,
89
		IUserManager $userMgr,
90
		IGroupManager $groupManager,
91
		IAvatarManager $avatarManager,
92
		ILogger $logger,
93
		IL10N $trans,
94
		IURLGenerator $urlGenerator,
95
		$userId,
96
		CommentMapper $commentMapper,
97
		OptionsMapper $optionsMapper,
98
		EventMapper $eventMapper,
99
		NotificationMapper $notificationMapper,
100
		VotesMapper $VotesMapper
101
	) {
102 1
		parent::__construct($appName, $request);
103 1
		$this->userMgr = $userMgr;
104 1
		$this->groupManager = $groupManager;
105 1
		$this->avatarManager = $avatarManager;
106 1
		$this->logger = $logger;
107 1
		$this->trans = $trans;
108 1
		$this->urlGenerator = $urlGenerator;
109 1
		$this->userId = $userId;
110 1
		$this->commentMapper = $commentMapper;
111 1
		$this->eventMapper = $eventMapper;
112 1
		$this->notificationMapper = $notificationMapper;
113 1
		$this->optionsMapper = $optionsMapper;
114 1
		$this->votesMapper = $VotesMapper;
115 1
	}
116
117
	/**
118
	 * @NoAdminRequired
119
	 * @NoCSRFRequired
120
	 */
121 1
	public function index() {
122 1
		$polls = $this->eventMapper->findAllForUserWithInfo($this->userId);
123 1
		$comments = $this->commentMapper->findDistinctByUser($this->userId);
124 1
		$votes = $this->votesMapper->findDistinctByUser($this->userId);
125 1
		$response = new TemplateResponse('polls', 'main.tmpl', [
126 1
			'polls' => $polls,
127 1
			'comments' => $comments,
128 1
			'votes' => $votes,
129 1
			'userId' => $this->userId,
130 1
			'userMgr' => $this->userMgr,
131 1
			'urlGenerator' => $this->urlGenerator
132
		]);
133 1
		$csp = new ContentSecurityPolicy();
134 1
		$response->setContentSecurityPolicy($csp);
135 1
		return $response;
136
	}
137
138
	/**
139
	 * @param int $pollId
140
	 * @param string $from
141
	 */
142
	private function sendNotifications($pollId, $from) {
143
		$poll = $this->eventMapper->find($pollId);
144
		$notifications = $this->notificationMapper->findAllByPoll($pollId);
145
		foreach ($notifications as $notification) {
146
			if ($from === $notification->getUserId()) {
147
				continue;
148
			}
149
			$email = \OC::$server->getConfig()->getUserValue($notification->getUserId(), 'settings', 'email');
150
			if ($email === null || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
151
				continue;
152
			}
153
			$url = $this->urlGenerator->getAbsoluteURL(
154
				$this->urlGenerator->linkToRoute('polls.page.goto_poll',
155
					array('hash' => $poll->getHash()))
156
			);
157
158
			$recUser = $this->userMgr->get($notification->getUserId());
159
			$sendUser = $this->userMgr->get($from);
160
			$rec = '';
161
			if ($recUser !== null) {
162
				$rec = $recUser->getDisplayName();
163
			}
164
			$sender = $from;
165
			if ($sendUser !== null) {
166
				$sender = $sendUser->getDisplayName();
167
			}
168
			$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>',
169
				array(
170
					$rec,
171
					$sender,
172
					$poll->getTitle(),
173
					$url
174
				));
175
176
			$msg .= '<br/><br/>';
177
178
			$toName = $this->userMgr->get($notification->getUserId())->getDisplayName();
179
			$subject = $this->trans->t('Polls App - New Activity');
180
			$fromAddress = Util::getDefaultEmailAddress('no-reply');
181
			$fromName = $this->trans->t('Polls App') . ' (' . $from . ')';
182
183
			try {
184
				/** @var IMailer $mailer */
185
				$mailer = \OC::$server->getMailer();
186
				/** @var \OC\Mail\Message $message */
187
				$message = $mailer->createMessage();
188
				$message->setSubject($subject);
189
				$message->setFrom(array($fromAddress => $fromName));
190
				$message->setTo(array($email => $toName));
191
				$message->setHtmlBody($msg);
192
				$mailer->send($message);
193
			} catch (\Exception $e) {
194
				$message = 'Error sending mail to: ' . $toName . ' (' . $email . ')';
195
				Util::writeLog('polls', $message, Util::ERROR);
196
			}
197
		}
198
	}
199
200
	/**
201
	 * @NoAdminRequired
202
	 * @NoCSRFRequired
203
	 * @PublicPage
204
	 * @param string $hash
205
	 * @return TemplateResponse
206
	 */
207
	public function gotoPoll($hash) {
208
		try {
209
			$poll = $this->eventMapper->findByHash($hash);
210
		} catch (DoesNotExistException $e) {
211
			return new TemplateResponse('polls', 'no.acc.tmpl', []);
212
		}
213
		$options = $this->optionsMapper->findByPoll($poll->getId());
214
		$votes = $this->votesMapper->findByPoll($poll->getId());
215
		$participants = $this->votesMapper->findParticipantsByPoll($poll->getId());
216
		$comments = $this->commentMapper->findByPoll($poll->getId());
217
218
		try {
219
			$notification = $this->notificationMapper->findByUserAndPoll($poll->getId(), $this->userId);
220
		} catch (DoesNotExistException $e) {
221
			$notification = null;
222
		}
223
		if ($this->hasUserAccess($poll)) {
224
			return new TemplateResponse('polls', 'goto.tmpl', [
225
				'poll' => $poll,
226
				'options' => $options,
227
				'comments' => $comments,
228
				'votes' => $votes,
229
				'participant' => $participants,
230
				'notification' => $notification,
231
				'userId' => $this->userId,
232
				'userMgr' => $this->userMgr,
233
				'urlGenerator' => $this->urlGenerator,
234
				'avatarManager' => $this->avatarManager
235
			]);
236
		} else {
237
			User::checkLoggedIn();
238
			return new TemplateResponse('polls', 'no.acc.tmpl', []);
239
		}
240
	}
241
242
	/**
243
	 * @NoAdminRequired
244
	 * @NoCSRFRequired
245
	 * @param int $pollId
246
	 * @return TemplateResponse|RedirectResponse
247
	 */
248
	public function deletePoll($pollId) {
249
		$pollToDelete = $this->eventMapper->find($pollId);
250
		if ($this->userId !== $pollToDelete->getOwner()) {
251
			return new TemplateResponse('polls', 'no.delete.tmpl');
252
		}
253
		$poll = new Event();
254
		$poll->setId($pollId);
255
		$this->eventMapper->delete($poll);
256
		$this->optionsMapper->deleteByPoll($pollId);
257
		$this->votesMapper->deleteByPoll($pollId);
258
		$this->commentMapper->deleteByPoll($pollId);
259
		$url = $this->urlGenerator->linkToRoute('polls.page.index');
260
		return new RedirectResponse($url);
261
	}
262
263
	/**
264
	 * @NoAdminRequired
265
	 * @NoCSRFRequired
266
	 * @param string $hash
267
	 * @return TemplateResponse
268
	 */
269
	public function editPoll($hash) {
270
		return new TemplateResponse('polls', 'create.tmpl', [
271
			'urlGenerator' => $this->urlGenerator,
272
 			'hash' => $hash
273
		]);
274
	}
275
276
	/**
277
	 * @NoAdminRequired
278
	 * @NoCSRFRequired
279
	 * @param int $pollId
280
	 * @param string $pollType
281
	 * @param string $pollTitle
282
	 * @param string $pollDesc
283
	 * @param string $userId
284
	 * @param string $chosenOptions
285
	 * @param int $expireTs
286
	 * @param string $accessType
287
	 * @param string $accessValues
288
	 * @param bool $isAnonymous
289
	 * @param bool $hideNames
290
	 * @return RedirectResponse
291
	 */
292
	public function updatePoll(
293
		$pollId,
294
		$pollType,
295
		$pollTitle,
296
		$pollDesc,
297
		$chosenOptions,
298
		$expireTs,
299
		$accessType,
300
		$accessValues,
301
		$isAnonymous,
302
		$hideNames
303
	) {
304
		$event = $this->eventMapper->find($pollId);
305
		$event->setTitle($pollTitle);
306
		$event->setDescription($pollDesc);
307
		$event->setIsAnonymous($isAnonymous ? 1 : 0);
308
		$event->setFullAnonymous($isAnonymous && $hideNames ? 1 : 0);
309
310
		if ($accessType === 'select') {
311
			if (isset($accessValues)) {
312
				$accessValues = json_decode($accessValues);
313
				if ($accessValues !== null) {
314
					$groups = array();
315
					$users = array();
316
					if ($accessValues->groups !== null) {
317
						$groups = $accessValues->groups;
318
					}
319
					if ($accessValues->users !== null) {
320
						$users = $accessValues->users;
321
					}
322
					$accessType = '';
323
					foreach ($groups as $gid) {
324
						$accessType .= $gid . ';';
325
					}
326
					foreach ($users as $uid) {
327
						$accessType .= $uid . ';';
328
					}
329
				}
330
			}
331
		}
332
		$event->setAccess($accessType);
333
		/** @var string[] $optionsArray */
334
		$optionsArray = json_decode($chosenOptions, true);
335
336
		$expire = null;
337
		if ($expireTs !== 0 && $expireTs !== '') {
338
			$expire = date('Y-m-d H:i:s', $expireTs);
339
		}
340
		$event->setExpire($expire);
341
342
		$this->optionsMapper->deleteByPoll($pollId);
343
		if ($pollType === 'event') {
344
			$event->setType(0);
345
			$this->eventMapper->update($event);
346
			sort($optionsArray);
347
			foreach ($optionsArray as $optionElement) {
348
				$option = new Options();
349
				$option->setPollId($pollId);
350
				$option->setPollOptionText(date('Y-m-d H:i:s', (int) $optionElement));
351
				$this->optionsMapper->insert($option);
352
			}
353
		} else {
354
			$event->setType(1);
355
			$this->eventMapper->update($event);
356
			foreach ($optionsArray as $optionElement) {
357
				$option = new Options();
358
				$option->setPollId($pollId);
359
				$option->setpollOptionText($optionElement);
360
				$this->optionsMapper->insert($option);
361
			}
362
		}
363
		$url = $this->urlGenerator->linkToRoute('polls.page.index');
364
		return new RedirectResponse($url);
365
	}
366
367
	/**
368
	 * @NoAdminRequired
369
	 * @NoCSRFRequired
370
	 */
371
	public function createPoll() {
372
		return new TemplateResponse('polls', 'create.tmpl',
373
			['urlGenerator' => $this->urlGenerator]);
374
	}
375
376
	/**
377
	 * @NoAdminRequired
378
	 * @NoCSRFRequired
379
	 * @param string $pollType
380
	 * @param string $pollTitle
381
	 * @param string $pollDesc
382
	 * @param string $userId
383
	 * @param string $chosenOptions
384
	 * @param int $expireTs
385
	 * @param string $accessType
386
	 * @param string $accessValues
387
	 * @param bool $isAnonymous
388
	 * @param bool $hideNames
389
	 * @return RedirectResponse
390
	 */
391
	public function insertPoll(
392
		$pollType,
393
		$pollTitle,
394
		$pollDesc,
395
		$userId,
396
		$chosenOptions,
397
		$expireTs,
398
		$accessType,
399
		$accessValues,
400
		$isAnonymous,
401
		$hideNames
402
	) {
403
		$event = new Event();
404
		$event->setTitle($pollTitle);
405
		$event->setDescription($pollDesc);
406
		$event->setOwner($userId);
407
		$event->setCreated(date('Y-m-d H:i:s'));
408
		$event->setHash(\OC::$server->getSecureRandom()->generate(
409
			16,
410
			ISecureRandom::CHAR_DIGITS .
411
			ISecureRandom::CHAR_LOWER .
412
			ISecureRandom::CHAR_UPPER
413
		));
414
		$event->setIsAnonymous($isAnonymous ? 1 : 0);
415
		$event->setFullAnonymous($isAnonymous && $hideNames ? 1 : 0);
416
417
		if ($accessType === 'select') {
418
			if (isset($accessValues)) {
419
				$accessValues = json_decode($accessValues);
420
				if ($accessValues !== null) {
421
					$groups = array();
422
					$users = array();
423
					if ($accessValues->groups !== null) {
424
						$groups = $accessValues->groups;
425
					}
426
					if ($accessValues->users !== null) {
427
						$users = $accessValues->users;
428
					}
429
					$accessType = '';
430
					foreach ($groups as $gid) {
431
						$accessType .= $gid . ';';
432
					}
433
					foreach ($users as $uid) {
434
						$accessType .= $uid . ';';
435
					}
436
				}
437
			}
438
		}
439
		$event->setAccess($accessType);
440
		/** @var string[] $optionsArray */
441
		$optionsArray = json_decode($chosenOptions, true);
442
443
		$expire = null;
444
		if ($expireTs !== 0 && $expireTs !== '') {
445
			$expire = date('Y-m-d H:i:s', $expireTs);
446
		}
447
		$event->setExpire($expire);
448
449
		if ($pollType === 'event') {
450
			$event->setType(0);
451
			$ins = $this->eventMapper->insert($event);
452
			$pollId = $ins->getId();
453
			sort($optionsArray);
454
			foreach ($optionsArray as $optionElement) {
455
				$option = new Options();
456
				$option->setPollId($pollId);
457
				$option->setPollOptionText(date('Y-m-d H:i:s', (int) $optionElement));
458
				$this->optionsMapper->insert($option);
459
			}
460
		} else {
461
			$event->setType(1);
462
			$ins = $this->eventMapper->insert($event);
463
			$pollId = $ins->getId();
464
			foreach ($optionsArray as $optionElement) {
465
				$option = new Options();
466
				$option->setPollId($pollId);
467
				$option->setpollOptionText($optionElement);
468
				$this->optionsMapper->insert($option);
469
			}
470
		}
471
		$url = $this->urlGenerator->linkToRoute('polls.page.index');
472
		return new RedirectResponse($url);
473
	}
474
475
	/**
476
	 * @NoAdminRequired
477
	 * @NoCSRFRequired
478
	 * @PublicPage
479
	 * @param int $pollId
480
	 * @param string $userId
481
	 * @param string $answers
482
	 * @param string $options
483
	 * @param bool $receiveNotifications
484
	 * @param bool $changed
485
	 * @return RedirectResponse
486
	 */
487
	public function insertVote($pollId, $userId, $answers, $options, $receiveNotifications, $changed) {
488
		if ($this->userId !== null) {
489
			if ($receiveNotifications) {
490
				try {
491
					//check if user already set notification for this poll
492
					$this->notificationMapper->findByUserAndPoll($pollId, $userId);
493
				} catch (DoesNotExistException $e) {
494
					//insert if not exist
495
					$not = new Notification();
496
					$not->setUserId($userId);
497
					$not->setPollId($pollId);
498
					$this->notificationMapper->insert($not);
499
				}
500
			} else {
501
				try {
502
					//delete if entry is in db
503
					$not = $this->notificationMapper->findByUserAndPoll($pollId, $userId);
504
					$this->notificationMapper->delete($not);
505
				} catch (DoesNotExistException $e) {
506
					//doesn't exist in db, nothing to do
507
				}
508
			}
509
		}
510
		$poll = $this->eventMapper->find($pollId);
511
		
512
		if ($changed) {
513
			$options = json_decode($options);
514
			$answers = json_decode($answers);
515
			$count_options = count($options);
516
			$this->votesMapper->deleteByPollAndUser($pollId, $userId);
517
			
518
			for ($i = 0; $i < $count_options; $i++) {
519
				$vote = new Votes();
520
				$vote->setPollId($pollId);
521
				$vote->setUserId($userId);
522
				$vote->setVoteOptionText($options[$i]);
523
				$vote->setVoteAnswer($answers[$i]);
524
				$this->votesMapper->insert($vote);
525
526
			}
527
			$this->sendNotifications($pollId, $userId);
528
		}
529
		$hash = $poll->getHash();
530
		$url = $this->urlGenerator->linkToRoute('polls.page.goto_poll', ['hash' => $hash]);
531
		return new RedirectResponse($url);
532
	}
533
534
	/**
535
	 * @NoAdminRequired
536
	 * @NoCSRFRequired
537
	 * @PublicPage
538
	 * @param int $pollId
539
	 * @param string $userId
540
	 * @param string $commentBox
541
	 * @return JSONResponse
542
	 */
543
	public function insertComment($pollId, $userId, $commentBox) {
544
		$comment = new Comment();
545
		$comment->setPollId($pollId);
546
		$comment->setUserId($userId);
547
		$comment->setComment($commentBox);
548
		$comment->setDt(date('Y-m-d H:i:s'));
549
		$this->commentMapper->insert($comment);
550
		$this->sendNotifications($pollId, $userId);
551
		$timeStamp = time();
552
		$displayName = $userId;
553
		$user = $this->userMgr->get($userId);
554
		if ($user !== null) {
555
			$displayName = $user->getDisplayName();
556
		}
557
		return new JSONResponse(array(
558
			'userId' => $userId,
559
			'displayName' => $displayName,
560
			'timeStamp' => $timeStamp * 100, 
561
			'date' => date('Y-m-d H:i:s', $timeStamp),
562
			'relativeNow' => $this->trans->t('just now'),
563
			'comment' => $commentBox
564
		));
565
	}
566
567
	/**
568
	 * @NoAdminRequired
569
	 * @NoCSRFRequired
570
	 * @param string $searchTerm
571
	 * @param string $groups
572
	 * @param string $users
573
	 * @return array
574
	 */
575
	public function search($searchTerm, $groups, $users) {
576
		return array_merge($this->searchForGroups($searchTerm, $groups), $this->searchForUsers($searchTerm, $users));
577
	}
578
579
	/**
580
	 * @NoAdminRequired
581
	 * @NoCSRFRequired
582
	 * @param string $searchTerm
583
	 * @param string $groups
584
	 * @return array
585
	 */
586
	public function searchForGroups($searchTerm, $groups) {
587
		$selectedGroups = json_decode($groups);
588
		$groups = $this->groupManager->search($searchTerm);
589
		$gids = array();
590
		$sgids = array();
591
		foreach ($selectedGroups as $sg) {
592
			$sgids[] = str_replace('group_', '', $sg);
593
		}
594
		foreach ($groups as $g) {
595
			$gids[] = $g->getGID();
596
		}
597
		$diffGids = array_diff($gids, $sgids);
598
		$gids = array();
599
		foreach ($diffGids as $g) {
600
			$gids[] = ['gid' => $g, 'isGroup' => true];
601
		}
602
		return $gids;
603
	}
604
605
	/**
606
	 * @NoAdminRequired
607
	 * @NoCSRFRequired
608
	 * @param string $searchTerm
609
	 * @param string $users
610
	 * @return array
611
	 */
612
	public function searchForUsers($searchTerm, $users) {
613
		$selectedUsers = json_decode($users);
614
		Util::writeLog('polls', print_r($selectedUsers, true), Util::ERROR);
615
		$userNames = $this->userMgr->searchDisplayName($searchTerm);
616
		$users = array();
617
		$sUsers = array();
618
		foreach ($selectedUsers as $su) {
619
			$sUsers[] = str_replace('user_', '', $su);
620
		}
621
		foreach ($userNames as $u) {
622
			$allreadyAdded = false;
623
			foreach ($sUsers as &$su) {
624
				if ($su === $u->getUID()) {
625
					unset($su);
626
					$allreadyAdded = true;
627
					break;
628
				}
629
			}
630
			if (!$allreadyAdded) {
631
				$users[] = array('uid' => $u->getUID(), 'displayName' => $u->getDisplayName(), 'isGroup' => false);
632
			} else {
633
				continue;
634
			}
635
		}
636
		return $users;
637
	}
638
639
	/**
640
	 * @NoAdminRequired
641
	 * @NoCSRFRequired
642
	 * @param string $username
643
	 * @return string
644
	 */
645
	public function getDisplayName($username) {
646
		return $this->userMgr->get($username)->getDisplayName();
647
	}
648
649
	/**
650
	 * @return \OCP\IGroup[]
651
	 */
652
	private function getGroups() {
653
		if (class_exists('\OC_Group')) {
654
			// Nextcloud <= 11, ownCloud
655
			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...
656
		}
657
		// Nextcloud >= 12
658
		$groups = $this->groupManager->getUserGroups(\OC::$server->getUserSession()->getUser());
659
		return array_map(function($group) {
660
			return $group->getGID();
661
		}, $groups);
662
	}
663
664
	/**
665
	 * Check if user has access to this poll
666
	 *
667
	 * @param Event $poll
668
	 * @return bool
669
	 */
670
	private function hasUserAccess($poll) {
671
		$access = $poll->getAccess();
672
		$owner = $poll->getOwner();
673
		if ($access === 'public' || $access === 'hidden') {
674
			return true;
675
		}
676
		if ($this->userId === null) {
677
			return false;
678
		}
679
		if ($access === 'registered') {
680
			return true;
681
		}
682
		if ($owner === $this->userId) {
683
			return true;
684
		}
685
		Util::writeLog('polls', $this->userId, Util::ERROR);
686
		$userGroups = $this->getGroups();
687
		$arr = explode(';', $access);
688
		foreach ($arr as $item) {
689
			if (strpos($item, 'group_') === 0) {
690
				$grp = substr($item, 6);
691
				foreach ($userGroups as $userGroup) {
692
					if ($userGroup === $grp) {
693
						return true;
694
					}
695
				}
696
			} else {
697
				if (strpos($item, 'user_') === 0) {
698
					$usr = substr($item, 5);
699
					if ($usr === $this->userId) {
700
						return true;
701
					}
702
				}
703
			}
704
		}
705
		return false;
706
	}
707
	/**
708
	 * Check if user is owner of this poll
709
	 *
710
	 * @param Event $poll
711
	 * @return bool
712
	 */
713
714
	private function userIsOwner($poll) {
0 ignored issues
show
Unused Code introduced by
The method userIsOwner() 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...
715
		$owner = $poll->getOwner();
716
717
		if ($owner === $this->userId) {
718
			return true;
719
		}
720
		Util::writeLog('polls', $this->userId, Util::ERROR);
721
		return false;
722
	}
723
}
724