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