ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
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  private $logger = null;
23 
27  public function __construct($a_role_folder_id = 0)
28  {
29  global $DIC;
30 
31  $this->logger = $DIC->logger()->otpl();
32  $this->role_folder = $a_role_folder_id;
33  }
34 
35  public function setXml($a_xml)
36  {
37  $this->xml = $a_xml;
38  }
39 
40  public function getXml()
41  {
42  return $this->xml;
43  }
44 
49  public function getRoleFolderId()
50  {
51  return $this->role_folder;
52  }
53 
58  public function getRole()
59  {
60  return $this->role;
61  }
62 
67  public function setRole(ilObject $role)
68  {
69  $this->role = $role;
70  }
71 
76  public function import()
77  {
78  libxml_use_internal_errors(true);
79 
80  $root = simplexml_load_string($this->getXml());
81 
82  if (!$root instanceof SimpleXMLElement) {
83  throw new ilRoleImporterException($this->parseXmlErrors());
84  }
85  foreach ($root->role as $roleElement) {
86  $this->importSimpleXml($roleElement);
87  // only one role is parsed
88  break;
89  }
90  }
91 
92 
97  public function importSimpleXml(SimpleXMLElement $role)
98  {
99  global $DIC;
100 
101  $rbacadmin = $DIC['rbacadmin'];
102  $rbacreview = $DIC['rbacreview'];
103  $lng = $DIC['lng'];
104 
105  $import_id = (string) $role['id'];
106  $this->logger->info('Importing role with import_id: ' . $import_id);
107 
108  if (!$this->initRole($import_id)) {
109  return 0;
110  }
111 
112  $this->getRole()->setTitle(trim((string) $role->title));
113  $this->getRole()->setDescription(trim((string) $role->description));
114 
115  $this->logger->info('Current role import id: ' . $this->getRole()->getImportId());
116 
117  $type = ilObject::_lookupType($this->getRoleFolderId(), true);
118  $exp = explode("_", $this->getRole()->getTitle());
119 
120  if (count($exp) > 0 && $exp[0] === "il") {
121  if (count($exp) > 1 && $exp[1] !== $type) {
122  throw new ilRoleImporterException(sprintf(
123  $lng->txt("rbac_cant_import_role_wrong_type"),
124  $lng->txt('obj_' . $exp[1]),
125  $lng->txt('obj_' . $type)
126  ));
127  }
128 
129  $exp[3] = $this->getRoleFolderId();
130 
131  $id = ilObjRole::_getIdsForTitle(implode("_", $exp));
132 
133  if ($id[0]) {
134  $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Overwrite role ' . implode("_", $exp));
135  $this->getRole()->setId($id[0]);
136  $this->getRole()->read();
137  }
138  }
139 
140  // Create or update
141  if ($this->getRole()->getId()) {
142  $rbacadmin->deleteRolePermission($this->getRole()->getId(), $this->getRoleFolderId());
143  $this->getRole()->update();
144  } else {
145  $this->getRole()->create();
146  }
147 
148 
149  $this->assignToRoleFolder();
150 
151  $protected = (string) $role['protected'];
152  if ($protected) {
153  $rbacadmin->setProtected(0, $this->getRole()->getId(), 'y');
154  }
155 
156  // Add operations
157  $ops = $rbacreview->getOperations();
158  $operations = array();
159  foreach ($ops as $ope) {
160  $operations[$ope['operation']] = $ope['ops_id'];
161  }
162 
163  foreach ($role->operations as $sxml_operations) {
164  foreach ($sxml_operations as $sxml_op) {
165  $ops_group = (string) $sxml_op['group'];
166  $ops_id = (int) $operations[trim((string) $sxml_op)];
167  $ops = trim((string) $sxml_op);
168 
169  if ($ops_group and $ops_id) {
170  $rbacadmin->setRolePermission(
171  $this->getRole()->getId(),
172  $ops_group,
173  array($ops_id),
174  $this->getRoleFolderId() // #10161
175  );
176  } else {
177  $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Cannot create operation for...');
178  $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': New operation for group ' . $ops_group);
179  $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': New operation ' . $ops);
180  $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': New operation ' . $ops_id);
181  }
182  }
183  }
184 
185  return $this->getRole()->getId();
186  }
187 
193  protected function assigntoRoleFolder()
194  {
195  global $DIC;
196 
197  $rbacadmin = $DIC['rbacadmin'];
198  $rbacreview = $DIC['rbacreview'];
199 
200  if (!$this->getRoleFolderId()) {
201  return;
202  }
203 
204  if ($rbacreview->isRoleAssignedToObject($this->getRole()->getId(), $this->getRoleFolderId())) {
205  return;
206  }
207 
208  $rbacadmin->assignRoleToFolder(
209  $this->getRole()->getId(),
210  $this->getRoleFolderId(),
211  $this->getRole() instanceof ilObjRole ? 'y' : 'n'
212  );
213  }
214 
215 
216  protected function initRole($import_id)
217  {
218  if ($this->getRole()) {
219  return true;
220  }
221 
222  $this->logger->debug('Searching already imported role by import_id: ' . $import_id);
223  $obj_id = 0;
224  if ($import_id) {
225  $obj_id = ilObject::_lookupObjIdByImportId($import_id);
226  }
227  $this->logger->debug('Found already imported obj_id: ' . $obj_id);
228 
229 
230  if ($obj_id) {
231  $this->role = ilObjectFactory::getInstanceByObjId($obj_id, false);
232  }
233  if (
234  (!$this->getRole() instanceof ilObjRole) &&
235  (!$this->getRole() instanceof ilObjRoleTemplate)
236  ) {
237  $this->logger->debug('Creating new role template');
238  $this->role = new ilObjRoleTemplate();
239  }
240  $this->role->setImportId((string) $import_id);
241  return true;
242  }
243 
244  protected function parseXmlErrors()
245  {
246  $errors = '';
247 
248  foreach (libxml_get_errors() as $err) {
249  $errors .= $err->code . '<br/>';
250  }
251  return $errors;
252  }
253 }
importSimpleXml(SimpleXMLElement $role)
Import using simplexml.
Class ilObjRole.
$errors
Class ilObjRoleTemplate.
$type
setRole(ilObject $role)
Set role or role template.
$lng
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
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.
$DIC
Definition: xapitoken.php:46
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.