ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilShibbolethRoleAssignmentRule.php
Go to the documentation of this file.
1 <?php
2 /******************************************************************************
3  *
4  * This file is part of ILIAS, a powerful learning management system.
5  *
6  * ILIAS is licensed with the GPL-3.0, you should have received a copy
7  * of said license along with the source code.
8  *
9  * If this is not the case or you just want to try ILIAS, you'll find
10  * us at:
11  * https://www.ilias.de
12  * https://github.com/ILIAS-eLearning
13  *
14  *****************************************************************************/
26 {
27  private const ERR_MISSING_NAME = 'shib_missing_attr_name';
28  private const ERR_MISSING_VALUE = 'shib_missing_attr_value';
29  private const ERR_MISSING_ROLE = 'shib_missing_role';
30  private const ERR_MISSING_PLUGIN_ID = 'shib_missing_plugin_id';
31  private const TABLE_NAME = 'shib_role_assignment';
32 
33  private ilDBInterface $db;
34  private int $rule_id;
35  private int $role_id = 0;
36  private string $attribute_name = '';
37  private string $attribute_value = '';
38  private bool $plugin_active = false;
39  private bool $add_on_update = false;
40  private bool $remove_on_update = false;
41  private ?string $plugin_id = null;
42 
43  public function __construct(int $a_rule_id = 0)
44  {
45  global $DIC;
46  $ilDB = $DIC['ilDB'];
47  $this->db = $ilDB;
48  $this->rule_id = $a_rule_id;
49  $this->read();
50  }
51 
52  public function setRuleId(int $a_id): void
53  {
54  $this->rule_id = $a_id;
55  }
56 
57  public function getRuleId(): int
58  {
59  return $this->rule_id;
60  }
61 
62  public function setRoleId(int $a_id): void
63  {
64  $this->role_id = $a_id;
65  }
66 
67  public function getRoleId(): int
68  {
69  return $this->role_id;
70  }
71 
72  public function setName(string $a_name): void
73  {
74  $this->attribute_name = $a_name;
75  }
76 
77  public function getName(): string
78  {
79  return $this->attribute_name;
80  }
81 
82  public function setValue(string $a_value): void
83  {
84  $this->attribute_value = $a_value;
85  }
86 
87  public function getValue(): string
88  {
90  }
91 
92  public function enablePlugin(bool $a_status): void
93  {
94  $this->plugin_active = $a_status;
95  }
96 
97  public function isPluginActive(): bool
98  {
99  return $this->plugin_active;
100  }
101 
102  public function enableAddOnUpdate(bool $a_status): void
103  {
104  $this->add_on_update = $a_status;
105  }
106 
107  public function isAddOnUpdateEnabled(): bool
108  {
109  return $this->add_on_update;
110  }
111 
112  public function enableRemoveOnUpdate(bool $a_status): void
113  {
114  $this->remove_on_update = $a_status;
115  }
116 
117  public function isRemoveOnUpdateEnabled(): bool
118  {
120  }
121 
122  public function setPluginId(?string $a_id): void
123  {
124  $this->plugin_id = $a_id;
125  }
126 
127  public function getPluginId(): ?string
128  {
129  return $this->plugin_id;
130  }
131 
132  public function conditionToString(): ?string
133  {
134  global $DIC;
135  $lng = $DIC['lng'];
136  if ($this->isPluginActive()) {
137  return $lng->txt('shib_plugin_id') . ': ' . $this->getPluginId();
138  }
139 
140  return $this->getName() . '=' . $this->getValue();
141  }
142 
143  public function validate(): string
144  {
145  if ($this->getRoleId() === 0) {
146  return self::ERR_MISSING_ROLE;
147  }
148  if (!$this->isPluginActive()) {
149  if ($this->getName() === '' || $this->getName() === '0') {
150  return self::ERR_MISSING_NAME;
151  }
152  if ($this->getValue() === '' || $this->getValue() === '0') {
153  return self::ERR_MISSING_VALUE;
154  }
155  } elseif ($this->getPluginId() === 0) {
156  return self::ERR_MISSING_PLUGIN_ID;
157  }
158 
159  return '';
160  }
161 
162  public function delete(): bool
163  {
164  $query = 'DELETE FROM ' . self::TABLE_NAME . ' ' . 'WHERE rule_id = ' . $this->db->quote(
165  $this->getRuleId(),
166  'integer'
167  );
168  $this->db->manipulate($query);
169 
170  return true;
171  }
172 
173  public function add(): bool
174  {
175  $next_id = $this->db->nextId(self::TABLE_NAME);
176  $query = 'INSERT INTO ' . self::TABLE_NAME . ' (rule_id,role_id,name,value,plugin,plugin_id,add_on_update,remove_on_update ) ' . 'VALUES( '
177  . $this->db->quote($next_id, 'integer') . ', ' . $this->db->quote($this->getRoleId(), 'integer') . ', '
178  . $this->db->quote($this->getName(), 'text') . ', ' . $this->db->quote($this->getValue(), 'text') . ', '
179  . $this->db->quote((int) $this->isPluginActive(), 'integer') . ', ' . $this->db->quote(
180  $this->getPluginId() ?? 0,
181  'integer'
182  ) . ', '
183  . $this->db->quote((int) $this->isAddOnUpdateEnabled(), 'integer') . ', '
184  . $this->db->quote((int) $this->isRemoveOnUpdateEnabled(), 'integer') . ') ';
185  $this->db->manipulate($query);
186  $this->setRuleId($this->db->getLastInsertId());
187 
188  return true;
189  }
190 
191  public function update(): bool
192  {
193  $query = 'UPDATE ' . self::TABLE_NAME . ' ' . 'SET role_id = ' . $this->db->quote(
194  $this->getRoleId(),
195  'integer'
196  ) . ', ' . 'name = '
197  . $this->db->quote($this->getName(), 'text') . ', ' . 'value = ' . $this->db->quote(
198  $this->getValue(),
199  'text'
200  ) . ', ' . 'plugin = '
201  . $this->db->quote((int) $this->isPluginActive(), 'integer') . ', ' . 'plugin_id = '
202  . $this->db->quote($this->getPluginId() ?? 0, 'integer') . ', ' . 'add_on_update = '
203  . $this->db->quote((int) $this->isAddOnUpdateEnabled(), 'integer') . ', ' . 'remove_on_update = '
204  . $this->db->quote((int) $this->isRemoveOnUpdateEnabled(), 'integer') . ' '
205  . 'WHERE rule_id = ' . $this->db->quote($this->getRuleId(), 'integer');
206  $this->db->manipulate($query);
207 
208  return true;
209  }
210 
214  public function matches(array $a_data): bool
215  {
216  if ($this->isPluginActive()) {
218  }
219  // No value
220  if (!isset($a_data[$this->getName()])) {
221  return false;
222  }
223  $values = $a_data[$this->getName()];
224  if (is_array($values)) {
225  return in_array($this->getValue(), $values);
226  }
227 
228  return $this->wildcardCompare($this->getValue(), $values);
229  }
230 
234  protected function wildcardCompare(string $a_str1, string $a_str2): bool
235  {
236  $pattern = str_replace('*', '.*?', $a_str1);
237 
238  return (bool) preg_match("/" . $pattern . "/us", $a_str2);
239  }
240 
241  public function doesMatch(array $a_data): bool
242  {
243  if ($this->isPluginActive()) {
245  }
246 
247  if (!isset($a_data[$this->getName()])) {
248  return false;
249  }
250 
251  $values = $a_data[$this->getName()];
252  if (is_array($values)) {
253  return in_array($this->getValue(), $values);
254  }
255 
256  $pattern = str_replace('*', '.*?', $this->getValue());
257 
258  return (bool) preg_match('/^' . $pattern . '$/us', $values);
259  }
260 
261  private function read(): void
262  {
263  if ($this->getRuleId() === 0) {
264  return;
265  }
266 
267  $query = 'SELECT * FROM ' . self::TABLE_NAME . ' ' . 'WHERE rule_id = ' . $this->db->quote(
268  $this->getRuleId(),
269  'integer'
270  );
271  $res = $this->db->query($query);
272  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
273  $this->setRoleId((int) $row->role_id);
274  $this->setName((string) $row->name);
275  $this->setValue((string) $row->value);
276  $this->enablePlugin((bool) $row->plugin);
277  $this->setPluginId($row->plugin_id);
278  $this->enableAddOnUpdate((bool) $row->add_on_update);
279  $this->enableRemoveOnUpdate((bool) $row->remove_on_update);
280  }
281  }
282 }
$res
Definition: ltiservices.php:69
$lng
static callPlugin(string $a_plugin_id, array $a_user_data)
global $DIC
Definition: feed.php:28
$query