ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
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 ilAccessHandler;
28 use ilDBInterface;
29 use Closure;
30 use ilObject;
31 
33 {
40  private Closure $type_of;
41 
46  public function __construct(
47  ilDBInterface $database,
48  ilAccessHandler $access,
49  ?Incident $incident = null,
50  $references_of = [ilObject::class, '_getAllReferences'],
51  $type_of = [ilObject::class, '_lookupType']
52  ) {
53  $this->database = $database;
54  $this->access = $access;
55  $this->incident = $incident ?? new Incident();
56  $this->references_of = Closure::fromCallable($references_of);
57  $this->type_of = Closure::fromCallable($type_of);
58  }
59 
60  public function isPermitted(string $path): Result
61  {
62  $question_id = $this->questionId($path);
63  if (!$question_id) {
64  return new Error('Not a question image path of test questions.');
65  }
66 
67  $object_id = $this->objectId($question_id);
68  if (!$object_id) {
69  return new Ok(false);
70  }
71 
72  $permitted = $this->incident->any([$this, 'refIdPermitted'], ($this->references_of)($object_id));
73 
74  return new Ok($permitted);
75  }
76 
77  public function refIdPermitted(int $ref_id): bool
78  {
79  $ref_id = $ref_id;
80  $type = ($this->type_of)($ref_id, true);
81 
82  switch ($type) {
83  case 'qpl': return $this->access->checkAccess('read', '', $ref_id);
84  case 'tst': return $this->access->checkAccess('write', '', $ref_id);
85  default: return false;
86  }
87  }
88 
89  private function questionId(string $path): ?int
90  {
91  $results = [];
92  if (!preg_match(':/assessment/qst_preview/\d+/(\d+)/fileuploads/([^/]+)$:', $path, $results)) {
93  return null;
94  }
95 
96  return (int) $results[1];
97  }
98 
99  private function objectId(int $question_id): ?int
100  {
101  $object_id = $this->database->fetchAssoc($this->database->queryF(
102  'SELECT obj_fi FROM qpl_questions WHERE question_id = %s',
104  [$question_id]
105  ))['obj_fi'] ?? null;
106 
107  return $object_id ? (int) $object_id : null;
108  }
109 }
__construct(ilDBInterface $database, ilAccessHandler $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: 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