ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilAccessRBACOperationClonedObjective.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use ILIAS\Setup;
23
28{
29 protected string $type;
30 protected int $src_id;
31 protected int $dest_id;
32
33 public function __construct(string $type, int $src_id, int $dest_id)
34 {
35 $this->type = $type;
36 $this->src_id = $src_id;
37 $this->dest_id = $dest_id;
38 }
39
40 public function getHash(): string
41 {
42 return hash("sha256", self::class);
43 }
44
45 public function getLabel(): string
46 {
47 return "Clone rbac operation from $this->src_id to $this->dest_id";
48 }
49
50 public function isNotable(): bool
51 {
52 return true;
53 }
54
55 public function getPreconditions(Environment $environment): array
56 {
57 return [
59 ];
60 }
61
62 public function achieve(Environment $environment): Environment
63 {
64 $db = $environment->getResource(Environment::RESOURCE_DATABASE);
65
66 $sql =
67 "SELECT rpa.rol_id, rpa.ops_id, rpa.ref_id" . PHP_EOL
68 . "FROM rbac_pa rpa" . PHP_EOL
69 . "JOIN object_reference ref ON (ref.ref_id = rpa.ref_id)" . PHP_EOL
70 . "JOIN object_data od ON (od.obj_id = ref.obj_id AND od.type = " . $db->quote($this->type, "text") . ")" . PHP_EOL
71 . "WHERE (" . $db->like("ops_id", "text", "%i:" . $this->src_id . "%") . PHP_EOL
72 . "OR " . $db->like("ops_id", "text", "%:\"" . $this->src_id . "\";%") . ")" . PHP_EOL
73 ;
74
75 $result = $db->query($sql);
76
77 while ($row = $db->fetchAssoc($result)) {
78 $ops = unserialize($row["ops_id"]);
79 // the query above could match by array KEY, we need extra checks
80 if (in_array($this->src_id, $ops) && !in_array($this->dest_id, $ops)) {
81 $ops[] = $this->dest_id;
82
83 $sql =
84 "UPDATE rbac_pa" . PHP_EOL
85 . "SET ops_id = " . $db->quote(serialize($ops), "text") . PHP_EOL
86 . "WHERE rol_id = " . $db->quote($row["rol_id"], "integer") . PHP_EOL
87 . "AND ref_id = " . $db->quote($row["ref_id"], "integer") . PHP_EOL
88 ;
89
90 $db->manipulate($sql);
91 }
92 }
93
94 // rbac_templates
95 $tmp = [];
96 $sql =
97 "SELECT rol_id, parent, ops_id" . PHP_EOL
98 . "FROM rbac_templates" . PHP_EOL
99 . "WHERE type = " . $db->quote($this->type, "text") . PHP_EOL
100 . "AND (ops_id = " . $db->quote($this->src_id, "integer") . PHP_EOL
101 . "OR ops_id = " . $db->quote($this->dest_id, "integer") . ")" . PHP_EOL
102 ;
103
104 $result = $db->query($sql);
105
106 while ($row = $db->fetchAssoc($result)) {
107 $tmp[$row["rol_id"]][$row["parent"]][] = $row["ops_id"];
108 }
109
110 foreach ($tmp as $role_id => $parents) {
111 foreach ($parents as $parent_id => $ops_ids) {
112 // only if the target op is missing
113 if (sizeof($ops_ids) < 2 && in_array($this->src_id, $ops_ids)) {
114 $values = [
115 "rol_id" => ["integer", $role_id],
116 "type" => ["text", $this->type],
117 "ops_id" => ["integer", $this->dest_id],
118 "parent" => ["integer", $parent_id]
119 ];
120
121 $db->insert("rbac_templates", $values);
122 }
123 }
124 }
125
126 return $environment;
127 }
128
129 public function isApplicable(Environment $environment): bool
130 {
131 return true;
132 }
133}
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
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...