forked from nextcloud/forms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormsService.php
More file actions
753 lines (662 loc) Β· 20.4 KB
/
FormsService.php
File metadata and controls
753 lines (662 loc) Β· 20.4 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
<?php
/**
* @copyright Copyright (c) 2020 John Molakvoæ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
* @author Jonas Rittershofer <jotoeri@users.noreply.github.com>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Forms\Service;
use OCA\Forms\Activity\ActivityManager;
use OCA\Forms\Constants;
use OCA\Forms\Db\Form;
use OCA\Forms\Db\FormMapper;
use OCA\Forms\Db\OptionMapper;
use OCA\Forms\Db\Question;
use OCA\Forms\Db\QuestionMapper;
use OCA\Forms\Db\Share;
use OCA\Forms\Db\ShareMapper;
use OCA\Forms\Db\SubmissionMapper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\IMapperException;
use OCP\Files\IMimeTypeDetector;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Security\ISecureRandom;
use OCP\Share\IShare;
use Psr\Log\LoggerInterface;
/**
* Trait for getting forms information in a service
*/
class FormsService {
private ?IUser $currentUser;
public function __construct(
IUserSession $userSession,
private ActivityManager $activityManager,
private FormMapper $formMapper,
private OptionMapper $optionMapper,
private QuestionMapper $questionMapper,
private ShareMapper $shareMapper,
private SubmissionMapper $submissionMapper,
private ConfigService $configService,
private IGroupManager $groupManager,
private LoggerInterface $logger,
private IUserManager $userManager,
private ISecureRandom $secureRandom,
private CirclesService $circlesService,
private IRootFolder $storage,
private IL10N $l10n,
private IMimeTypeDetector $mimeTypeDetector,
) {
$this->currentUser = $userSession->getUser();
}
/**
* Create a new Form Hash
*/
public function generateFormHash(): string {
return $this->secureRandom->generate(
16,
ISecureRandom::CHAR_HUMAN_READABLE
);
}
/**
* Load options corresponding to question
*
* @param integer $questionId
* @return array
*/
private function getOptions(int $questionId): array {
$optionList = [];
try {
$optionEntities = $this->optionMapper->findByQuestion($questionId);
foreach ($optionEntities as $optionEntity) {
$optionList[] = $optionEntity->read();
}
} catch (DoesNotExistException $e) {
//handle silently
} finally {
return $optionList;
}
}
/**
* Load questions corresponding to form
*
* @param integer $formId
* @return array
*/
public function getQuestions(int $formId): array {
$questionList = [];
try {
$questionEntities = $this->questionMapper->findByForm($formId);
foreach ($questionEntities as $questionEntity) {
$question = $questionEntity->read();
$question['options'] = $this->getOptions($question['id']);
$question['accept'] = [];
if ($question['type'] === Constants::ANSWER_TYPE_FILE) {
if ($question['extraSettings']['allowedFileTypes'] ?? null) {
$aliases = $this->mimeTypeDetector->getAllAliases();
foreach ($question['extraSettings']['allowedFileTypes'] as $type) {
$question['accept'] = array_keys(array_filter($aliases, function (string $alias) use ($type) {
return $alias === $type;
}));
}
}
if ($question['extraSettings']['allowedFileExtensions'] ?? null) {
foreach ($question['extraSettings']['allowedFileExtensions'] as $extension) {
$question['accept'][] = '.' . $extension;
}
}
}
$questionList[] = $question;
}
} catch (DoesNotExistException $e) {
//handle silently
} finally {
return $questionList;
}
}
/**
* Load shares corresponding to form
*
* @param integer $formId
* @return array
*/
public function getShares(int $formId): array {
$shareList = [];
$shareEntities = $this->shareMapper->findByForm($formId);
foreach ($shareEntities as $shareEntity) {
$share = $shareEntity->read();
$share['displayName'] = $this->getShareDisplayName($share);
$shareList[] = $share;
}
return $shareList;
}
/**
* Get a form data
*
* @param Form $form
* @return array
* @throws IMapperException
*/
public function getForm(Form $form): array {
$result = $form->read();
$result['questions'] = $this->getQuestions($form->getId());
$result['shares'] = $this->getShares($form->getId());
// Append permissions for current user.
$result['permissions'] = $this->getPermissions($form);
// Append canSubmit, to be able to show proper EmptyContent on internal view.
$result['canSubmit'] = $this->canSubmit($form);
// Append submissionCount if currentUser has permissions to see results
if (in_array(Constants::PERMISSION_RESULTS, $result['permissions'])) {
$result['submissionCount'] = $this->submissionMapper->countSubmissions($form->getId());
}
if ($result['fileId']) {
try {
$result['filePath'] = $this->getFilePath($form);
// If file was deleted, set filePath to null
} catch (NotFoundException $e) {
$result['filePath'] = null;
}
}
return $result;
}
/**
* Create partial form, as returned by Forms-Lists.
*
* @param Form $form
* @return array
* @throws IMapperException
*/
public function getPartialFormArray(Form $form): array {
$result = [
'id' => $form->getId(),
'hash' => $form->getHash(),
'title' => $form->getTitle(),
'expires' => $form->getExpires(),
'lastUpdated' => $form->getLastUpdated(),
'permissions' => $this->getPermissions($form),
'partial' => true,
'state' => $form->getState(),
];
// Append submissionCount if currentUser has permissions to see results
if (in_array(Constants::PERMISSION_RESULTS, $result['permissions'])) {
$result['submissionCount'] = $this->submissionMapper->countSubmissions($form->getId());
}
return $result;
}
/**
* Get a form data without sensitive informations
*
* @param Form $form
* @return array
* @throws IMapperException
*/
public function getPublicForm(Form $form): array {
$formData = $this->getForm($form);
// Remove sensitive data
unset($formData['access']);
unset($formData['ownerId']);
unset($formData['shares']);
unset($formData['fileId']);
unset($formData['filePath']);
unset($formData['fileFormat']);
return $formData;
}
/**
* Get current users permissions on a form
*
* @param Form $form
* @return array
*/
public function getPermissions(Form $form): array {
if (!$this->currentUser) {
return [];
}
// Owner is allowed to do everything
if ($this->currentUser->getUID() === $form->getOwnerId()) {
return Constants::PERMISSION_ALL;
}
$permissions = [];
$shares = $this->getSharesWithUser($form->getId(), $this->currentUser->getUID());
foreach ($shares as $share) {
$permissions = array_merge($permissions, $share->getPermissions());
}
// Fall back to submit permission if access is granted to all users
if (count($permissions) === 0) {
$access = $form->getAccess();
if ($access['permitAllUsers'] && $this->configService->getAllowPermitAll()) {
$permissions = [Constants::PERMISSION_SUBMIT];
}
}
return array_values(array_unique($permissions));
}
/**
* Can the current user edit a form
*
* @param Form $form
* @return boolean
*/
public function canEditForm(Form $form): bool {
return in_array(Constants::PERMISSION_EDIT, $this->getPermissions($form));
}
/**
* Can the current user see results of a form
*
* @param Form $form
* @return boolean
*/
public function canSeeResults(Form $form): bool {
return in_array(Constants::PERMISSION_RESULTS, $this->getPermissions($form));
}
/**
* Can the current user delete results of a form
*
* @param Form $form
* @return boolean
*/
public function canDeleteResults(Form $form): bool {
// Check permissions
if (!in_array(Constants::PERMISSION_RESULTS_DELETE, $this->getPermissions($form))) {
return false;
}
// Do not allow deleting results on archived forms
return !$this->isFormArchived($form);
}
/**
* Can the user submit a form
*
* @param Form $form
* @return boolean
*/
public function canSubmit(Form $form): bool {
// We cannot control how many time users can submit if public link / legacyLink available
if ($this->hasPublicLink($form)) {
return true;
}
// Owner is always allowed to submit
if ($this->currentUser->getUID() === $form->getOwnerId()) {
return true;
}
// Refuse access, if SubmitMultiple is not set and user already has taken part.
if (
!$form->getSubmitMultiple() &&
$this->submissionMapper->hasFormSubmissionsByUser($form, $this->currentUser->getUID())
) {
return false;
}
return true;
}
/**
* Searching Shares for public link
*
* @param Form $form
* @return boolean
*/
private function hasPublicLink(Form $form): bool {
$access = $form->getAccess();
if (isset($access['legacyLink'])) {
return true;
}
$shareEntities = $this->shareMapper->findByForm($form->getId());
foreach ($shareEntities as $shareEntity) {
if ($shareEntity->getShareType() === IShare::TYPE_LINK) {
return true;
}
}
return false;
}
/**
* Check if current user has access to this form
*
* @param Form $form
* @return boolean
*/
public function hasUserAccess(Form $form): bool {
$access = $form->getAccess();
$ownerId = $form->getOwnerId();
// Refuse access, if no user logged in.
if (!$this->currentUser) {
return false;
}
// Always grant access to owner.
if ($ownerId === $this->currentUser->getUID()) {
return true;
}
// Now all remaining users are allowed, if permitAll is set.
if ($access['permitAllUsers'] && $this->configService->getAllowPermitAll()) {
return true;
}
// Selected Access remains.
if ($this->isSharedToUser($form->getId())) {
return true;
}
// None of the possible access-options matched.
return false;
}
/**
* Get all forms shared to the user
* @param IUser $user User to query shared forms for
* @param bool $filterShown Set to false to also include forms shared but not visible on sidebar
*/
public function getSharedForms(IUser $user, bool $filterShown = true): array {
$groups = $this->groupManager->getUserGroupIds($user);
$teams = $this->circlesService->getUserTeamIds($user->getUID());
$forms = $this->formMapper->findSharedForms(
$user->getUID(),
$groups,
$teams,
$filterShown,
);
// filter expired forms
$forms = array_filter($forms, fn (Form $form): bool => $this->isSharedFormShown($form));
return $forms;
}
/**
* Is the shared form shown on sidebar to the user.
*
* @param Form $form
* @return bool
*/
private function isSharedFormShown(Form $form): bool {
// Dont show expired forms if user isn't allowed to see results.
if ($this->hasFormExpired($form) && !$this->canSeeResults($form)) {
return false;
}
return true;
}
/**
* Checking all selected shares
*
* @param int $formId
* @return bool
*/
private function isSharedToUser(int $formId): bool {
$shareEntities = $this->getSharesWithUser($formId, $this->currentUser->getUID());
return count($shareEntities) > 0;
}
/**
* Check if the form is archived
* If a form is archived no changes are allowed
*/
public function isFormArchived(Form $form): bool {
return $form->getState() === Constants::FORM_STATE_ARCHIVED;
}
/**
* Check if the form was closed or archived or has expired.
*
* @param Form $form
* @return boolean
*/
public function hasFormExpired(Form $form): bool {
// Check for form state first
if ($form->getState() !== Constants::FORM_STATE_ACTIVE) {
return true;
}
return ($form->getExpires() !== 0 && $form->getExpires() < time());
}
/**
* Get DisplayNames to Shares
*
* @param array $share
* @return string
*/
public function getShareDisplayName(array $share): string {
$displayName = '';
switch ($share['shareType']) {
case IShare::TYPE_USER:
$user = $this->userManager->get($share['shareWith']);
if ($user instanceof IUser) {
$displayName = $user->getDisplayName();
}
break;
case IShare::TYPE_GROUP:
$group = $this->groupManager->get($share['shareWith']);
if ($group instanceof IGroup) {
$displayName = $group->getDisplayName();
}
break;
case IShare::TYPE_CIRCLE:
$circle = $this->circlesService->getCircle($share['shareWith']);
if (!is_null($circle)) {
$displayName = $circle->getDisplayName();
}
break;
default:
// Preset Empty.
}
return $displayName;
}
/**
* Creates activities for sharing to users.
* @param Form $form Related Form
* @param Share $share The new Share
*/
public function notifyNewShares(Form $form, Share $share): void {
switch ($share->getShareType()) {
case IShare::TYPE_USER:
$this->activityManager->publishNewShare($form, $share->getShareWith());
break;
case IShare::TYPE_GROUP:
$this->activityManager->publishNewGroupShare($form, $share->getShareWith());
break;
case IShare::TYPE_CIRCLE:
$this->activityManager->publishNewCircleShare($form, $share->getShareWith());
break;
default:
// Do nothing.
}
}
/**
* Creates activities for new submissions on a form
*
* @param Form $form Related Form
* @param string $submitter The ID of the user who submitted the form. Can also be our 'anon-user-'-ID
*/
public function notifyNewSubmission(Form $form, string $submitter): void {
$shares = $this->getShares($form->getId());
$this->activityManager->publishNewSubmission($form, $submitter);
foreach ($shares as $share) {
if (!in_array(Constants::PERMISSION_RESULTS, $share['permissions'])) {
continue;
}
$this->activityManager->publishNewSharedSubmission($form, $share['shareType'], $share['shareWith'], $submitter);
}
}
/**
* Return shares of a form shared with given user
*
* @param int $formId The form to query shares for
* @param string $userId The user to check if shared with
* @return Share[]
*/
private function getSharesWithUser(int $formId, string $userId): array {
$shareEntities = $this->shareMapper->findByForm($formId);
return array_filter($shareEntities, function ($shareEntity) use ($userId) {
$share = $shareEntity->read();
// Needs different handling for shareTypes
switch ($share['shareType']) {
case IShare::TYPE_USER:
if ($share['shareWith'] === $userId) {
return true;
}
break;
case IShare::TYPE_GROUP:
if ($this->groupManager->isInGroup($userId, $share['shareWith'])) {
return true;
}
break;
case IShare::TYPE_CIRCLE:
if ($this->circlesService->isUserInCircle($share['shareWith'], $userId)) {
return true;
}
break;
default:
return false;
}
});
}
/**
* Update lastUpdated timestamp for the given form
*
* @param int $formId The form to update
*/
public function setLastUpdatedTimestamp(int $formId): void {
$form = $this->formMapper->findById($formId);
$form->setLastUpdated(time());
$this->formMapper->update($form);
}
/*
* Validates the extraSettings
*
* @param array $extraSettings input extra settings
* @param string $questionType the question type
* @return bool if the settings are valid
*/
public function areExtraSettingsValid(array $extraSettings, string $questionType) {
if (count($extraSettings) === 0) {
return true;
}
// Ensure only allowed keys are set
switch ($questionType) {
case Constants::ANSWER_TYPE_DROPDOWN:
$allowed = Constants::EXTRA_SETTINGS_DROPDOWN;
break;
case Constants::ANSWER_TYPE_MULTIPLE:
case Constants::ANSWER_TYPE_MULTIPLEUNIQUE:
$allowed = Constants::EXTRA_SETTINGS_MULTIPLE;
break;
case Constants::ANSWER_TYPE_SHORT:
$allowed = Constants::EXTRA_SETTINGS_SHORT;
break;
case Constants::ANSWER_TYPE_FILE:
$allowed = Constants::EXTRA_SETTINGS_FILE;
break;
default:
$allowed = [];
}
// Number of keys in extraSettings but not in allowed (but not the other way round)
$diff = array_diff(array_keys($extraSettings), array_keys($allowed));
if (count($diff) > 0) {
return false;
}
// Check type of extra settings
foreach ($extraSettings as $key => $value) {
if (!in_array(gettype($value), $allowed[$key])) {
// Not allowed type
return false;
}
}
if ($questionType === Constants::ANSWER_TYPE_MULTIPLE) {
// Ensure limits are sane
if (isset($extraSettings['optionsLimitMax']) && isset($extraSettings['optionsLimitMin'])
&& $extraSettings['optionsLimitMax'] < $extraSettings['optionsLimitMin']) {
return false;
}
// Special handling of short input for validation
} elseif ($questionType === Constants::ANSWER_TYPE_SHORT && isset($extraSettings['validationType'])) {
// Ensure input validation type is known
if (!in_array($extraSettings['validationType'], Constants::SHORT_INPUT_TYPES)) {
return false;
}
// For custom validation we need to sanitize the regex
if ($extraSettings['validationType'] === 'regex') {
// regex is required for "custom" input validation type
if (!isset($extraSettings['validationRegex'])) {
return false;
}
// regex option must be a string
if (!is_string($extraSettings['validationRegex'])) {
return false;
}
// empty regex matches every thing, this happens also when a new question is created
if (strlen($extraSettings['validationRegex']) === 0) {
return true;
}
// general pattern of a valid regex
$VALID_REGEX = '/^\/(.+)\/([smi]{0,3})$/';
// pattern to look for unescaped slashes
$REGEX_UNESCAPED_SLASHES = '/(?<=(^|[^\\\\]))(\\\\\\\\)*\\//';
$matches = [];
// only pattern with delimiters and supported modifiers (by PHP *and* JS)
if (@preg_match($VALID_REGEX, $extraSettings['validationRegex'], $matches) !== 1) {
return false;
}
// We use slashes as delimters, so unescaped slashes within the pattern are **not** allowed
if (@preg_match($REGEX_UNESCAPED_SLASHES, $matches[1]) === 1) {
return false;
}
// Try to compile the given pattern, `preg_match` will return false if the pattern is invalid
if (@preg_match($extraSettings['validationRegex'], 'some string') === false) {
return false;
}
}
}
return true;
}
public function getFilePath(Form $form): ?string {
$fileId = $form->getFileId();
if ($fileId === null) {
return null;
}
$folder = $this->storage->getUserFolder($form->getOwnerId());
$nodes = $folder->getById($fileId);
if (empty($nodes)) {
throw new NotFoundException('File not found');
}
$internalPath = array_shift($nodes)->getPath();
return $folder->getRelativePath($internalPath);
}
public function getFileName(Form $form, string $fileFormat): string {
if (!isset(Constants::SUPPORTED_EXPORT_FORMATS[$fileFormat])) {
throw new \InvalidArgumentException('Invalid file format');
}
// TRANSLATORS Appendix for CSV-Export: 'Form Title (responses).csv'
$fileName = $form->getTitle() . ' (' . $this->l10n->t('responses') . ').'.$fileFormat;
return self::normalizeFileName($fileName);
}
public function getFormUploadedFilesFolderPath(Form $form): string {
return implode('/', [
Constants::FILES_FOLDER,
self::normalizeFileName($form->getId().' - '.$form->getTitle()),
]);
}
public function getUploadedFilePath(Form $form, int $submissionId, int $questionId, ?string $questionName, string $questionText): string {
return implode('/', [
$this->getFormUploadedFilesFolderPath($form),
$submissionId,
self::normalizeFileName($questionId.' - '.($questionName ?: $questionText))
]);
}
public function getTemporaryUploadedFilePath(Form $form, Question $question): string {
return implode('/', [
Constants::UNSUBMITTED_FILES_FOLDER,
microtime(true),
self::normalizeFileName($form->getId().' - '.$form->getTitle()),
self::normalizeFileName($question->getId().' - '.($question->getName() ?: $question->getText()))
]);
}
private static function normalizeFileName(string $fileName): string {
return str_replace(mb_str_split(\OCP\Constants::FILENAME_INVALID_CHARS), '-', $fileName);
}
}