8
8
9
9
namespace OCA \Polls \Db ;
10
10
11
- use OCA \Polls \Helper \ Container ;
11
+ use OCA \Polls \Model \ User \ Anon ;
12
12
use OCA \Polls \Model \UserBase ;
13
13
use OCA \Polls \UserSession ;
14
14
use OCP \AppFramework \Db \Entity ;
15
+ use OCP \Server ;
15
16
16
17
/**
17
18
* @psalm-suppress UnusedProperty
23
24
* @method string getPollOwnerId()
24
25
* @method string getPollShowResults()
25
26
* @method int getPollExpire()
27
+ * @method string getShareType()
26
28
*/
27
29
28
30
abstract class EntityWithUser extends Entity {
29
31
protected int $ anonymized = 0 ;
30
32
protected string $ pollOwnerId = '' ;
31
33
protected string $ pollShowResults = '' ;
32
34
protected int $ pollExpire = 0 ;
33
-
34
- public const ANON_FULL = 'anonymous ' ;
35
- public const ANON_PRIVACY = 'privacy ' ;
36
- public const ANON_NONE = 'ful_view ' ;
35
+ protected ?string $ shareType = '' ;
37
36
38
37
public function __construct () {
39
38
// joined Attributes
@@ -42,41 +41,66 @@ public function __construct() {
42
41
}
43
42
44
43
/**
45
- * Anonymized the user completely (ANON_FULL) or just strips out personal information
44
+ * Is the current user the owner of the entity
45
+ * @return bool
46
46
*/
47
- public function getAnonymizeLevel (): string {
48
- $ currentUserId = Container::queryClass (UserSession::class)->getCurrentUserId ();
49
- // Don't censor for poll owner or it is the current user's entity
50
- if ($ this ->getPollOwnerId () === $ currentUserId || $ this ->getIsOwner ()) {
51
- return self ::ANON_NONE ;
47
+ public function getCurrentUserIsEntityUser (): bool {
48
+ $ userSession = Server::get (UserSession::class);
49
+ return $ userSession ->getCurrentUserId () === $ this ->getUserId ();
50
+ }
51
+
52
+ private function getEntityAnonymization (): bool {
53
+ if ($ this ->getCurrentUserIsEntityUser ()) {
54
+ // if the current user is the owner of the entity, don't anonymize the entity
55
+ return false ;
52
56
}
53
57
54
- // Anonymize if poll's anonymize setting is true
55
- if (( bool ) $ this -> anonymized ) {
56
- return self :: ANON_FULL ;
58
+ if ( $ this -> getAnonymized () < 0 ) {
59
+ // the poll is anonymized and locked, anonymize the entity
60
+ return true ;
57
61
}
58
62
59
- // Anonymize if votes are hidden
63
+ if ($ this ->getAnonymized () > 0 ) {
64
+ // the poll is anonymized and unlocked
65
+ $ userSession = Server::get (UserSession::class);
66
+ if ($ this ->getPollOwnerId () === $ userSession ->getCurrentUserId ()) {
67
+ // if the current user is the poll owner, don't anonymize the entity
68
+ return false ;
69
+ }
70
+ if ($ this ->getShareType () === Share::TYPE_ADMIN ) {
71
+ // if the current user is a delegated admin, don't anonymize the entity
72
+ return false ;
73
+ }
74
+ // if the current user is not the poll owner, anonymize the entity
75
+ return true ;
76
+ }
77
+
78
+ // the poll is not anonymized
60
79
if ($ this ->getPollShowResults () === Poll::SHOW_RESULTS_NEVER
61
- || ($ this ->getPollShowResults () === Poll::SHOW_RESULTS_CLOSED && (
62
- !$ this ->getPollExpire () || $ this -> getPollExpire () > time ()
63
- ))
64
- ) {
65
- return self :: ANON_FULL ;
80
+ || ($ this ->getPollShowResults () === Poll::SHOW_RESULTS_CLOSED
81
+ && !$ this ->getPollExpire () > time ())) {
82
+
83
+ // Do not anonymize the poll owner
84
+ return !( $ this instanceof Poll) ;
66
85
}
67
86
68
- return self ::ANON_PRIVACY ;
87
+ // in all other cases, don't anonymize the entity
88
+ return false ;
69
89
}
70
90
71
- public function getIsOwner (): bool {
72
- return Container::queryClass (UserSession::class)->getCurrentUserId () === $ this ->getUserId ();
73
- }
74
91
92
+ /**
93
+ * @return UserBase Gets owner of the entity
94
+ */
75
95
public function getUser (): UserBase {
76
- /** @var UserMapper */
77
- $ userMapper = (Container::queryClass (UserMapper::class));
96
+ if ($ this ->getEntityAnonymization ()) {
97
+ $ user = new Anon ($ this ->getUserId ());
98
+ return $ user ;
99
+ }
100
+
101
+ $ userMapper = (Server::get (UserMapper::class));
102
+
78
103
$ user = $ userMapper ->getParticipant ($ this ->getUserId (), $ this ->getPollId ());
79
- $ user ->setAnonymizeLevel ($ this ->getAnonymizeLevel ());
80
104
return $ user ;
81
105
}
82
106
}
0 commit comments