ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilTreeAdminNodeAddedObjective.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 use ILIAS\Setup;
24 
25 class ilTreeAdminNodeAddedObjective implements Setup\Objective
26 {
27  protected const RBAC_OP_EDIT_PERMISSIONS = 1;
28  protected const RBAC_OP_VISIBLE = 2;
29  protected const RBAC_OP_READ = 3;
30  protected const RBAC_OP_WRITE = 4;
31 
32  protected array $rbac_ops = [
33  self::RBAC_OP_EDIT_PERMISSIONS,
34  self::RBAC_OP_VISIBLE,
35  self::RBAC_OP_READ,
36  self::RBAC_OP_WRITE
37  ];
38 
39  protected string $type;
40  protected string $title;
41  protected string $parent_type;
42 
43  public function __construct(string $type, string $title, string $parent_type = "")
44  {
45  $this->type = $type;
46  $this->title = $title;
47  $this->parent_type = $parent_type;
48  }
49 
50  public function getHash(): string
51  {
52  return hash("sha256", self::class . "::" . $this->type);
53  }
54 
55  public function getLabel(): string
56  {
57  return "Add new admin node to tree (type=$this->type;title=$this->title)";
58  }
59 
60  public function isNotable(): bool
61  {
62  return true;
63  }
64 
65  public function getPreconditions(Environment $environment): array
66  {
67  return [
70  ];
71  }
72 
73  public function achieve(Environment $environment): Environment
74  {
75  $client_ini = $environment->getResource(Setup\Environment::RESOURCE_CLIENT_INI);
76  $db = $environment->getResource(Environment::RESOURCE_DATABASE);
77 
78  // ATTENTION: This is a total abomination. It only exists to allow various
79  // sub components of the various readers to run. This is a memento to the
80  // fact, that dependency injection is something we want. Currently, every
81  // component could just service locate the whole world via the global $DIC.
82  $PREVIOUS_DIC = $GLOBALS["DIC"] ?? null;
83  $GLOBALS["DIC"] = new Container();
84  $GLOBALS["DIC"]["ilDB"] = $db;
85 
86  try {
87  if (!defined("ROOT_FOLDER_ID")) {
88  define("ROOT_FOLDER_ID", (int) $client_ini->readVariable("system", "ROOT_FOLDER_ID"));
89  }
90  if (!defined("SYSTEM_FOLDER_ID")) {
91  define("SYSTEM_FOLDER_ID", $client_ini->readVariable("system", "SYSTEM_FOLDER_ID"));
92  }
93  if (!defined("ILIAS_LOG_ENABLED")) {
94  define("ILIAS_LOG_ENABLED", false);
95  }
96 
97  $obj_type_id = $db->nextId("object_data");
98  $values = [
99  'obj_id' => ['integer', $obj_type_id],
100  'type' => ['text', 'typ'],
101  'title' => ['text', $this->type],
102  'description' => ['text', $this->title],
103  'owner' => ['integer', -1],
104  'create_date' => ['timestamp', date("Y-m-d H:i:s")],
105  'last_update' => ['timestamp', date("Y-m-d H:i:s")]
106  ];
107  $db->insert("object_data", $values);
108 
109  $obj_id = $db->nextId("object_data");
110  $values = [
111  'obj_id' => ['integer', $obj_id],
112  'type' => ['text', $this->type],
113  'title' => ['text', $this->title],
114  'description' => ['text', $this->title],
115  'owner' => ['integer', -1],
116  'create_date' => ['timestamp', date("Y-m-d H:i:s")],
117  'last_update' => ['timestamp', date("Y-m-d H:i:s")]
118  ];
119  $db->insert("object_data", $values);
120 
121  $ref_id = $db->nextId("object_reference");
122  $values = [
123  "obj_id" => ["integer", $obj_id],
124  "ref_id" => ["integer", $ref_id]
125  ];
126  $db->insert("object_reference", $values);
127 
128  $tree = new ilTree(
130  0,
131  $db
132  );
133  if ($this->parent_type) {
134  $set = $db->queryF(
135  "SELECT * FROM object_data " .
136  " WHERE type = %s ",
137  ["text"],
138  [$this->parent_type]
139  );
140  $rec = $db->fetchAssoc($set);
141 
142  $set = $db->queryF(
143  "SELECT * FROM object_reference " .
144  " WHERE obj_id = %s ",
145  ["integer"],
146  [$rec["obj_id"]]
147  );
148  $rec = $db->fetchAssoc($set);
149  $parent_type_ref_id = $rec["ref_id"];
150 
151  $tree->insertNode((int) $ref_id, (int) $parent_type_ref_id);
152  } else {
153  $tree->insertNode((int) $ref_id, (int) SYSTEM_FOLDER_ID);
154  }
155 
156  foreach ($this->rbac_ops as $ops_id) {
157  if (ilRbacReview::_isRBACOperation($obj_type_id, $ops_id, $db)) {
158  continue;
159  }
160  $values = [
161  "typ_id" => ["integer", $obj_type_id],
162  "ops_id" => ["integer", $ops_id]
163  ];
164  $db->insert("rbac_ta", $values);
165  }
166  } finally {
167  // reset the abomination again so we don't make objectives flaky.
168  $GLOBALS["DIC"] = $PREVIOUS_DIC;
169  }
170 
171  return $environment;
172  }
173 
174  public function isApplicable(Environment $environment): bool
175  {
176  $db = $environment->getResource(Environment::RESOURCE_DATABASE);
177  return !((bool) ilObject::_getObjectTypeIdByTitle($this->type, $db));
178  }
179 }
const ROOT_FOLDER_ID
Definition: constants.php:32
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
const SYSTEM_FOLDER_ID
Definition: constants.php:35
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
$ref_id
Definition: ltiauth.php:65
$GLOBALS["DIC"]
Definition: wac.php:53
static _isRBACOperation(int $type_id, int $ops_id, ?\ilDBInterface $ilDB=null)
getResource(string $id)
Consumers of this method should check if the result is what they expect, e.g.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
An environment holds resources to be used in the setup process.
Definition: Environment.php:27
static _getObjectTypeIdByTitle(string $type, ?\ilDBInterface $ilDB=null)
__construct(string $type, string $title, string $parent_type="")