-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathPoll.php
More file actions
915 lines (804 loc) · 28.7 KB
/
Poll.php
File metadata and controls
915 lines (804 loc) · 28.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2017 Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Polls\Db;
use JsonSerializable;
use OCA\Polls\AppConstants;
use OCA\Polls\Exceptions\ForbiddenException;
use OCA\Polls\Exceptions\NoDeadLineException;
use OCA\Polls\Helper\Container;
use OCA\Polls\Model\Settings\AppSettings;
use OCA\Polls\Model\Settings\SystemSettings;
use OCA\Polls\UserSession;
use OCP\IURLGenerator;
/**
* @psalm-api
* @method int getId()
* @method void setId(int $value)
* @method string getType()
* @method void setType(string $value)
* @method string getTitle()
* @method void setTitle(string $value)
* @method void setDescription(string $value)
* @method string getOwner()
* @method void setOwner(string $value)
* @method int getCreated()
* @method void setCreated(int $value)
* @method int getExpire()
* @method void setExpire(int $value)
* @method int getDeleted()
* @method void setDeleted(int $value)
* @method void setAccess(string $value)
* @method int getAnonymous()
* @method void setAnonymous(int $value)
* @method int getAllowComment()
* @method void setAllowComment(int $value)
* @method int getAllowMaybe()
* @method void setAllowMaybe(int $value)
* @method string getAllowProposals()
* @method void setAllowProposals(string $value)
* @method int getProposalsExpire()
* @method void setProposalsExpire(int $value)
* @method int getVoteLimit()
* @method void setVoteLimit(int $value)
* @method int getOptionLimit()
* @method void setOptionLimit(int $value)
* @method string getShowResults()
* @method void setShowResults(string $value)
* @method int getAdminAccess()
* @method void setAdminAccess(int $value)
* @method int getHideBookedUp()
* @method void setHideBookedUp(int $value)
* @method int getUseNo()
* @method void setUseNo(int $value)
* @method int getLastInteraction()
* @method void setLastInteraction(int $value)
* @method string getMiscSettings()
* @method void setMiscSettings(string $value)
* @method string getVotingVariant()
* @method void setVotingVariant(string $value)
*
* Magic functions for joined columns
* @method int getMinDate()
* @method int getMaxDate()
* @method int getShareToken()
* @method int getOptionsCount()
* @method int getProposalsCount()
*
* Magic functions for subqueried columns
* @method int getCurrentUserOrphanedVotes()
* @method int getCurrentUserVotes()
* @method int getCurrentUserVotesYes()
* @method int getParticipantsCount()
*/
class Poll extends EntityWithUser implements JsonSerializable {
public const TABLE = 'polls_polls';
public const TYPE_DATE = 'datePoll';
public const TYPE_TEXT = 'textPoll';
public const VARIANT_SIMPLE = 'simple';
public const ACCESS_HIDDEN = 'hidden';
public const ACCESS_PUBLIC = 'public';
public const ACCESS_PRIVATE = 'private';
public const ACCESS_OPEN = 'open';
public const SHOW_RESULTS_ALWAYS = 'always';
public const SHOW_RESULTS_CLOSED = 'closed';
public const SHOW_RESULTS_NEVER = 'never';
public const PROPOSAL_DISALLOW = 'disallow';
public const PROPOSAL_ALLOW = 'allow';
public const PROPOSAL_REVIEW = 'review';
public const URI_PREFIX = 'poll/';
public const FIVE_DAYS = 432000;
public const FOUR_DAYS = 345600;
public const THREE_DAYS = 259200;
public const TWO_DAYS = 172800;
public const ONE_AND_HALF_DAY = 129600;
public const ROLE_USER = Share::TYPE_USER;
public const ROLE_ADMIN = Share::TYPE_ADMIN;
public const ROLE_EMAIL = Share::TYPE_EMAIL;
public const ROLE_CONTACT = Share::TYPE_CONTACT;
public const ROLE_EXTERNAL = Share::TYPE_EXTERNAL;
public const ROLE_OWNER = 'owner';
public const ROLE_NONE = 'none';
public const PERMISSION_OVERRIDE = 'override_permission';
public const PERMISSION_POLL_VIEW = 'view';
public const PERMISSION_POLL_EDIT = 'edit';
public const PERMISSION_POLL_CHANGE_OWNER = 'changeOwner';
public const PERMISSION_POLL_DELETE = 'delete';
public const PERMISSION_POLL_ARCHIVE = 'archive';
public const PERMISSION_POLL_RESULTS_VIEW = 'seeResults';
public const PERMISSION_POLL_USERNAMES_VIEW = 'seeUserNames';
public const PERMISSION_POLL_TAKEOVER = 'takeOver';
public const PERMISSION_POLL_SUBSCRIBE = 'subscribe';
public const PERMISSION_COMMENT_ADD = 'addComment';
public const PERMISSION_COMMENT_DELETE = 'deleteComment';
public const PERMISSION_OPTION_ADD = 'addOptions';
public const PERMISSION_OPTION_CONFIRM = 'confirmOption';
public const PERMISSION_OPTION_CLONE = 'cloneOption';
public const PERMISSION_OPTION_DELETE = 'deleteOption';
public const PERMISSION_OPTIONS_REORDER = 'reorderOptions';
public const PERMISSION_OPTIONS_SHIFT = 'shiftOptions';
public const PERMISSION_VOTE_EDIT = 'vote';
public const PERMISSION_VOTE_FOREIGN_CHANGE = 'changeForeignVotes';
public const PERMISSION_SHARE_ADD = 'shareCreate';
public const PERMISSION_SHARE_ADD_EXTERNAL = 'shareCreateExternal';
public const PERMISSION_DEANONYMIZE = 'deanonymize';
private IURLGenerator $urlGenerator;
protected SystemSettings $systemSettings;
protected AppSettings $appSettings;
protected UserSession $userSession;
// schema columns
public $id = null;
protected string $type = '';
protected string $title = '';
protected ?string $description = '';
protected ?string $owner = '';
protected int $created = 0;
protected int $expire = 0;
protected int $deleted = 0;
protected string $access = '';
protected int $anonymous = 0;
protected int $allowMaybe = 0;
protected string $allowProposals = '';
protected int $proposalsExpire = 0;
protected int $voteLimit = 0;
protected int $optionLimit = 0;
protected string $showResults = '';
protected int $adminAccess = 0;
protected int $allowComment = 0;
protected int $hideBookedUp = 0;
protected int $useNo = 0;
protected int $lastInteraction = 0;
protected ?string $miscSettings = '';
protected string $votingVariant = '';
// joined columns
protected ?int $isCurrentUserLocked = 0;
protected int $maxDate = 0;
protected int $minDate = 0;
protected string $userRole = self::ROLE_NONE;
protected string $shareToken = '';
protected ?string $groupShares = '';
protected int $optionsCount = 0;
protected int $proposalsCount = 0;
// subqueried columns
protected int $currentUserOrphanedVotes = 0;
protected int $currentUserVotes = 0;
protected int $currentUserVotesYes = 0;
protected int $participantsCount = 0;
public function __construct() {
$this->addType('created', 'integer');
$this->addType('expire', 'integer');
$this->addType('deleted', 'integer');
$this->addType('anonymous', 'integer');
$this->addType('allowComment', 'integer');
$this->addType('allowMaybe', 'integer');
$this->addType('proposalsExpire', 'integer');
$this->addType('voteLimit', 'integer');
$this->addType('optionLimit', 'integer');
$this->addType('adminAccess', 'integer');
$this->addType('hideBookedUp', 'integer');
$this->addType('useNo', 'integer');
$this->addType('lastInteraction', 'integer');
// joined columns
$this->addType('isCurrentUserLocked', 'integer');
$this->addType('maxDate', 'integer');
$this->addType('minDate', 'integer');
$this->addType('countOptions', 'integer');
// subqueried columns
$this->addType('currentUserVotes', 'integer');
$this->addType('currentUserVotesYes', 'integer');
$this->addType('currentUserOrphanedVotes', 'integer');
$this->addType('participantsCount', 'integer');
$this->urlGenerator = Container::queryClass(IURLGenerator::class);
$this->systemSettings = Container::queryClass(SystemSettings::class);
$this->appSettings = Container::queryClass(AppSettings::class);
$this->userSession = Container::queryClass(UserSession::class);
}
/**
* @return array
*
* @psalm-suppress PossiblyUnusedMethod
*/
public function jsonSerialize(): array {
return [
'id' => $this->getId(),
'type' => $this->getType(),
'votingVariant' => $this->getVotingVariant(),
// editable settings
'configuration' => $this->getConfigurationArray(),
// read only properties
'descriptionSafe' => $this->getDescriptionSafe(),
'owner' => $this->getUser(),
'status' => $this->getStatusArray(),
'currentUserStatus' => $this->getCurrentUserStatus(),
'permissions' => $this->getPermissionsArray(),
];
}
public function getStatusArray(): array {
return [
'lastInteraction' => $this->getLastInteraction(),
'created' => $this->getCreated(),
'isAnonymous' => boolval($this->getAnonymous()),
'isArchived' => boolval($this->getDeleted()),
'isExpired' => $this->getExpired(),
'isRealAnonymous' => $this->getAnonymous() < 0,
'relevantThreshold' => $this->getRelevantThreshold(),
'deletionDate' => $this->getDeletionDate(),
'archivedDate' => $this->getDeleted(),
'countOptions' => $this->getOptionsCount(),
'countParticipants' => $this->getIsAllowed(self::PERMISSION_POLL_RESULTS_VIEW) ? $this->getParticipantsCount() : 0,
'countProposals' => $this->getIsAllowed(self::PERMISSION_POLL_RESULTS_VIEW) ? $this->getProposalsCount() : 0,
];
}
public function getConfigurationArray(): array {
return [
'access' => $this->getAccess(),
'allowComment' => boolval($this->getAllowComment()),
'allowMaybe' => boolval($this->getAllowMaybe()),
'allowProposals' => $this->getAllowProposals(),
'anonymous' => boolval($this->getAnonymous()),
'autoReminder' => $this->getAutoReminder(),
'description' => $this->getDescription(),
'expire' => $this->getExpire(),
'hideBookedUp' => boolval($this->getHideBookedUp()),
'maxVotesPerOption' => $this->getOptionLimit(),
'maxVotesPerUser' => $this->getVoteLimit(),
'proposalsExpire' => $this->getProposalsExpire(),
'showResults' => $this->getShowResults(),
'title' => $this->getTitle(),
'useNo' => boolval($this->getUseNo()),
];
}
public function getCurrentUserStatus(): array {
return [
'countVotes' => $this->getCurrentUserVotes(),
'groupInvitations' => $this->getGroupShares(),
'isInvolved' => $this->getIsInvolved(),
'isLocked' => boolval($this->getIsCurrentUserLocked()),
'isLoggedIn' => $this->userSession->getIsLoggedIn(),
'isNoUser' => !$this->userSession->getIsLoggedIn(),
'isOwner' => $this->getIsPollOwner(),
'orphanedVotes' => $this->getCurrentUserOrphanedVotes(),
'shareToken' => $this->getShareToken(),
'userId' => $this->userSession->getCurrentUserId(),
'userRole' => $this->getUserRole(),
'yesVotes' => $this->getCurrentUserVotesYes(),
];
}
public function getPermissionsArray(): array {
return [
'addOptions' => $this->getIsAllowed(self::PERMISSION_OPTION_ADD),
'addShares' => $this->getIsAllowed(self::PERMISSION_SHARE_ADD),
'addSharesExternal' => $this->getIsAllowed(self::PERMISSION_SHARE_ADD_EXTERNAL),
'archive' => $this->getIsAllowed(self::PERMISSION_POLL_ARCHIVE),
'changeForeignVotes' => $this->getIsAllowed(self::PERMISSION_VOTE_FOREIGN_CHANGE),
'changeOwner' => $this->getIsAllowed(self::PERMISSION_POLL_CHANGE_OWNER),
'clone' => $this->getIsAllowed(self::PERMISSION_OPTION_CLONE),
'comment' => $this->getIsAllowed(self::PERMISSION_COMMENT_ADD),
'confirmOptions' => $this->getIsAllowed(self::PERMISSION_OPTION_CONFIRM),
'deanonymize' => $this->getIsAllowed(self::PERMISSION_DEANONYMIZE),
'delete' => $this->getIsAllowed(self::PERMISSION_POLL_DELETE),
'edit' => $this->getIsAllowed(self::PERMISSION_POLL_EDIT),
'reorderOptions' => $this->getIsAllowed(self::PERMISSION_OPTIONS_REORDER),
'seeResults' => $this->getIsAllowed(self::PERMISSION_POLL_RESULTS_VIEW),
'seeUsernames' => $this->getIsAllowed(self::PERMISSION_POLL_USERNAMES_VIEW),
'shiftOptions' => $this->getIsAllowed(self::PERMISSION_OPTIONS_SHIFT),
'subscribe' => $this->getIsAllowed(self::PERMISSION_POLL_SUBSCRIBE),
'takeOver' => $this->getIsAllowed(self::PERMISSION_POLL_TAKEOVER),
'view' => $this->getIsAllowed(self::PERMISSION_POLL_VIEW),
'vote' => $this->getIsAllowed(self::PERMISSION_VOTE_EDIT),
];
}
/**
* @return static
*/
public function deserializeArray(array $pollConfiguration): self {
$this->setTitle($pollConfiguration['title'] ?? $this->getTitle());
$this->setDescription($pollConfiguration['description'] ?? $this->getDescription());
$this->setAccess($pollConfiguration['access'] ?? $this->getAccess());
$this->setAllowComment($pollConfiguration['allowComment'] ?? $this->getAllowComment());
$this->setAllowMaybe($pollConfiguration['allowMaybe'] ?? $this->getAllowMaybe());
$this->setAllowProposals($pollConfiguration['allowProposals'] ?? $this->getAllowProposals());
$this->setAnonymousSafe($pollConfiguration['anonymous'] ?? $this->getAnonymous());
$this->setAutoReminder($pollConfiguration['autoReminder'] ?? $this->getAutoReminder());
$this->setExpire($pollConfiguration['expire'] ?? $this->getExpire());
$this->setHideBookedUp($pollConfiguration['hideBookedUp'] ?? $this->getHideBookedUp());
$this->setProposalsExpire($pollConfiguration['proposalsExpire'] ?? $this->getProposalsExpire());
$this->setShowResults($pollConfiguration['showResults'] ?? $this->getShowResults());
$this->setUseNo($pollConfiguration['useNo'] ?? $this->getUseNo());
$this->setOptionLimit($pollConfiguration['maxVotesPerOption'] ?? $this->getOptionLimit());
$this->setVoteLimit($pollConfiguration['maxVotesPerUser'] ?? $this->getVoteLimit());
return $this;
}
public function getExpired(): bool {
$compareTime = time();
$expiry = $this->getExpire();
return (
$expiry > 0
&& $expiry < $compareTime
);
}
public function getUserRole(): string {
if ($this->getCurrentUserIsEntityUser()) {
return self::ROLE_OWNER;
}
if ($this->getIsCurrentUserLocked() && $this->userRole === self::ROLE_ADMIN) {
return self::ROLE_USER;
}
if ($this->userSession->getShareType() === Share::TYPE_PUBLIC) {
return Share::TYPE_PUBLIC;
}
return $this->userRole;
}
public function getVoteUrl(): string {
return $this->urlGenerator->linkToRouteAbsolute(
AppConstants::APP_ID . '.page.vote',
['id' => $this->getId()]
);
}
/**
* Set anonymous setting
* If setting has negative value, it is locked and cannot be changed
* @param bool|int $value - true for anonymous, false for non-anonymous
*/
public function setAnonymousSafe(bool|int $value): void {
// if anonymous is locked, do not allow changes
if ($this->getAnonymous() < 0) {
return;
}
if (!$this->getAllowDeanonymize() && $value > 0) {
// if the owner of the poll is restricted, lock the anonymous
// setting once it is set
// lock anonymous setting by setting it to negative value
$value = -1;
}
$this->setAnonymous((int)$value);
}
private function setAutoReminder(bool|int $value): void {
$this->setMiscSettingsByKey('autoReminder', (bool)$value);
}
private function getAutoReminder(): bool {
return $this->getMiscSettingsArray()['autoReminder'] ?? false;
}
// alias of getId()
public function getPollId(): int {
return $this->getId();
}
// alias of getOwner()
public function getUserId(): string {
return $this->getOwner();
}
// alias of setOwner($value)
public function setUserId(string $userId): void {
$this->setOwner($userId);
}
private function getGroupShares(): array {
if ($this->groupShares !== null && $this->groupShares !== '') {
// explode with separator and remove empty elements
return array_filter(explode(PollMapper::CONCAT_SEPARATOR, PollMapper::CONCAT_SEPARATOR . $this->groupShares));
}
return [];
}
private function getAccess(): string {
if ($this->access === self::ACCESS_PUBLIC) {
return self::ACCESS_OPEN;
}
if ($this->access === self::ACCESS_HIDDEN) {
return self::ACCESS_PRIVATE;
}
return $this->access;
}
private function getProposalsExpired(): bool {
return (
$this->getProposalsExpire() > 0
&& $this->getProposalsExpire() < time()
);
}
public function getPollShowResults() {
// avoiding migration, expired has been renamed to closed
return $this->showResults === 'expired' ? Poll::SHOW_RESULTS_CLOSED : $this->showResults;
}
public function getDescription(): string {
return $this->description ?? '';
}
private function getDescriptionSafe(): string {
return htmlspecialchars($this->getDescription());
}
private function setMiscSettingsArray(array $value): void {
$this->setMiscSettings(json_encode($value));
}
private function getMiscSettingsArray(): array {
if ($this->getMiscSettings()) {
return json_decode($this->getMiscSettings(), true);
}
return [];
}
public function getTimeToDeadline(int $time = 0): int {
if ($time === 0) {
$time = time();
}
$deadline = $this->getDeadline();
if (
$deadline - $this->getCreated() > self::FIVE_DAYS
&& $deadline - $time < self::TWO_DAYS
&& $deadline > $time
) {
return self::TWO_DAYS;
}
if (
$deadline - $this->getCreated() > self::TWO_DAYS
&& $deadline - $time < self::ONE_AND_HALF_DAY
&& $deadline > $time
) {
return self::ONE_AND_HALF_DAY;
}
throw new NoDeadLineException();
}
private function getRelevantThreshold(): int {
return max(
$this->getCreated(),
$this->getLastInteraction(),
$this->getExpire(),
$this->getMaxDate(),
);
}
private function getDeletionDate(): int {
if ($this->getDeleted() > 0 && $this->appSettings->getAutoDeleteEnabled()) {
return $this->getDeleted() + ($this->appSettings->getAutoDeleteOffsetDays() * 60 * 60 * 24);
}
return 0;
}
private function getIsCurrentUserLocked(): bool {
return boolval($this->isCurrentUserLocked);
}
public function getDeadline(): int {
// if expiration is set return expiration date
if ($this->getExpire()) {
return $this->getExpire();
}
if ($this->getType() === Poll::TYPE_DATE) {
// use lowest date option as reminder deadline threshold
// if no options are set return is the current time
return $this->getMinDate();
}
throw new NoDeadLineException();
}
/**
* @param bool|string|int|array $value
*/
private function setMiscSettingsByKey(string $key, $value): void {
$miscSettings = $this->getMiscSettingsArray();
$miscSettings[$key] = $value;
$this->setMiscSettingsArray($miscSettings);
}
/**
*
* Check Permissions
*
*/
/**
* Request a permission level and get exception if denied
* @throws ForbiddenException Thrown if access is denied
*/
public function request(string $permission): bool {
if (!$this->getIsAllowed($permission)) {
throw new ForbiddenException('denied permission ' . $permission);
}
return true;
}
/**
* Check particular rights and inform via boolean value, if the right is granted or denied
*/
public function getIsAllowed(string $permission): bool {
return match ($permission) {
self::PERMISSION_COMMENT_ADD => $this->getAllowCommenting(),
self::PERMISSION_COMMENT_DELETE => $this->getAllowDeleteComment(),
self::PERMISSION_OPTION_ADD => $this->getAllowAddOptions(),
self::PERMISSION_OPTION_CONFIRM => $this->getAllowConfirmOption(),
self::PERMISSION_OPTION_CLONE => $this->getAllowCloneOption(),
self::PERMISSION_OPTION_DELETE => $this->getAllowDeleteOption(),
self::PERMISSION_OPTIONS_SHIFT => $this->getAllowShiftOptions(),
self::PERMISSION_OPTIONS_REORDER => $this->getAllowReorderOptions(),
self::PERMISSION_OVERRIDE => true,
self::PERMISSION_POLL_VIEW => $this->getAllowAccessPoll(),
self::PERMISSION_POLL_EDIT => $this->getAllowEditPoll(),
self::PERMISSION_POLL_DELETE => $this->getAllowDeletePoll(),
self::PERMISSION_POLL_ARCHIVE => $this->getAllowEditPoll(),
self::PERMISSION_POLL_TAKEOVER => $this->getAllowTakeOver(),
self::PERMISSION_POLL_CHANGE_OWNER => $this->getAllowChangeOwner(),
self::PERMISSION_POLL_SUBSCRIBE => $this->getAllowSubscribeToPoll(),
self::PERMISSION_POLL_RESULTS_VIEW => $this->getAllowShowResults(),
self::PERMISSION_POLL_USERNAMES_VIEW => $this->getAllowEditPoll() || !$this->getAnonymous(),
self::PERMISSION_VOTE_EDIT => $this->getAllowVote(),
self::PERMISSION_VOTE_FOREIGN_CHANGE => $this->getAllowChangeForeignVotes(),
self::PERMISSION_SHARE_ADD => $this->systemSettings->getShareCreateAllowed(),
self::PERMISSION_SHARE_ADD_EXTERNAL => $this->systemSettings->getExternalShareCreationAllowed(),
self::PERMISSION_DEANONYMIZE => $this->getAllowDeanonymize(),
default => false,
};
}
/**
* getIsInvolved - Is current user involved in current poll?
* @return bool Returns true, if the current user is involved in the poll via share, as a participant or as the poll owner.
*/
private function getIsInvolved(): bool {
return (
$this->getIsPollOwner()
|| $this->getIsParticipant()
|| $this->getIsPersonallyInvited())
|| $this->getIsInvitedViaGroupShare();
}
/**
* Check, if poll settings is set to open access for internal users
*/
private function getIsOpenPoll(): bool {
return $this->getAccess() === Poll::ACCESS_OPEN && $this->userSession->getIsLoggedIn();
}
/**
* getIsParticipant - Is user a participant?
* @return bool Returns true, if the current user is already a particitipant of the current poll.
*/
private function getIsParticipant(): bool {
return $this->getCurrentUserVotes() > 0;
}
/**
* getIsInvitedViaGroupShare - Is the poll shared via group share?
* where the current user is member of. This only affects logged in users.
* @return bool Returns true, if the current poll contains a group share with a group,
*/
private function getIsInvitedViaGroupShare(): bool {
if (!$this->userSession->getIsLoggedIn()) {
return false;
}
return count($this->getGroupSharesForUser()) > 0;
}
private function getGroupSharesForUser(): array {
return array_filter($this->getGroupShares(), function ($groupName) {
return ($this->userSession->getCurrentUser()->getIsInGroup($groupName));
});
}
/**
* getIsPersonallyInvited - Is the poll shared via user share with the current user?
* Checking via user role
* @return bool Returns true, if the current poll contains a user role which matches a share type
*/
private function getIsPersonallyInvited(): bool {
return in_array($this->getUserRole(), [
Poll::ROLE_ADMIN,
Poll::ROLE_USER,
Poll::ROLE_EXTERNAL,
Poll::ROLE_EMAIL,
Poll::ROLE_CONTACT,
]);
}
/**
* The detailed checks - For the sake of readability, the queries and selections
* were kept detailed and with low complexity
*/
/**
* Checks, if the user has delegated admin rights to edit poll settings via share
*/
private function getIsDelegatedAdmin(): bool {
return $this->getUserRole() === Poll::ROLE_ADMIN
&& !$this->getIsCurrentUserLocked();
}
/**
* Checks, if user is allowed to edit the poll configuration
**/
private function getAllowEditPoll(): bool {
// Console has god mode
if (defined('OC_CONSOLE')) {
return true;
}
// owner is always allowed to edit the poll configuration
if ($this->getIsPollOwner()) {
return true;
}
// user has delegated owner rights
if ($this->getIsDelegatedAdmin()) {
return true;
}
// deny edit rights in all other cases
return false;
}
private function getAllowTakeOver(): bool {
return $this->userSession->getCurrentUser()->getIsAdmin();
}
/**
* Checks, if user is allowed to edit the poll configuration
**/
private function getAllowChangeOwner(): bool {
return $this->getAllowEditPoll()
|| $this->userSession->getCurrentUser()->getIsAdmin();
}
/**
* Checks, if user is allowed to access (view) poll
*/
private function getAllowAccessPoll(): bool {
// edit rights include access to poll
if ($this->getAllowEditPoll()) {
return true;
}
// No further access to poll, if it is deleted
if ($this->getDeleted()) {
return false;
}
// grant access if poll poll is an open poll (for logged in users)
if ($this->getIsOpenPoll() && $this->userSession->getIsLoggedIn()) {
return true;
}
// grant access if user is involved in poll in any way
if ($this->getIsInvolved()) {
return true;
}
$share = $this->userSession->getShare();
// return check result of an existing valid share for this user
return boolval($share->getId() && $share->getPollId() === $this->getId());
}
/**
* Checks, if user is allowed to delete the poll
* includes the right to archive and take over
**/
private function getAllowDeletePoll(): bool {
if ($this->getAllowEditPoll()) {
// users with edit rights are allowed to delete the poll
return true;
}
// additionally site admins are allowed to delete polls, in all other cases deny poll deletion right
return $this->userSession->getCurrentUser()->getIsAdmin();
}
/**
* Checks, if user is allowed to add add vote options
**/
private function getAllowAddOptions(): bool {
// Edit right includes adding new options
if ($this->getAllowEditPoll()) {
return true;
}
// deny, if user has no access right to this poll
if (!$this->getAllowAccessPoll()) {
return false;
}
// public shares are not allowed to add options
if ($this->userSession->getShare()->getType() === Share::TYPE_PUBLIC) {
return false;
}
// Request for option proposals is expired, deny
if ($this->getProposalsExpired()) {
return false;
}
// Locked Users are not allowed to add options
if (boolval($this->getIsCurrentUserLocked())) {
return false;
}
// Allow, if poll requests proposals
return $this->getAllowProposals() === Poll::PROPOSAL_ALLOW;
}
private function getAllowShiftOptions(): bool {
return $this->getAllowEditPoll() && $this->getProposalsCount() === 0;
}
private function getAllowCloneOption(): bool {
return $this->getAllowEditPoll();
}
/**
* Is current user allowed to delete options from poll
*/
private function getAllowDeleteOption(): bool {
return $this->getIsPollOwner() || $this->getIsDelegatedAdmin();
}
/**
* Is current user allowed to confirm options
*/
private function getAllowConfirmOption(): bool {
return $this->getAllowEditPoll() && $this->getExpired();
}
/**
* Is current user allowed to confirm options
*/
private function getAllowReorderOptions(): bool {
return $this->getAllowEditPoll() && !$this->getExpired() && $this->getType() === Poll::TYPE_TEXT;
}
/**
* Compare $userId with current user's id
*/
public function matchUser(string $userId): bool {
return (bool)$this->userSession->getCurrentUser()->getId() && $this->userSession->getCurrentUser()->getId() === $userId;
}
/**
* Checks, if the current user is the poll owner
**/
public function getIsPollOwner(): bool {
return ($this->getUserRole() === Poll::ROLE_OWNER);
}
/**
* Permission checks
*/
/**
* Checks, if user is allowed to see and write comments
**/
private function getAllowCommenting(): bool {
// user has no access right to this poll
if (!$this->getAllowAccessPoll()) {
return false;
}
// public shares are not allowed to comment
if ($this->userSession->getShare()->getType() === Share::TYPE_PUBLIC) {
return false;
}
// public shares are not allowed to comment
if (boolval($this->getIsCurrentUserLocked())) {
return false;
}
// return the poll setting for comments
return (bool)$this->getAllowComment();
}
/**
* Checks, if user is allowed to delete comments from poll
**/
private function getAllowDeleteComment(): bool {
return $this->getAllowEditPoll();
}
/**
* Checks, if poll owner is allowed to change votes
**/
private function getAllowChangeForeignVotes(): bool {
return $this->getAnonymous() > -1 && $this->getAllowEditPoll() && $this->getUser()->getIsUnrestrictedPollOwner();
}
/**
* Checks, if poll owner is allowed to deanonymize votes
**/
private function getAllowDeanonymize(): bool {
// Current user is allowed to edit the poll and the owner of the poll is unrestricted
return $this->getAnonymous() > -1 && $this->getAllowEditPoll() && $this->getUser()->getIsUnrestrictedPollOwner();
}
/**
* Checks, if user is allowed to vote
**/
private function getAllowVote(): bool {
// user has no access right to this poll
if (!$this->getAllowAccessPoll()) {
return false;
}
// public shares are not allowed to vote
if ($this->userSession->getShare()->getType() === Share::TYPE_PUBLIC) {
return false;
}
// Locked users are not allowed to vote
if (boolval($this->getIsCurrentUserLocked())) {
return false;
}
// deny votes, if poll is expired
return !$this->getExpired();
}
/**
* Checks, if user is allowed to subscribe to updates
**/
private function getAllowSubscribeToPoll(): bool {
// user with access to poll are always allowed to subscribe
if (!$this->getAllowAccessPoll()) {
return false;
}
return $this->userSession->getCurrentUser()->getHasEmail();
}
/**
* Checks, if user is allowed to see results of current poll
**/
private function getAllowShowResults(): bool {
// edit rights include access to results
if ($this->getAllowEditPoll()) {
return true;
}
// no access to poll, deny
if (!$this->getAllowAccessPoll()) {
return false;
}
// show results, when poll is closed
if ($this->getShowResults() === Poll::SHOW_RESULTS_CLOSED && $this->getExpired()) {
return true;
}
// return poll settings
return $this->getShowResults() === Poll::SHOW_RESULTS_ALWAYS;
}
}