ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilAccessCustomRBACOperationAddedObjective.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use ILIAS\Setup;
23use ILIAS\DI;
24
26{
27 private const NO_DIC_FOUND = 'There is no $DIC.';
28
29 protected string $id;
30 protected string $title;
31 protected string $class;
32 protected int $pos;
33 protected array $types;
34
35 public function __construct(string $id, string $title, string $class, int $pos, array $types = [])
36 {
37 $this->id = $id;
38 $this->title = $title;
39 $this->class = $class;
40 $this->pos = $pos;
41 $this->types = $types;
42 }
43
44 public function getHash(): string
45 {
46 return hash("sha256", self::class . "::" . $this->id);
47 }
48
49 public function getLabel(): string
50 {
51 $types = implode(",", $this->types);
52 return "Add custom rbac operation (id=$this->id;title=$this->title;class=$this->class;pos=$this->pos;types=($types))";
53 }
54
55 public function isNotable(): bool
56 {
57 return true;
58 }
59
60 public function getPreconditions(Environment $environment): array
61 {
62 return [
64 ];
65 }
66
67 public function achieve(Environment $environment): Environment
68 {
69 $db = $environment->getResource(Environment::RESOURCE_DATABASE);
70
71 $dic = $this->initEnvironment($environment);
72
73 if ($this->class == "create") {
74 $this->pos = 9999;
75 }
76
77 $ops_id = ilRbacReview::_getCustomRBACOperationId($this->id, $db);
78 if (is_null($ops_id)) {
79 $ops_id = $db->nextId("rbac_operations");
80
81 $values = [
82 'ops_id' => ['integer', $ops_id],
83 'operation' => ['text', $this->id],
84 'description' => ['text', $this->title],
85 'class' => ['text', $this->class],
86 'op_order' => ['integer', $this->pos]
87 ];
88
89 $db->insert("rbac_operations", $values);
90 }
91
92 foreach ($this->types as $type) {
93 $type_id = ilObject::_getObjectTypeIdByTitle($type, $db);
94 if (!$type_id) {
95 $type_id = $db->nextId('object_data');
96
97 $fields = [
98 'obj_id' => ['integer', $type_id],
99 'type' => ['text', 'typ'],
100 'title' => ['text', $type],
101 'description' => ['text', $this->title],
102 'owner' => ['integer', -1],
103 'create_date' => ['timestamp', $db->now()],
104 'last_update' => ['timestamp', $db->now()]
105 ];
106 $db->insert('object_data', $fields);
107 }
108
109 $sql =
110 "SELECT typ_id, ops_id " . PHP_EOL
111 . "FROM rbac_ta" . PHP_EOL
112 . "WHERE typ_id = " . $db->quote($type_id, "integer") . PHP_EOL
113 . "AND ops_id = " . $db->quote($ops_id, 'integer') . PHP_EOL
114 ;
115
116 $result = $db->query($sql);
117 if ($db->numRows($result)) {
118 continue;
119 }
120
121 $values = [
122 "typ_id" => ["integer", $type_id],
123 "ops_id" => ["integer", $ops_id]
124 ];
125
126 $db->insert("rbac_ta", $values);
127 }
128
129 $this->resetDIC($dic);
130 return $environment;
131 }
132
133 public function isApplicable(Environment $environment): bool
134 {
135 $db = $environment->getResource(Environment::RESOURCE_DATABASE);
136
137 $dic = $this->initEnvironment($environment);
138
139 $ops_id = ilRbacReview::_getCustomRBACOperationId($this->id, $db);
140 if (!$ops_id) {
141 return true;
142 }
143
144 foreach ($this->types as $key => $type) {
145 $type_id = ilObject::_getObjectTypeIdByTitle($type, $db);
146 if (is_null($type_id)) {
147 return true;
148 }
149
150 $sql =
151 "SELECT typ_id, ops_id " . PHP_EOL
152 . "FROM rbac_ta" . PHP_EOL
153 . "WHERE typ_id = " . $db->quote($type_id, "integer") . PHP_EOL
154 . "AND ops_id = " . $db->quote($ops_id, 'integer') . PHP_EOL
155 ;
156
157 $result = $db->query($sql);
158 if ($db->numRows($result)) {
159 unset($this->types[$key]);
160 }
161 }
162
163 $this->resetDIC($dic);
164 return count($this->types) && in_array($this->class, ['create', 'object', 'general']);
165 }
166
167 protected function initEnvironment(Setup\Environment $environment)
168 {
169 $db = $environment->getResource(Setup\Environment::RESOURCE_DATABASE);
170
171 // ATTENTION: This is a total abomination. It only exists to allow various
172 // subcomponents of the various readers to run. This is a memento to the
173 // fact, that dependency injection is something we want. Currently, every
174 // component could just service locate the whole world via the global $DIC.
176 if (array_key_exists('DIC', $GLOBALS)) {
177 $DIC = $GLOBALS['DIC'];
178 }
179 $GLOBALS['DIC'] = new DI\Container();
180 $GLOBALS['DIC']['ilDB'] = $db;
181
182 if (!defined('ILIAS_ABSOLUTE_PATH')) {
183 define('ILIAS_ABSOLUTE_PATH', dirname(__FILE__, 6));
184 }
185
186 return $DIC;
187 }
188
189 protected function resetDIC($dic): void
190 {
191 if ($dic !== self::NO_DIC_FOUND) {
192 $GLOBALS['DIC'] = $dic;
193 return;
194 }
195 unset($GLOBALS['DIC']);
196 }
197}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
__construct(string $id, string $title, string $class, int $pos, array $types=[])
static _getObjectTypeIdByTitle(string $type, ?\ilDBInterface $ilDB=null)
static _getCustomRBACOperationId(string $operation, ?\ilDBInterface $ilDB=null)
An environment holds resources to be used in the setup process.
Definition: Environment.php:28
getResource(string $id)
Consumers of this method should check if the result is what they expect, e.g.
An objective is a desired state of the system that is supposed to be created by the setup.
Definition: Objective.php:31
$dic
Definition: ltiresult.php:33
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Container.php:19
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:26
$GLOBALS["DIC"]
Definition: wac.php:54