ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilShibbolethRoleAssignmentRule.php
Go to the documentation of this file.
1<?php
2
30{
34 private const ERR_MISSING_NAME = 'shib_missing_attr_name';
38 private const ERR_MISSING_VALUE = 'shib_missing_attr_value';
42 private const ERR_MISSING_ROLE = 'shib_missing_role';
46 private const ERR_MISSING_PLUGIN_ID = 'shib_missing_plugin_id';
50 private const TABLE_NAME = 'shib_role_assignment';
51
53 private int $role_id = 0;
54 private string $attribute_name = '';
55 private string $attribute_value = '';
56 private bool $plugin_active = false;
57 private bool $add_on_update = false;
58 private bool $remove_on_update = false;
59 private ?string $plugin_id = null;
60
61 public function __construct(private int $rule_id = 0)
62 {
63 global $DIC;
64 $this->db = $DIC->database();
65 $this->read();
66 }
67
68 public function setRuleId(int $a_id): void
69 {
70 $this->rule_id = $a_id;
71 }
72
73 public function getRuleId(): int
74 {
75 return $this->rule_id;
76 }
77
78 public function setRoleId(int $a_id): void
79 {
80 $this->role_id = $a_id;
81 }
82
83 public function getRoleId(): int
84 {
85 return $this->role_id;
86 }
87
88 public function setName(string $a_name): void
89 {
90 $this->attribute_name = $a_name;
91 }
92
93 public function getName(): string
94 {
96 }
97
98 public function setValue(string $a_value): void
99 {
100 $this->attribute_value = $a_value;
101 }
102
103 public function getValue(): string
104 {
106 }
107
108 public function enablePlugin(bool $a_status): void
109 {
110 $this->plugin_active = $a_status;
111 }
112
113 public function isPluginActive(): bool
114 {
116 }
117
118 public function enableAddOnUpdate(bool $a_status): void
119 {
120 $this->add_on_update = $a_status;
121 }
122
123 public function isAddOnUpdateEnabled(): bool
124 {
126 }
127
128 public function enableRemoveOnUpdate(bool $a_status): void
129 {
130 $this->remove_on_update = $a_status;
131 }
132
133 public function isRemoveOnUpdateEnabled(): bool
134 {
136 }
137
138 public function setPluginId(?string $a_id): void
139 {
140 $this->plugin_id = $a_id;
141 }
142
143 public function getPluginId(): ?string
144 {
145 return $this->plugin_id;
146 }
147
148 public function conditionToString(): ?string
149 {
150 global $DIC;
151 $lng = $DIC['lng'];
152 if ($this->isPluginActive()) {
153 return $lng->txt('shib_plugin_id') . ': ' . $this->getPluginId();
154 }
155
156 return $this->getName() . '=' . $this->getValue();
157 }
158
159 public function validate(): string
160 {
161 if ($this->getRoleId() === 0) {
163 }
164 if (!$this->isPluginActive()) {
165 if ($this->getName() === '' || $this->getName() === '0') {
167 }
168 if ($this->getValue() === '' || $this->getValue() === '0') {
170 }
171 } elseif ($this->getPluginId() === 0) {
173 }
174
175 return '';
176 }
177
178 public function delete(): bool
179 {
180 $query = 'DELETE FROM ' . self::TABLE_NAME . ' ' . 'WHERE rule_id = ' . $this->db->quote(
181 $this->getRuleId(),
182 'integer'
183 );
184 $this->db->manipulate($query);
185
186 return true;
187 }
188
189 public function add(): bool
190 {
191 $next_id = $this->db->nextId(self::TABLE_NAME);
192 $query = 'INSERT INTO ' . self::TABLE_NAME . ' (rule_id,role_id,name,value,plugin,plugin_id,add_on_update,remove_on_update ) ' . 'VALUES( '
193 . $this->db->quote($next_id, 'integer') . ', ' . $this->db->quote($this->getRoleId(), 'integer') . ', '
194 . $this->db->quote($this->getName(), 'text') . ', ' . $this->db->quote($this->getValue(), 'text') . ', '
195 . $this->db->quote((int) $this->isPluginActive(), 'integer') . ', ' . $this->db->quote(
196 $this->getPluginId() ?? '0',
197 'integer'
198 ) . ', '
199 . $this->db->quote((int) $this->isAddOnUpdateEnabled(), 'integer') . ', '
200 . $this->db->quote((int) $this->isRemoveOnUpdateEnabled(), 'integer') . ') ';
201 $this->db->manipulate($query);
202 $this->setRuleId($this->db->getLastInsertId());
203
204 return true;
205 }
206
207 public function update(): bool
208 {
209 $query = 'UPDATE ' . self::TABLE_NAME . ' ' . 'SET role_id = ' . $this->db->quote(
210 $this->getRoleId(),
211 'integer'
212 ) . ', ' . 'name = '
213 . $this->db->quote($this->getName(), 'text') . ', ' . 'value = ' . $this->db->quote(
214 $this->getValue(),
215 'text'
216 ) . ', ' . 'plugin = '
217 . $this->db->quote((int) $this->isPluginActive(), 'integer') . ', ' . 'plugin_id = '
218 . $this->db->quote($this->getPluginId() ?? '0', 'integer') . ', ' . 'add_on_update = '
219 . $this->db->quote((int) $this->isAddOnUpdateEnabled(), 'integer') . ', ' . 'remove_on_update = '
220 . $this->db->quote((int) $this->isRemoveOnUpdateEnabled(), 'integer') . ' '
221 . 'WHERE rule_id = ' . $this->db->quote($this->getRuleId(), 'integer');
222 $this->db->manipulate($query);
223
224 return true;
225 }
226
230 public function matches(array $a_data): bool
231 {
232 if ($this->isPluginActive()) {
234 }
235 // No value
236 if (!isset($a_data[$this->getName()])) {
237 return false;
238 }
239 $values = $a_data[$this->getName()];
240 if (is_array($values)) {
241 return in_array($this->getValue(), $values);
242 }
243
244 return $this->wildcardCompare($this->getValue(), $values);
245 }
246
250 protected function wildcardCompare(string $a_str1, string $a_str2): bool
251 {
252 $pattern = str_replace('*', '.*?', $a_str1);
253
254 return (bool) preg_match("/" . $pattern . "/us", $a_str2);
255 }
256
257 public function doesMatch(array $a_data): bool
258 {
259 if ($this->isPluginActive()) {
261 }
262
263 if (!isset($a_data[$this->getName()])) {
264 return false;
265 }
266
267 $values = $a_data[$this->getName()];
268 if (is_array($values)) {
269 return in_array($this->getValue(), $values);
270 }
271
272 $pattern = str_replace('*', '.*?', $this->getValue());
273
274 return (bool) preg_match('/^' . $pattern . '$/us', (string) $values);
275 }
276
277 private function read(): void
278 {
279 if ($this->getRuleId() === 0) {
280 return;
281 }
282
283 $query = 'SELECT * FROM ' . self::TABLE_NAME . ' ' . 'WHERE rule_id = ' . $this->db->quote(
284 $this->getRuleId(),
285 'integer'
286 );
287 $res = $this->db->query($query);
288 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
289 $this->setRoleId((int) $row->role_id);
290 $this->setName((string) $row->name);
291 $this->setValue((string) $row->value);
292 $this->enablePlugin((bool) $row->plugin);
293 $this->setPluginId($row->plugin_id);
294 $this->enableAddOnUpdate((bool) $row->add_on_update);
295 $this->enableRemoveOnUpdate((bool) $row->remove_on_update);
296 }
297 }
298}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static callPlugin(string $a_plugin_id, array $a_user_data)
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
global $lng
Definition: privfeed.php:31
global $DIC
Definition: shib_login.php:26