ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilOrgUnitPermissionDBRepository.php
Go to the documentation of this file.
1 <?php
2 
20 declare(strict_types=1);
21 
23 {
24  public const TABLE_NAME = 'il_orgu_permissions';
26  protected ilDBInterface $db;
28 
30  {
31  $this->db = $db;
32  $this->operationRepo = $operationRepo;
33  $this->contextRepo = $contextRepo;
34  }
35 
36  public function get(int $parent_id, int $position_id): ilOrgUnitPermission
37  {
38  if ($position_id === 0) {
39  throw new ilException('$position_id cannot be 0');
40  }
41  if ($parent_id <= 0) {
42  throw new ilException('$parent_id cannot be <=0');
43  }
44 
45  $context = $this->contextRepo->getByRefId($parent_id);
46  if (!$context) {
47  throw new ilException('Context for ref_id ' . $parent_id . ' not found');
48  }
49 
50  if (!$this->isContextEnabled($context->getContext())) {
52  "Position-related permissions not active in {$context->getContext()}",
53  $context->getContext()
54  );
55  }
56 
57  $permission = $this->find($parent_id, $position_id);
58  if ($permission) {
59  return $permission;
60  }
61 
62  $template = $this->getDefaultForContext($context->getContext(), $position_id);
63  $permission = (new ilOrgUnitPermission())
64  ->withParentId($parent_id)
65  ->withContextId($context->getId())
66  ->withPositionId($position_id)
67  ->withOperations($template->getOperations())
68  ->withProtected(false);
69  $permission = $this->store($permission);
70 
71  return $permission;
72  }
73 
74  public function find(int $parent_id, int $position_id): ?ilOrgUnitPermission
75  {
76  if ($position_id === 0) {
77  throw new ilException('$position_id cannot be 0');
78  }
79  if ($parent_id <= 0) {
80  throw new ilException('$parent_id cannot be <=0');
81  }
82 
83  $context = $this->contextRepo->getByRefId($parent_id);
84  if (!$context) {
85  return null;
86  }
87 
88  if (!$this->isContextEnabled($context->getContext())) {
89  return null;
90  }
91 
92  $query = 'SELECT id, parent_id, context_id, position_id, protected, operations FROM' . PHP_EOL
93  . self::TABLE_NAME
94  . ' WHERE ' . self::TABLE_NAME . '.parent_id = ' . $this->db->quote($parent_id, 'integer') . PHP_EOL
95  . ' AND ' . self::TABLE_NAME . '.position_id = ' . $this->db->quote($position_id, 'integer') . PHP_EOL
96  . ' AND ' . self::TABLE_NAME . '.context_id = ' . $this->db->quote($context->getId(), 'integer') . PHP_EOL;
97  $res = $this->db->query($query);
98  if ($res->numRows() === 0) {
99  return null;
100  }
101 
102  $rec = $this->db->fetchAssoc($res);
103  $ret = (new ilOrgUnitPermission((int) $rec['id']))
104  ->withParentId((int) $rec["parent_id"])
105  ->withContextId((int) $rec['context_id'])
106  ->withPositionId((int) $rec['position_id'])
107  ->withProtected((bool) $rec['protected'])
108  ->withOperations($this->convertToArray((string) $rec["operations"]));
109 
110  $ret = $this->update($ret);
111  return $ret;
112  }
113 
114  public function store(ilOrgUnitPermission $permission): ilOrgUnitPermission
115  {
116  if ($permission->getId() === 0) {
117  $permission = $this->insertDB($permission);
118  } else {
119  if ($permission->isProtected()) {
120  throw new ilException("Protected permission " . $permission->getId() . " can not be updated");
121  }
122  if ($permission->getParentId() == ilOrgUnitPermission::PARENT_TEMPLATE) {
123  $permission = $permission->withProtected(true);
124  }
125  $this->updateDB($permission);
126  }
127 
128  $permission = $this->update($permission);
129  return $permission;
130  }
131 
132  private function insertDB(ilOrgUnitPermission $permission): ilOrgUnitPermission
133  {
134  $id = $this->db->nextId(self::TABLE_NAME);
135 
136  $values = [
137  'id' => [ 'integer', $id ],
138  'parent_id' => [ 'string', $permission->getParentId()],
139  'context_id' => [ 'string', $permission->getContextId()],
140  'position_id' => [ 'integer', $permission->getPositionId() ],
141  'protected' => [ 'integer', ($permission->isProtected()) ? 1 : 0],
142  'operations' => [ 'string', $this->convertToJson($permission->getOperations())]
143  ];
144 
145  $this->db->insert(self::TABLE_NAME, $values);
146 
147  return (new ilOrgUnitPermission($id))
148  ->withParentId($permission->getParentId())
149  ->withContextId($permission->getContextId())
150  ->withPositionId($permission->getPositionId())
151  ->withProtected($permission->isProtected())
152  ->withOperations($permission->getOperations());
153  }
154 
155  private function updateDB(ilOrgUnitPermission $permission): void
156  {
157  $where = [ 'id' => [ 'integer', $permission->getId() ] ];
158 
159  $values = [
160  'parent_id' => [ 'string', $permission->getParentId()],
161  'context_id' => [ 'string', $permission->getContextId()],
162  'position_id' => [ 'integer', $permission->getPositionId() ],
163  'protected' => [ 'integer', ($permission->isProtected()) ? 1 : 0],
164  'operations' => [ 'string', $this->convertToJson($permission->getOperations())]
165  ];
166 
167  $this->db->update(self::TABLE_NAME, $values, $where);
168  }
169 
170  public function delete(int $parent_id, int $position_id): bool
171  {
172  if ($position_id === 0) {
173  throw new ilException('$position_id cannot be 0');
174  }
175  if ($parent_id <= 0) {
176  throw new ilException('$parent_id cannot be <=0');
177  }
178 
179  $context = $this->contextRepo->getByRefId($parent_id);
180  if (!$context) {
181  throw new ilException('Context for ref_id ' . $parent_id . ' not found');
182  }
183 
184  if (!$this->isContextEnabled($context->getContext())) {
186  "Position-related permissions not active in {$context->getContext()}",
187  $context->getContext()
188  );
189  }
190 
191  $permission = $this->find($parent_id, $position_id);
192  if ($permission) {
193  $query = 'DELETE FROM ' . self::TABLE_NAME . PHP_EOL
194  . ' WHERE id = ' . $this->db->quote($permission->getId(), 'integer');
195  $this->db->manipulate($query);
196 
197  return true;
198  }
199 
200  return false;
201  }
202 
203  // TODO: Check if public use in GUI is still necessary, otherwise make this private
204  public function update(ilOrgUnitPermission $permission): ilOrgUnitPermission
205  {
206  $permission = $permission->withPossibleOperations(
207  $this->operationRepo->getOperationsByContextId($permission->getContextId())
208  );
209  $permission = $permission->withOperations(
210  is_array($permission->getOperations()) ? $permission->getOperations() : []
211  );
212  $selected_operation_ids = [];
213  foreach ($permission->getOperations() as $operation) {
214  $selected_operation_ids[] = $operation->getOperationId();
215  }
216  $permission = $permission->withSelectedOperationIds($selected_operation_ids);
217  $permission = $permission->withContext(
218  $this->contextRepo->getById($permission->getContextId())
219  );
220 
221  return $permission;
222  }
223 
224  public function getLocalorDefault(int $parent_id, int $position_id): ilOrgUnitPermission
225  {
226  if ($position_id === 0) {
227  throw new ilException('$position_id cannot be 0');
228  }
229 
230  $context = $this->contextRepo->getByRefId($parent_id);
231  if (!$context) {
232  throw new ilException('Context for ref_id ' . $parent_id . ' not found');
233  }
234 
235  if (!$this->isContextEnabled($context->getContext())) {
237  "Position-related permissions not active in {$context->getContext()}",
238  $context->getContext()
239  );
240  }
241 
242  $permission = $this->find($parent_id, $position_id);
243  if ($permission) {
244  return $permission;
245  }
246 
247  return $this->getDefaultForContext($context->getContext(), $position_id);
248  }
249 
250  public function getDefaultForContext(string $context_name, int $position_id, bool $editable = false): ilOrgUnitPermission
251  {
252  if ($position_id === 0) {
253  throw new ilException('$position_id cannot be 0');
254  }
255 
256  $context = $this->contextRepo->find($context_name);
257  if (!$context) {
258  throw new ilException('Context ' . $context_name . ' not found');
259  }
260 
261  $template = false;
262  $query = 'SELECT id, parent_id, context_id, position_id, protected, operations FROM' . PHP_EOL
263  . self::TABLE_NAME
264  . ' WHERE ' . self::TABLE_NAME . '.parent_id = ' . $this->db->quote(ilOrgUnitPermission::PARENT_TEMPLATE, 'integer') . PHP_EOL
265  . ' AND ' . self::TABLE_NAME . '.position_id = ' . $this->db->quote($position_id, 'integer') . PHP_EOL
266  . ' AND ' . self::TABLE_NAME . '.context_id = ' . $this->db->quote($context->getId(), 'integer') . PHP_EOL;
267  $res = $this->db->query($query);
268  if ($res->numRows() > 0) {
269  $rec = $this->db->fetchAssoc($res);
270  $template = (new ilOrgUnitPermission((int) $rec['id']))
271  ->withParentId((int) $rec["parent_id"])
272  ->withContextId((int) $rec['context_id'])
273  ->withPositionId((int) $rec['position_id'])
274  ->withProtected((bool) $rec['protected'])
275  ->withOperations($this->convertToArray((string) $rec["operations"]));
276  $template = $this->update($template);
277  }
278 
279  if (!$template) {
280  $template = (new ilOrgUnitPermission())
282  ->withContextId($context->getId())
283  ->withPositionId($position_id)
284  ->withProtected(true);
285  $template = $this->store($template);
286  }
287 
288  $template = $template->withProtected(!$editable);
289  $template = $this->update($template);
290 
291  return $template;
292  }
293 
294  public function getDefaultsForActiveContexts(int $position_id, bool $editable = false): array
295  {
296  $active_contexts = [];
297  foreach (ilOrgUnitGlobalSettings::getInstance()->getPositionSettings() as $ilOrgUnitObjectPositionSetting) {
298  if ($ilOrgUnitObjectPositionSetting->isActive()) {
299  $active_contexts[] = $ilOrgUnitObjectPositionSetting->getType();
300  }
301  }
302 
303  $permissions = [];
304  foreach ($active_contexts as $context) {
305  $permissions[] = $this->getDefaultForContext($context, $position_id, $editable);
306  }
307 
308  return $permissions;
309  }
310 
311  private function isContextEnabled(string $context): bool
312  {
313  $ilOrgUnitGlobalSettings = ilOrgUnitGlobalSettings::getInstance();
314  $ilOrgUnitObjectPositionSetting = $ilOrgUnitGlobalSettings->getObjectPositionSettingsByType($context);
315  if (!$ilOrgUnitObjectPositionSetting->isActive()) {
316  return false;
317  }
318 
319  return true;
320  }
321 
326  private function convertToArray(string $operations)
327  {
328  $ids = json_decode($operations);
329  $ret = [];
330  foreach ($ids as $operation_id) {
331  $ret[] = $this->operationRepo->getById((int)$operation_id);
332  }
333  return $ret;
334  }
335 
340  private function convertToJson(array $operations)
341  {
342  $ids = [];
343  foreach ($operations as $operation) {
344  $ids[] = $operation->getOperationId();
345  }
346  return json_encode($ids);
347  }
348 }
$res
Definition: ltiservices.php:69
$context
Definition: webdav.php:31
convertToArray(string $operations)
This will be replaced in a future update including a migration for existing db entries.
update(ilOrgUnitPermission $permission)
Update/refresh the additional fields of the permssion object (e.g.
store(ilOrgUnitPermission $permission)
Store permission to db Returns permission with updated fields (see update())
getLocalorDefault(int $parent_id, int $position_id)
Get an existing local permission.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
withOperations(array $operations)
withPossibleOperations(array $possible_operations)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
convertToJson(array $operations)
This will be replaced in a future update including a migration for existing db entries.
find(int $parent_id, int $position_id)
Find local permission for parent and position Does not create new local permissions, returns null if no local permission exists.
__construct(ilDBInterface $db, ilOrgUnitOperationDBRepository $operationRepo, ilOrgUnitOperationContextDBRepository $contextRepo)
getDefaultForContext(string $context_name, int $position_id, bool $editable=false)
Get the default setting for a specified context If the setting does not exist, it is created (if perm...
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
ilOrgUnitOperationContextDBRepository $contextRepo
withSelectedOperationIds(array $selected_operation_ids)
getDefaultsForActiveContexts(int $position_id, bool $editable=false)
Get an array of default settings for all active contexts If the settings don&#39;t exist yet...
withContext(ilOrgUnitOperationContext $context)