-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathShare.php
More file actions
277 lines (242 loc) · 7.24 KB
/
Share.php
File metadata and controls
277 lines (242 loc) · 7.24 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
<?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\Helper\Container;
use OCP\IURLGenerator;
/**
* @psalm-suppress UnusedProperty
* @method int getId()
* @method void setId(int $value)
* @method string getToken()
* @method void setToken(string $value)
* @method string getType()
* @method void setType(string $value)
* @method int getPollId()
* @method void setPollId(int $value)
* @method string getUserId()
* @method void setUserId(string $value)
* @method string getEmailAddress()
* @method void setEmailAddress(string $value)
* @method int getInvitationSent()
* @method void setInvitationSent(int $value)
* @method int getReminderSent()
* @method void setReminderSent(int $value)
* @method int getLocked()
* @method void setLocked(int $value)
* @method string getDisplayName()
* @method void setDisplayName(string $value)
* @method string getMiscSettings()
* @method void setMiscSettings(string $value)
* @method int getVoted()
* @method void setVoted(int $value)
* @method int getDeleted()
* @method void setDeleted(int $value)
* @method string getLabel()
* @method void setLabel(string $value)
*/
class Share extends EntityWithUser implements JsonSerializable {
public const TABLE = 'polls_share';
public const EMAIL_OPTIONAL = 'optional';
public const EMAIL_MANDATORY = 'mandatory';
public const EMAIL_DISABLED = 'disabled';
// Only authenticated access
public const TYPE_USER = 'user';
public const TYPE_ADMIN = 'admin';
public const TYPE_GROUP = 'group';
// Public and authenticated Access
public const TYPE_PUBLIC = 'public';
// Only public access
public const TYPE_EMAIL = 'email';
public const TYPE_CONTACT = 'contact';
public const TYPE_EXTERNAL = 'external';
// no direct Access
public const TYPE_CIRCLE = 'circle';
public const TYPE_CONTACTGROUP = 'contactGroup';
public const CONVERATABLE_PUBLIC_SHARES = [
self::TYPE_EMAIL,
self::TYPE_CONTACT,
];
// Share types, that are allowed for public access (without login)
public const SHARE_PUBLIC_ACCESS_ALLOWED = [
self::TYPE_PUBLIC,
self::TYPE_EXTERNAL,
...self::CONVERATABLE_PUBLIC_SHARES,
];
// Share types, that are allowed for authenticated access (with login)
public const SHARE_AUTH_ACCESS_ALLOWED = [
self::TYPE_PUBLIC,
self::TYPE_ADMIN,
self::TYPE_GROUP,
self::TYPE_USER,
];
public const TYPE_SORT_ARRAY = [
self::TYPE_PUBLIC,
self::TYPE_ADMIN,
self::TYPE_GROUP,
self::TYPE_USER,
self::TYPE_CONTACT,
self::TYPE_EMAIL,
self::TYPE_EXTERNAL,
self::TYPE_CIRCLE,
self::TYPE_CONTACTGROUP,
];
public const RESOLVABLE_SHARES = [
self::TYPE_CIRCLE,
self::TYPE_CONTACTGROUP
];
protected IURLGenerator $urlGenerator;
// schema columns
public $id = null;
protected int $pollId = 0;
protected string $token = '';
protected string $type = '';
protected string $label = '';
protected string $userId = '';
protected ?string $displayName = null;
protected ?string $emailAddress = null;
protected int $invitationSent = 0;
protected int $reminderSent = 0;
protected int $locked = 0;
protected ?string $miscSettings = '';
protected int $deleted = 0;
// joined columns
protected int $voted = 0;
public function __construct() {
$this->addType('pollId', 'integer');
$this->addType('invitationSent', 'integer');
$this->addType('locked', 'integer');
$this->addType('reminderSent', 'integer');
$this->addType('deleted', 'integer');
$this->urlGenerator = Container::queryClass(IURLGenerator::class);
}
/**
* @return array
*
* @psalm-suppress PossiblyUnusedMethod
*/
public function jsonSerialize(): array {
return [
'id' => $this->getId(),
'token' => $this->getToken(),
'type' => $this->getType(),
'pollId' => $this->getPollId(),
'invitationSent' => boolval($this->getInvitationSent()),
'reminderSent' => boolval($this->getReminderSent()),
'locked' => boolval($this->getDeleted() ? 0 : $this->getLocked()),
'label' => $this->getLabel(),
'URL' => $this->getURL(),
'publicPollEmail' => $this->getPublicPollEmail(),
'voted' => boolval($this->getVoted()),
'deleted' => boolval($this->getDeleted()),
'user' => $this->getUser()->getRichUserArray(),
];
}
/**
* Setting, if email is optional, mandatory or hidden on public poll registration
*/
public function getPublicPollEmail(): string {
return $this->getMiscSettingsArray()['publicPollEmail'] ?? 'optional';
}
/**
* Get userId of share user
*/
public function getUserId(): string {
return $this->userId;
}
/**
* Get userId of share user
*/
public function getType(): string {
if (($this->getDeleted() || $this->getLocked()) && $this->type === self::TYPE_ADMIN) {
return self::TYPE_USER;
}
return $this->type;
}
/**
* Setting, if email is optional, mandatory or hidden on public poll registration
*/
public function setPublicPollEmail(string $value): void {
$this->setMiscSettingsByKey('publicPollEmail', $value);
}
/**
* Share label for public shares, falls back to username until migrated
* TODO: remove fallback after migration was introduced
*/
public function getLabel(): string {
if ($this->getType() === self::TYPE_PUBLIC && $this->label) {
return $this->label;
}
return $this->displayName ?? '';
}
/**
* Sharee's displayName. In case of public poll label is used instead
* TODO: remove public poll chaeck after migration to label
*/
public function getDisplayName(): string {
if ($this->getType() === self::TYPE_PUBLIC) {
return '';
}
return (string)$this->displayName;
}
public function getTimeZoneName(): string {
return $this->getMiscSettingsArray()['timeZone'] ?? '';
}
public function setTimeZoneName(string $value): void {
$this->setMiscSettingsByKey('timeZone', $value);
}
public function getLanguage(): string {
return $this->getMiscSettingsArray()['language'] ?? '';
}
public function setLanguage(string $value): void {
$this->setMiscSettingsByKey('language', $value);
}
// Fallback for now; use language as locale
public function getLocale(): string {
return $this->getLanguage();
}
public function getURL(): string {
if (in_array($this->type, [self::TYPE_USER, self::TYPE_ADMIN, self::TYPE_GROUP], true)) {
return $this->urlGenerator->linkToRouteAbsolute(
AppConstants::APP_ID . '.page.vote',
['id' => $this->pollId]
);
} elseif ($this->token) {
return $this->urlGenerator->linkToRouteAbsolute(
AppConstants::APP_ID . '.public.votePage',
['token' => $this->token]
);
} else {
return '';
}
}
public function getRichObjectString(): array {
return [
'type' => 'highlight',
'id' => (string)$this->getId(),
'name' => $this->getType(),
];
}
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 [];
}
/**
* @param bool|string|int|array $value
*/
private function setMiscSettingsByKey(string $key, $value): void {
$miscSettings = $this->getMiscSettingsArray();
$miscSettings[$key] = $value;
$this->setMiscSettingsArray($miscSettings);
}
}