ILIAS  trunk Revision v11.0_alpha-1713-gd8962da2f67
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilRoleXmlImporter.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
29 {
30  protected int $role_folder = 0;
31  protected ?ilObject $role = null;
32 
33  protected string $xml = '';
34 
35  private ilLogger $logger;
40 
41  public function __construct(int $a_role_folder_id = 0)
42  {
43  global $DIC;
44 
45  $this->logger = $DIC->logger()->ac();
46  $this->rbacreview = $DIC->rbac()->review();
47  $this->rbacadmin = $DIC->rbac()->admin();
48  $this->language = $DIC->language();
49  $this->refinery = $DIC['refinery'];
50 
51  $this->role_folder = $a_role_folder_id;
52  }
53 
54  public function setXml(string $a_xml): void
55  {
56  $this->xml = $a_xml;
57  }
58 
59  public function getXml(): string
60  {
61  return $this->xml;
62  }
63 
64  public function getRoleFolderId(): int
65  {
66  return $this->role_folder;
67  }
68 
69  public function getRole(): ?ilObject
70  {
71  return $this->role;
72  }
73 
74  public function setRole(ilObject $role): void
75  {
76  $this->role = $role;
77  }
78 
83  public function import(): void
84  {
85  $use_internal_errors = libxml_use_internal_errors(true);
86  $root = simplexml_load_string($this->getXml());
87  libxml_use_internal_errors($use_internal_errors);
88 
89  if (!$root instanceof SimpleXMLElement) {
90  throw new ilRoleImporterException($this->parseXmlErrors());
91  }
92  foreach ($root->role as $roleElement) {
93  $this->importSimpleXml($roleElement);
94  // only one role is parsed
95  break;
96  }
97  }
98 
99  public function importSimpleXml(SimpleXMLElement $role): int
100  {
101  $import_id = (string) $role['id'];
102  $this->logger->info('Importing role with import_id: ' . $import_id);
103  $this->initRole($import_id);
104 
105  $trafo = $this->refinery->in()->series([
106  $this->refinery->kindlyTo()->string(),
107  $this->refinery->string()->stripTags()
108  ]);
109 
110  $this->getRole()->setTitle($trafo->transform($role->title ?? ''));
111  $this->getRole()->setDescription($trafo->transform($role->description ?? ''));
112 
113  $this->logger->info('Current role import id: ' . $this->getRole()->getImportId());
114 
115  $type = ilObject::_lookupType($this->getRoleFolderId(), true);
116  $exp = explode("_", $this->getRole()->getTitle());
117 
118  if (count($exp) > 0 && $exp[0] === "il") {
119  if (count($exp) > 1 && $exp[1] !== $type) {
120  throw new ilRoleImporterException(sprintf(
121  $this->language->txt("rbac_cant_import_role_wrong_type"),
122  $this->language->txt('obj_' . $exp[1]),
123  $this->language->txt('obj_' . $type)
124  ));
125  }
126 
127  $exp[3] = $this->getRoleFolderId();
128 
129  $id = ilObjRole::_getIdsForTitle(implode("_", $exp));
130 
131  if ($id[0]) {
132  $this->getRole()->setId($id[0]);
133  $this->getRole()->read();
134  }
135  }
136 
137  // Create or update
138  if ($this->getRole()->getId()) {
139  $this->rbacadmin->deleteRolePermission($this->getRole()->getId(), $this->getRoleFolderId());
140  $this->getRole()->update();
141  } else {
142  $this->getRole()->create();
143  }
144 
145  $this->assignToRoleFolder();
146 
147  $protected = (string) $role['protected'];
148  if ($protected) {
149  $this->rbacadmin->setProtected(0, $this->getRole()->getId(), 'y');
150  }
151 
152  // Add operations
153  $ops = $this->rbacreview->getOperations();
154  $operations = [];
155  foreach ($ops as $ope) {
156  $operations[$ope['operation']] = $ope['ops_id'];
157  }
158 
159  foreach ($role->operations as $sxml_operations) {
160  foreach ($sxml_operations as $sxml_op) {
161  $operation = trim((string) $sxml_op);
162  if (!array_key_exists($operation, $operations)) {
163  continue;
164  }
165  $ops_group = (string) $sxml_op['group'];
166  $ops_id = (int) $operations[$operation];
167  $ops = $operation;
168 
169  if ($ops_group && $ops_id) {
170  $this->rbacadmin->setRolePermission(
171  $this->getRole()->getId(),
172  $ops_group,
173  [$ops_id],
174  $this->getRoleFolderId() // #10161
175  );
176  }
177  }
178  }
179  return $this->getRole()->getId();
180  }
181 
182  protected function assignToRoleFolder(): void
183  {
184  if (!$this->getRoleFolderId()) {
185  return;
186  }
187 
188  if ($this->rbacreview->isRoleAssignedToObject($this->getRole()->getId(), $this->getRoleFolderId())) {
189  return;
190  }
191 
192  $this->rbacadmin->assignRoleToFolder(
193  $this->getRole()->getId(),
194  $this->getRoleFolderId(),
195  $this->getRole() instanceof ilObjRole ? 'y' : 'n'
196  );
197  }
198 
199  protected function initRole(string $import_id): void
200  {
201  if ($this->getRole()) {
202  return;
203  }
204 
205  $this->logger->debug('Searching already imported role by import_id: ' . $import_id);
206  $obj_id = 0;
207  if ($import_id) {
208  $obj_id = ilObject::_lookupObjIdByImportId($import_id);
209  }
210  $this->logger->debug('Found already imported obj_id: ' . $obj_id);
211 
212  if ($obj_id) {
213  $this->role = ilObjectFactory::getInstanceByObjId($obj_id, false);
214  }
215  if (
216  (!$this->getRole() instanceof ilObjRole) &&
217  (!$this->getRole() instanceof ilObjRoleTemplate)
218  ) {
219  $this->logger->debug('Creating new role template');
220  $this->role = new ilObjRoleTemplate();
221  }
222  $this->role->setImportId($import_id);
223  }
224 
225  protected function parseXmlErrors(): string
226  {
227  $errors = '';
228 
229  foreach (libxml_get_errors() as $err) {
230  $errors .= $err->code . '<br/>';
231  }
232  return $errors;
233  }
234 }
importSimpleXml(SimpleXMLElement $role)
Class ilObjRole.
static _lookupObjIdByImportId(string $import_id)
Get (latest) object id for an import id.
Class ilObjRoleTemplate.
__construct(int $a_role_folder_id=0)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
global $DIC
Definition: shib_login.php:22
static _getIdsForTitle(string $title, string $type='', bool $partial_match=false)
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
initRole(string $import_id)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
language()
description: > Example for rendring a language glyph.
Definition: language.php:41
Class ilRbacAdmin Core functions for role based access control.
static _lookupType(int $id, bool $reference=false)
Description of class.