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