ILIAS  release_8 Revision v8.24
AccessFileUploadPreview.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Modules\Test;
22
27use ilAccess;
29use Closure;
30use ilObject;
31
33{
38 private Closure $references_of;
40 private Closure $type_of;
41
49 public function __construct(
52 ?Incident $incident = null,
53 $references_of = [ilObject::class, '_getAllReferences'],
54 $type_of = [ilObject::class, '_lookupType']
55 ) {
56 $this->database = $database;
57 $this->access = $access;
58 $this->incident = $incident ?? new Incident();
59 $this->references_of = Closure::fromCallable($references_of);
60 $this->type_of = Closure::fromCallable($type_of);
61 }
62
63 public function isPermitted(string $path): Result
64 {
65 $question_id = $this->questionId($path);
66 if (!$question_id) {
67 return new Error('Not a question image path of test questions.');
68 }
69
70 $object_id = $this->objectId($question_id);
71 if (!$object_id) {
72 return new Ok(false);
73 }
74
75 $permitted = $this->incident->any([$this, 'refIdPermitted'], ($this->references_of)($object_id));
76
77 return new Ok($permitted);
78 }
79
83 public function refIdPermitted(int $ref_id): bool
84 {
86 $type = ($this->type_of)($ref_id, true);
87
88 switch ($type) {
89 case 'qpl': return $this->access->checkAccess('read', '', $ref_id);
90 case 'tst': return $this->access->checkAccess('write', '', $ref_id);
91 default: return false;
92 }
93 }
94
95 private function questionId(string $path): ?int
96 {
97 $results = [];
98 if (!preg_match(':/assessment/qst_preview/\d+/(\d+)/fileuploads/([^/]+)$:', $path, $results)) {
99 return null;
100 }
101
102 return (int) $results[1];
103 }
104
105 private function objectId(int $question_id): ?int
106 {
107 $object_id = $this->database->fetchAssoc($this->database->queryF(
108 'SELECT obj_fi FROM qpl_questions WHERE question_id = %s',
110 [$question_id]
111 ))['obj_fi'] ?? null;
112
113 return $object_id ? (int) $object_id : null;
114 }
115}
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Error.php:18
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:17
__construct(ilDBInterface $database, ilAccess $access, ?Incident $incident=null, $references_of=[ilObject::class, '_getAllReferences'], $type_of=[ilObject::class, '_lookupType'])
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:15
Interface ilDBInterface.
$ref_id
Definition: ltiauth.php:67
$path
Definition: ltiservices.php:32
$type
$results