ILIAS  release_7 Revision v7.30-3-g800a261c036
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 $database;
37  private $access;
39  private $incident;
41  private $references_of;
43  private $type_of;
44 
52  public function __construct(
55  ?Incident $incident = null,
56  $references_of = [ilObject::class, '_getAllReferences'],
57  $type_of = [ilObject::class, '_lookupType']
58  ) {
59  $this->database = $database;
60  $this->access = $access;
61  $this->incident = $incident ?? new Incident();
62  $this->references_of = Closure::fromCallable($references_of);
63  $this->type_of = Closure::fromCallable($type_of);
64  }
65 
66  public function isPermitted(string $path): Result
67  {
68  $question_id = $this->questionId($path);
69  if (!$question_id) {
70  return new Error('Not a question image path of test questions.');
71  }
72 
73  $object_id = $this->objectId($question_id);
74  if (!$object_id) {
75  return new Ok(false);
76  }
77 
78  $permitted = $this->incident->any([$this, 'refIdPermitted'], ($this->references_of)($object_id));
79 
80  return new Ok($permitted);
81  }
82 
86  public function refIdPermitted($ref_id): bool
87  {
88  $ref_id = (int) $ref_id;
89  $type = ($this->type_of)($ref_id, true);
90 
91  switch ($type) {
92  case 'qpl': return $this->access->checkAccess('read', '', $ref_id);
93  case 'tst': return $this->access->checkAccess('write', '', $ref_id);
94  default: return false;
95  }
96  }
97 
98  private function questionId(string $path): ?int
99  {
100  $results = [];
101  if (!preg_match(':/assessment/qst_preview/\d+/(\d+)/fileuploads/([^/]+)$:', $path, $results)) {
102  return null;
103  }
104 
105  return (int) $results[1];
106  }
107 
108  private function objectId(int $question_id): ?int
109  {
110  $object_id = $this->database->fetchAssoc($this->database->queryF(
111  'SELECT obj_fi FROM qpl_questions WHERE question_id = %s',
113  [$question_id]
114  ))['obj_fi'] ?? null;
115 
116  return $object_id ? (int) $object_id : null;
117  }
118 }
$type
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:11
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:13
$results
__construct(ilDBInterface $database, ilAccess $access, ?Incident $incident=null, $references_of=[ilObject::class, '_getAllReferences'], $type_of=[ilObject::class, '_lookupType'])
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Error.php:13