ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 $DIC;
92 
93  $rbacadmin = $DIC['rbacadmin'];
94  $rbacreview = $DIC['rbacreview'];
95  $lng = $DIC['lng'];
96 
97  $import_id = (string) $role['id'];
98  $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ' Importing role with import id ' . $import_id);
99 
100  if (!$this->initRole($import_id)) {
101  return 0;
102  }
103 
104  $this->getRole()->setTitle(trim((string) $role->title));
105  $this->getRole()->setDescription(trim((string) $role->description));
106 
107  $type = ilObject::_lookupType($this->getRoleFolderId(), true);
108  $exp = explode("_", $this->getRole()->getTitle());
109 
110  if (count($exp) > 0 && $exp[0] === "il") {
111  if (count($exp) > 1 && $exp[1] !== $type) {
112  throw new ilRoleImporterException(sprintf(
113  $lng->txt("rbac_cant_import_role_wrong_type"),
114  $lng->txt('obj_' . $exp[1]),
115  $lng->txt('obj_' . $type)
116  ));
117  }
118 
119  $exp[3] = $this->getRoleFolderId();
120 
121  $id = ilObjRole::_getIdsForTitle(implode("_", $exp));
122 
123  if ($id[0]) {
124  $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Overwrite role ' . implode("_", $exp));
125  $this->getRole()->setId($id[0]);
126  $this->getRole()->read();
127  }
128  }
129 
130  // Create or update
131  if ($this->getRole()->getId()) {
132  $rbacadmin->deleteRolePermission($this->getRole()->getId(), $this->getRoleFolderId());
133  $this->getRole()->update();
134  } else {
135  $this->getRole()->create();
136  }
137 
138 
139  $this->assignToRoleFolder();
140 
141  $protected = (string) $role['protected'];
142  if ($protected) {
143  $rbacadmin->setProtected(0, $this->getRole()->getId(), 'y');
144  }
145 
146  // Add operations
147  $ops = $rbacreview->getOperations();
148  $operations = array();
149  foreach ($ops as $ope) {
150  $operations[$ope['operation']] = $ope['ops_id'];
151  }
152 
153  foreach ($role->operations as $sxml_operations) {
154  foreach ($sxml_operations as $sxml_op) {
155  $ops_group = (string) $sxml_op['group'];
156  $ops_id = (int) $operations[trim((string) $sxml_op)];
157  $ops = trim((string) $sxml_op);
158 
159  if ($ops_group and $ops_id) {
160  $rbacadmin->setRolePermission(
161  $this->getRole()->getId(),
162  $ops_group,
163  array($ops_id),
164  $this->getRoleFolderId() // #10161
165  );
166  } else {
167  $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Cannot create operation for...');
168  $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': New operation for group ' . $ops_group);
169  $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': New operation ' . $ops);
170  $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': New operation ' . $ops_id);
171  }
172  }
173  }
174 
175  return $this->getRole()->getId();
176  }
177 
183  protected function assigntoRoleFolder()
184  {
185  global $DIC;
186 
187  $rbacadmin = $DIC['rbacadmin'];
188  $rbacreview = $DIC['rbacreview'];
189 
190  if (!$this->getRoleFolderId()) {
191  return;
192  }
193 
194  if ($rbacreview->isRoleAssignedToObject($this->getRole()->getId(), $this->getRoleFolderId())) {
195  return;
196  }
197 
198  $rbacadmin->assignRoleToFolder(
199  $this->getRole()->getId(),
200  $this->getRoleFolderId(),
201  $this->getRole() instanceof ilObjRole ? 'y' : 'n'
202  );
203  }
204 
205 
206  protected function initRole($import_id)
207  {
208  if ($this->getRole()) {
209  return true;
210  }
211 
212  $obj_id = ilObject::_lookupObjIdByImportId($import_id);
213  include_once './Services/Object/classes/class.ilObjectFactory.php';
214  if ($obj_id) {
215  $this->role = ilObjectFactory::getInstanceByObjId($obj_id, false);
216  }
217  if (!$this->getRole() instanceof ilObjRole or !$this->getRole() instanceof ilObjRoleTemplate) {
218  include_once './Services/AccessControl/classes/class.ilObjRoleTemplate.php';
219  $this->role = new ilObjRoleTemplate();
220  }
221  return true;
222  }
223 
224  protected function parseXmlErrors()
225  {
226  $errors = '';
227 
228  foreach (libxml_get_errors() as $err) {
229  $errors .= $err->code . '<br/>';
230  }
231  return $errors;
232  }
233 }
importSimpleXml(SimpleXMLElement $role)
Import using simplexml.
Class ilObjRole.
Class ilObjRoleTemplate.
$type
global $DIC
Definition: saml.php:7
setRole(ilObject $role)
Set role or role template.
if(!array_key_exists('StateId', $_REQUEST)) $id
$lng
$root
Definition: sabredav.php:45
static getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
static _lookupType($a_id, $a_reference=false)
lookup object type
getRoleFolderId()
Get role folder id.
$errors
Definition: index.php:6
static _getIdsForTitle($title, $type='', $partialmatch=false)
__construct($a_role_folder_id=0)
Constructor.
Description of class.
$GLOBALS['JPEG_Segment_Names']
Global Variable: XMP_tag_captions.
static _lookupObjIdByImportId($a_import_id)
assigntoRoleFolder()
Assign role to folder type $rbacadmin.