ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 
4 include_once './Services/AccessControl/exceptions/class.ilRoleImporterException.php';
5 
13 {
14  protected $role_folder = 0;
15  protected $role = null;
16 
17  protected $xml = '';
18 
22  public function __construct($a_role_folder_id = 0)
23  {
24  $this->role_folder = $a_role_folder_id;
25  }
26 
27  public function setXml($a_xml)
28  {
29  $this->xml = $a_xml;
30  }
31 
32  public function getXml()
33  {
34  return $this->xml;
35  }
36 
41  public function getRoleFolderId()
42  {
43  return $this->role_folder;
44  }
45 
50  public function getRole()
51  {
52  return $this->role;
53  }
54 
59  public function setRole(ilObject $role)
60  {
61  $this->role = $role;
62  }
63 
68  public function import()
69  {
70  libxml_use_internal_errors(true);
71 
72  $root = simplexml_load_string($this->getXml());
73 
74  if (!$root instanceof SimpleXMLElement) {
75  throw new ilRoleImporterException($this->parseXmlErrors());
76  }
77  foreach ($root->role as $roleElement) {
78  $this->importSimpleXml($roleElement);
79  // only one role is parsed
80  break;
81  }
82  }
83 
84 
90  {
91  global $rbacadmin, $rbacreview, $lng;
92 
93  $import_id = (string) $role['id'];
94  $GLOBALS['ilLog']->write(__METHOD__ . ' Importing role with import id ' . $import_id);
95 
96  if (!$this->initRole($import_id)) {
97  return 0;
98  }
99 
100  $this->getRole()->setTitle(trim((string) $role->title));
101  $this->getRole()->setDescription(trim((string) $role->description));
102 
103  $type = ilObject::_lookupType($this->getRoleFolderId(), true);
104  $exp = explode("_", $this->getRole()->getTitle());
105 
106  if (count($exp) > 0 && $exp[0] === "il") {
107  if (count($exp) > 1 && $exp[1] !== $type) {
108  throw new ilRoleImporterException(sprintf(
109  $lng->txt("rbac_cant_import_role_wrong_type"),
110  $lng->txt('obj_' . $exp[1]),
111  $lng->txt('obj_' . $type)
112  ));
113  }
114 
115  $exp[3] = $this->getRoleFolderId();
116 
117  $id = ilObjRole::_getIdsForTitle(implode("_", $exp));
118 
119  if ($id[0]) {
120  $GLOBALS['ilLog']->write(__METHOD__ . ': Overwrite role ' . implode("_", $exp));
121  $this->getRole()->setId($id[0]);
122  $this->getRole()->read();
123  }
124  }
125 
126  // Create or update
127  if ($this->getRole()->getId()) {
128  $rbacadmin->deleteRolePermission($this->getRole()->getId(), $this->getRoleFolderId());
129  $this->getRole()->update();
130  } else {
131  $this->getRole()->create();
132  }
133 
134 
135  $this->assignToRoleFolder();
136 
137  $protected = (string) $role['protected'];
138  if ($protected) {
139  $rbacadmin->setProtected(0, $this->getRole()->getId(), 'y');
140  }
141 
142  // Add operations
143  $ops = $rbacreview->getOperations();
144  $operations = array();
145  foreach ($ops as $ope) {
146  $operations[$ope['operation']] = $ope['ops_id'];
147  }
148 
149  foreach ($role->operations as $sxml_operations) {
150  foreach ($sxml_operations as $sxml_op) {
151  $ops_group = (string) $sxml_op['group'];
152  $ops_id = (int) $operations[trim((string) $sxml_op)];
153  $ops = trim((string) $sxml_op);
154 
155  if ($ops_group and $ops_id) {
156  $rbacadmin->setRolePermission(
157  $this->getRole()->getId(),
158  $ops_group,
159  array($ops_id),
160  $this->getRoleFolderId() // #10161
161  );
162  } else {
163  $GLOBALS['ilLog']->write(__METHOD__ . ': Cannot create operation for...');
164  $GLOBALS['ilLog']->write(__METHOD__ . ': New operation for group ' . $ops_group);
165  $GLOBALS['ilLog']->write(__METHOD__ . ': New operation ' . $ops);
166  $GLOBALS['ilLog']->write(__METHOD__ . ': New operation ' . $ops_id);
167  }
168  }
169  }
170 
171  return $this->getRole()->getId();
172  }
173 
179  protected function assigntoRoleFolder()
180  {
181  global $rbacadmin, $rbacreview;
182 
183  if (!$this->getRoleFolderId()) {
184  return;
185  }
186 
187  if ($rbacreview->isRoleAssignedToObject($this->getRole()->getId(), $this->getRoleFolderId())) {
188  return;
189  }
190 
191  $rbacadmin->assignRoleToFolder(
192  $this->getRole()->getId(),
193  $this->getRoleFolderId(),
194  $this->getRole() instanceof ilObjRole ? 'y' : 'n'
195  );
196  }
197 
198 
199  protected function initRole($import_id)
200  {
201  if ($this->getRole()) {
202  return true;
203  }
204 
205  $obj_id = ilObject::_lookupObjIdByImportId($import_id);
206  include_once './Services/Object/classes/class.ilObjectFactory.php';
207  if ($obj_id) {
208  $this->role = ilObjectFactory::getInstanceByObjId($obj_id, false);
209  }
210  if (!$this->getRole() instanceof ilObjRole or !$this->getRole() instanceof ilObjRoleTemplate) {
211  include_once './Services/AccessControl/classes/class.ilObjRoleTemplate.php';
212  $this->role = new ilObjRoleTemplate();
213  }
214  return true;
215  }
216 
217  protected function parseXmlErrors()
218  {
219  $errors = '';
220 
221  foreach (libxml_get_errors() as $err) {
222  $errors .= $err->code . '<br/>';
223  }
224  return $errors;
225  }
226 }
importSimpleXml(SimpleXMLElement $role)
Import using simplexml.
Class ilObjRole.
Add rich text string
Class ilObjRoleTemplate.
$type
setRole(ilObject $role)
Set role or role template.
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
if(!array_key_exists('StateId', $_REQUEST)) $id
static getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
Create styles array
The data for the language used.
static _lookupType($a_id, $a_reference=false)
lookup object type
getRoleFolderId()
Get role folder id.
$errors
Definition: index.php:6
global $lng
Definition: privfeed.php:17
static _getIdsForTitle($title, $type='', $partialmatch=false)
__construct($a_role_folder_id=0)
Constructor.
Description of class.
static _lookupObjIdByImportId($a_import_id)
assigntoRoleFolder()
Assign role to folder type $rbacadmin.