ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
AccessFileUploadPreview.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Test\Access;
22 
26 use ilDBConstants;
27 use ilAccessHandler;
28 use ilDBInterface;
29 use Closure;
30 use ilObject;
33 
35 {
42  private Closure $type_of;
43 
48  public function __construct(
49  ilDBInterface $database,
50  ilAccessHandler $access,
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  {
81  $ref_id = $ref_id;
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 }
$path
Definition: ltiservices.php:29
__construct(ilDBInterface $database, ilAccessHandler $access, ?Incident $incident=null, $references_of=[ilObject::class, '_getAllReferences'], $type_of=[ilObject::class, '_lookupType'])
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
$ref_id
Definition: ltiauth.php:65
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:30
$results