ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
DropPermissions.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
26use ilIniFile;
31
32readonly class DropPermissions implements Objective
33{
38 public function __construct(
39 private string $type,
40 private array $operations,
41 private array $additional_preconditions = []
42 ) {
43 }
44
45 public function getHash(): string
46 {
47 return hash("sha256", self::class . $this->type . ',' . join(',', $this->operations));
48 }
49
50 public function getLabel(): string
51 {
52 return 'Drop permissions ' . join(', ', $this->operations) . ' from ' . $this->type;
53 }
54
55 public function isNotable(): bool
56 {
57 return true;
58 }
59
60 public function getPreconditions(Environment $environment): array
61 {
62 return [
64 ...$this->additional_preconditions,
65 ];
66 }
67
68 public function achieve(Environment $environment): Environment
69 {
71 $operations = $this->operations($environment);
72 $ref_id = $this->firstRefIdOfType($db, $this->type);
73 if (!$ref_id) {
74 return $environment;
75 }
76
77 foreach ($operations as $operation) {
78 $objective = new ilAccessRBACOperationDeletedObjective($this->type, $operation);
79 $objective->achieve($environment);
80 }
81
82 // rbac_pa is not cleanup by ilAccessRBACOperationDeletedObjective::class.
83 $update_row = $db->prepare('UPDATE rbac_pa SET ops_id = ? WHERE rol_id = ? AND ref_id = ?', [ilDBConstants::T_TEXT, ilDBConstants::T_INTEGER]);
84 $delete_row = $db->prepare('DELETE FROM rbac_pa WHERE rol_id = ? AND ref_id = ?', [ilDBConstants::T_INTEGER, ilDBConstants::T_INTEGER]);
85 foreach ($db->fetchAll($db->queryF('SELECT * FROM rbac_pa WHERE ref_id = %s', [ilDBConstants::T_INTEGER], [$ref_id])) as $row) {
86 $ops_id = unserialize($row['ops_id'], ['allowed_classes' => false]);
87 $ops_id = array_values(array_filter($ops_id, fn($op) => !in_array($op, $operations, false)));
88 if ($ops_id === []) {
89 $db->execute($delete_row, [$row['rol_id'], $ref_id]);
90 } else {
91 $db->execute($update_row, [serialize($ops_id), $row['rol_id'], $ref_id]);
92 }
93 }
94
95 return $environment;
96 }
97
98 public function isApplicable(Environment $environment): bool
99 {
100 return $this->operations($environment) !== [];
101 }
102
106 private function operations(Environment $environment): array
107 {
109
110 $s = $db->query(
111 'SELECT ops_id FROM rbac_ta WHERE ' .
112 $db->in('ops_id', $this->operations, false, ilDBConstants::T_INTEGER) .
113 ' AND typ_id IN (SELECT obj_id FROM object_data WHERE title = ' .
114 $db->quote($this->type, ilDBConstants::T_TEXT) .
115 ' AND type = "typ")'
116 );
117
118 return array_map('intval', array_column($db->fetchAll($s), 'ops_id'));
119 }
120
121 private function firstRefIdOfType(ilDBInterface $db, string $type): int
122 {
123 $ref_query = 'SELECT ref_id FROM object_reference AS r INNER JOIN object_data AS o ON r.obj_id = o.obj_id where type = %s';
124 return (int) ($db->fetchAssoc($db->queryF($ref_query, [ilDBConstants::T_TEXT], [$type]))['ref_id'] ?? 0);
125 }
126}
isApplicable(Environment $environment)
Get to know whether the objective is applicable.
achieve(Environment $environment)
Objectives can be achieved.
getLabel()
Get a label that describes this objective.
isNotable()
Get to know if this is an interesting objective for a human.
getPreconditions(Environment $environment)
Objectives might depend on other objectives.
__construct(private string $type, private array $operations, private array $additional_preconditions=[])
Class ilDBConstants.
INIFile Parser Early access in init proceess! Avoid further dependencies like logging or other servic...
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
Interface ilDBInterface.
$ref_id
Definition: ltiauth.php:66