ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
AccessFileUploadPreview.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Test\Access;
22
29use Closure;
30use ilObject;
33
35{
40 private Closure $references_of;
42 private Closure $type_of;
43
48 public function __construct(
51 ?Incident $incident = null,
52 $references_of = [ilObject::class, '_getAllReferences'],
53 $type_of = [ilObject::class, '_lookupType']
54 ) {
55 $this->database = $database;
56 $this->access = $access;
57 $this->incident = $incident ?? new Incident();
58 $this->references_of = Closure::fromCallable($references_of);
59 $this->type_of = Closure::fromCallable($type_of);
60 }
61
62 public function isPermitted(string $path): Result
63 {
64 $question_id = $this->questionId($path);
65 if (!$question_id) {
66 return new Error('Not a question image path of test questions.');
67 }
68
69 $object_id = $this->objectId($question_id);
70 if (!$object_id) {
71 return new Ok(false);
72 }
73
74 $permitted = $this->incident->any([$this, 'refIdPermitted'], ($this->references_of)($object_id));
75
76 return new Ok($permitted);
77 }
78
79 public function refIdPermitted(int $ref_id): bool
80 {
82 $type = ($this->type_of)($ref_id, true);
83
84 switch ($type) {
85 case 'qpl': return $this->access->checkAccess('read', '', $ref_id);
86 case 'tst': return $this->access->checkAccess('write', '', $ref_id);
87 default: return false;
88 }
89 }
90
91 private function questionId(string $path): ?int
92 {
93 $results = [];
94 if (!preg_match(':/assessment/qst_preview/\d+/(\d+)/fileuploads/([^/]+)$:', $path, $results)) {
95 return null;
96 }
97
98 return (int) $results[1];
99 }
100
101 private function objectId(int $question_id): ?int
102 {
103 $object_id = $this->database->fetchAssoc($this->database->queryF(
104 'SELECT obj_fi FROM qpl_questions WHERE question_id = %s',
106 [$question_id]
107 ))['obj_fi'] ?? null;
108
109 return $object_id ? (int) $object_id : null;
110 }
111}
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:31
__construct(ilDBInterface $database, ilAccessHandler $access, ?Incident $incident=null, $references_of=[ilObject::class, '_getAllReferences'], $type_of=[ilObject::class, '_lookupType'])
Class ilDBConstants.
Class ilObject Basic functions for all objects.
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:29
Interface ilAccessHandler This interface combines all available interfaces which can be called via gl...
Interface ilDBInterface.
$ref_id
Definition: ltiauth.php:66
$path
Definition: ltiservices.php:30
$results