ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
AccessFileUploadPreview.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Modules\Test;
22 
26 use ilDBConstants;
27 use ilAccess;
28 use ilDBInterface;
29 use Closure;
30 use ilObject;
31 
33 {
35  private ilAccess $access;
40  private Closure $type_of;
41 
49  public function __construct(
50  ilDBInterface $database,
51  ilAccess $access,
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  {
85  $ref_id = $ref_id;
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 }
$type
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:14
$path
Definition: ltiservices.php:32
$ref_id
Definition: ltiauth.php:67
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:16
$results
__construct(ilDBInterface $database, ilAccess $access, ?Incident $incident=null, $references_of=[ilObject::class, '_getAllReferences'], $type_of=[ilObject::class, '_lookupType'])