ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilRoleXmlImporter.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4include_once './Services/AccessControl/exceptions/class.ilRoleImporterException.php';
5
13{
14 protected $role_folder = 0;
15 protected $role = null;
16
17 protected $xml = '';
18
22 private $logger = null;
23
27 private $refinery;
28
32 public function __construct($a_role_folder_id = 0)
33 {
34 global $DIC;
35
36 $this->logger = $DIC->logger()->otpl();
37 $this->role_folder = $a_role_folder_id;
38 $this->refinery = $DIC['refinery'];
39 }
40
41 public function setXml($a_xml)
42 {
43 $this->xml = $a_xml;
44 }
45
46 public function getXml()
47 {
48 return $this->xml;
49 }
50
55 public function getRoleFolderId()
56 {
57 return $this->role_folder;
58 }
59
64 public function getRole()
65 {
66 return $this->role;
67 }
68
73 public function setRole(ilObject $role)
74 {
75 $this->role = $role;
76 }
77
82 public function import()
83 {
84 libxml_use_internal_errors(true);
85
86 $root = simplexml_load_string($this->getXml());
87
88 if (!$root instanceof SimpleXMLElement) {
89 throw new ilRoleImporterException($this->parseXmlErrors());
90 }
91 foreach ($root->role as $roleElement) {
92 $this->importSimpleXml($roleElement);
93 // only one role is parsed
94 break;
95 }
96 }
97
98
103 public function importSimpleXml(SimpleXMLElement $role)
104 {
105 global $DIC;
106
107 $rbacadmin = $DIC['rbacadmin'];
108 $rbacreview = $DIC['rbacreview'];
109 $lng = $DIC['lng'];
110
111 $import_id = (string) $role['id'];
112 $this->logger->info('Importing role with import_id: ' . $import_id);
113
114 if (!$this->initRole($import_id)) {
115 return 0;
116 }
117
118 $trafo = $this->refinery->in()->series([
119 $this->refinery->kindlyTo()->string(),
120 $this->refinery->string()->stripTags()
121 ]);
122
123 $this->getRole()->setTitle($trafo->transform($role->title ?? ''));
124 $this->getRole()->setDescription($trafo->transform($role->description ?? ''));
125
126 $this->logger->info('Current role import id: ' . $this->getRole()->getImportId());
127
129 $exp = explode("_", $this->getRole()->getTitle());
130
131 if (count($exp) > 0 && $exp[0] === "il") {
132 if (count($exp) > 1 && $exp[1] !== $type) {
133 throw new ilRoleImporterException(sprintf(
134 $lng->txt("rbac_cant_import_role_wrong_type"),
135 $lng->txt('obj_' . $exp[1]),
136 $lng->txt('obj_' . $type)
137 ));
138 }
139
140 $exp[3] = $this->getRoleFolderId();
141
142 $id = ilObjRole::_getIdsForTitle(implode("_", $exp));
143
144 if ($id[0]) {
145 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Overwrite role ' . implode("_", $exp));
146 $this->getRole()->setId($id[0]);
147 $this->getRole()->read();
148 }
149 }
150
151 // Create or update
152 if ($this->getRole()->getId()) {
153 $rbacadmin->deleteRolePermission($this->getRole()->getId(), $this->getRoleFolderId());
154 $this->getRole()->update();
155 } else {
156 $this->getRole()->create();
157 }
158
159
160 $this->assignToRoleFolder();
161
162 $protected = (string) $role['protected'];
163 if ($protected) {
164 $rbacadmin->setProtected(0, $this->getRole()->getId(), 'y');
165 }
166
167 // Add operations
168 $ops = $rbacreview->getOperations();
169 $operations = array();
170 foreach ($ops as $ope) {
171 $operations[$ope['operation']] = $ope['ops_id'];
172 }
173
174 foreach ($role->operations as $sxml_operations) {
175 foreach ($sxml_operations as $sxml_op) {
176 $operation = trim((string) $sxml_op);
177 if (!array_key_exists($operation, $operations)) {
178 continue;
179 }
180 $ops_group = (string) $sxml_op['group'];
181 $ops_id = (int) $operations[$operation];
182 $ops = trim((string) $sxml_op);
183
184 if ($ops_group and $ops_id) {
185 $rbacadmin->setRolePermission(
186 $this->getRole()->getId(),
187 $ops_group,
188 array($ops_id),
189 $this->getRoleFolderId() // #10161
190 );
191 } else {
192 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Cannot create operation for...');
193 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': New operation for group ' . $ops_group);
194 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': New operation ' . $ops);
195 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': New operation ' . $ops_id);
196 }
197 }
198 }
199
200 return $this->getRole()->getId();
201 }
202
208 protected function assigntoRoleFolder()
209 {
210 global $DIC;
211
212 $rbacadmin = $DIC['rbacadmin'];
213 $rbacreview = $DIC['rbacreview'];
214
215 if (!$this->getRoleFolderId()) {
216 return;
217 }
218
219 if ($rbacreview->isRoleAssignedToObject($this->getRole()->getId(), $this->getRoleFolderId())) {
220 return;
221 }
222
223 $rbacadmin->assignRoleToFolder(
224 $this->getRole()->getId(),
225 $this->getRoleFolderId(),
226 $this->getRole() instanceof ilObjRole ? 'y' : 'n'
227 );
228 }
229
230
231 protected function initRole($import_id)
232 {
233 if ($this->getRole()) {
234 return true;
235 }
236
237 $this->logger->debug('Searching already imported role by import_id: ' . $import_id);
238 $obj_id = 0;
239 if ($import_id) {
240 $obj_id = ilObject::_lookupObjIdByImportId($import_id);
241 }
242 $this->logger->debug('Found already imported obj_id: ' . $obj_id);
243
244
245 if ($obj_id) {
246 $this->role = ilObjectFactory::getInstanceByObjId($obj_id, false);
247 }
248 if (
249 (!$this->getRole() instanceof ilObjRole) &&
250 (!$this->getRole() instanceof ilObjRoleTemplate)
251 ) {
252 $this->logger->debug('Creating new role template');
253 $this->role = new ilObjRoleTemplate();
254 }
255 $this->role->setImportId((string) $import_id);
256 return true;
257 }
258
259 protected function parseXmlErrors()
260 {
261 $errors = '';
262
263 foreach (libxml_get_errors() as $err) {
264 $errors .= $err->code . '<br/>';
265 }
266 return $errors;
267 }
268}
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
An exception for terminatinating execution or to throw for unit testing.
Class ilObjRoleTemplate.
Class ilObjRole.
static getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
Class ilObject Basic functions for all objects.
static _lookupObjIdByImportId($a_import_id)
static _lookupType($a_id, $a_reference=false)
lookup object type
static _getIdsForTitle($title, $type='', $partialmatch=false)
Description of class.
__construct($a_role_folder_id=0)
Constructor.
getRoleFolderId()
Get role folder id.
assigntoRoleFolder()
Assign role to folder @global type $rbacadmin.
setRole(ilObject $role)
Set role or role template.
importSimpleXml(SimpleXMLElement $role)
Import using simplexml.
global $DIC
Definition: goto.php:24
$errors
Definition: imgupload.php:49
$type
$lng