ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
CopyPermissions.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
29
34readonly class CopyPermissions implements Objective
35{
39 public function __construct(private string $src_type, private string $dest_type, private array $additional_preconditions)
40 {
41 }
42
43 public function getHash(): string
44 {
45 return hash("sha256", self::class . $this->src_type . ',' . $this->dest_type);
46 }
47
48 public function getLabel(): string
49 {
50 return sprintf('Copy permissions from %s to %s', $this->src_type, $this->dest_type);
51 }
52
53 public function isNotable(): bool
54 {
55 return true;
56 }
57
58 public function getPreconditions(Environment $environment): array
59 {
60 return [new ilDatabaseInitializedObjective(), ...$this->additional_preconditions];
61 }
62
63 public function achieve(Environment $environment): Environment
64 {
66 $src_ref_id = $this->firstRefIdOfType($db, $this->src_type);
67 $dest_ref_id = $this->firstRefIdOfType($db, $this->dest_type);
68
69 if (!$src_ref_id || !$dest_ref_id) {
70 return $environment;
71 }
72
73 $db->queryF(
74 'DELETE FROM rbac_pa WHERE ref_id = %s',
76 [$dest_ref_id]
77 );
78 $db->manipulateF(
79 'INSERT INTO rbac_pa (ref_id, ops_id, rol_id) SELECT %s AS ref_id, ops_id, rol_id FROM rbac_pa WHERE ref_id = %s',
81 [$dest_ref_id, $src_ref_id]
82 );
83
84 return $environment;
85 }
86
87 public function isApplicable(Environment $environment): bool
88 {
90 $src_ref = $this->firstRefIdOfType($db, $this->src_type);
91 $ret = null !== $db->fetchAssoc($db->queryF('select 1 from rbac_pa where ref_id = %s', [ilDBConstants::T_INTEGER], [$src_ref]));
92
93 return $ret;
94 }
95
96 private function firstRefIdOfType(ilDBInterface $db, string $type): int
97 {
98 $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';
99 return (int) ($db->fetchAssoc($db->queryF($ref_query, [ilDBConstants::T_TEXT], [$type]))['ref_id'] ?? 0);
100 }
101}
ALL permissions are copied from the $src_type and all existing permissions are first deleted from $de...
achieve(Environment $environment)
Objectives can be achieved.
isApplicable(Environment $environment)
Get to know whether the objective is applicable.
firstRefIdOfType(ilDBInterface $db, string $type)
getLabel()
Get a label that describes this objective.
__construct(private string $src_type, private string $dest_type, private array $additional_preconditions)
isNotable()
Get to know if this is an interesting objective for a human.
getPreconditions(Environment $environment)
Objectives might depend on other objectives.
Class ilDBConstants.
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.