ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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  {
76  throw new ilRoleImporterException($this->parseXmlErrors());
77  }
78  foreach($root->role as $roleElement)
79  {
80  $this->importSimpleXml($roleElement);
81  // only one role is parsed
82  break;
83  }
84  }
85 
86 
92  {
93  global $rbacadmin, $rbacreview, $lng;
94 
95  $import_id = (string) $role['id'];
96  $GLOBALS['ilLog']->write(__METHOD__.' Importing role with import id '. $import_id);
97 
98  if(!$this->initRole($import_id))
99  {
100  return 0;
101  }
102 
103  $this->getRole()->setTitle(trim((string) $role->title));
104  $this->getRole()->setDescription(trim((string) $role->description));
105 
106  $type = ilObject::_lookupType($this->getRoleFolderId(), true);
107  $exp = explode("_", $this->getRole()->getTitle());
108 
109  if(count($exp) > 0 && $exp[0] === "il")
110  {
111  if(count($exp) > 1 && $exp[1] !== $type)
112  {
113  throw new ilRoleImporterException(sprintf($lng->txt("rbac_cant_import_role_wrong_type"),
114  $lng->txt('obj_'.$exp[1]),$lng->txt('obj_'.$type)));
115  }
116 
117  $exp[3] = $this->getRoleFolderId();
118 
119  $id = ilObjRole::_getIdsForTitle(implode("_", $exp));
120 
121  if($id[0])
122  {
123  $GLOBALS['ilLog']->write(__METHOD__.': Overwrite role '. implode("_", $exp));
124  $this->getRole()->setId($id[0]);
125  $this->getRole()->read();
126  }
127  }
128 
129  // Create or update
130  if($this->getRole()->getId())
131  {
132  $rbacadmin->deleteRolePermission($this->getRole()->getId(), $this->getRoleFolderId());
133  $this->getRole()->update();
134  }
135  else
136  {
137  $this->getRole()->create();
138  }
139 
140 
141  $this->assignToRoleFolder();
142 
143  $protected = (string) $role['protected'];
144  if($protected)
145  {
146  $rbacadmin->setProtected(0,$this->getRole()->getId(),'y');
147  }
148 
149  // Add operations
150  $ops = $rbacreview->getOperations();
151  $operations = array();
152  foreach($ops as $ope)
153  {
154  $operations[$ope['operation']] = $ope['ops_id'];
155  }
156 
157  foreach($role->operations as $sxml_operations)
158  {
159  foreach($sxml_operations as $sxml_op)
160  {
161  $ops_group = (string) $sxml_op['group'];
162  $ops_id = (int) $operations[trim((string) $sxml_op)];
163  $ops = trim((string) $sxml_op);
164 
165  if($ops_group and $ops_id)
166  {
167  $rbacadmin->setRolePermission(
168  $this->getRole()->getId(),
169  $ops_group,
170  array($ops_id),
171  $this->getRoleFolderId() // #10161
172  );
173  }
174  else
175  {
176  $GLOBALS['ilLog']->write(__METHOD__.': Cannot create operation for...');
177  $GLOBALS['ilLog']->write(__METHOD__.': New operation for group '. $ops_group);
178  $GLOBALS['ilLog']->write(__METHOD__.': New operation '.$ops);
179  $GLOBALS['ilLog']->write(__METHOD__.': New operation '. $ops_id);
180  }
181 
182  }
183  }
184 
185  return $this->getRole()->getId();
186  }
187 
193  protected function assigntoRoleFolder()
194  {
195  global $rbacadmin, $rbacreview;
196 
197  if(!$this->getRoleFolderId())
198  {
199  return;
200  }
201 
202  if($rbacreview->isRoleAssignedToObject($this->getRole()->getId(),$this->getRoleFolderId()))
203  {
204  return;
205  }
206 
207  $rbacadmin->assignRoleToFolder(
208  $this->getRole()->getId(),
209  $this->getRoleFolderId(),
210  $this->getRole() instanceof ilObjRole ? 'y' : 'n'
211  );
212  }
213 
214 
215  protected function initRole($import_id)
216  {
217  if($this->getRole())
218  {
219  return true;
220  }
221 
222  $obj_id = ilObject::_lookupObjIdByImportId($import_id);
223  include_once './Services/Object/classes/class.ilObjectFactory.php';
224  if($obj_id)
225  {
226  $this->role = ilObjectFactory::getInstanceByObjId($obj_id,false);
227  }
228  if(!$this->getRole() instanceof ilObjRole or !$this->getRole() instanceof ilObjRoleTemplate)
229  {
230  include_once './Services/AccessControl/classes/class.ilObjRoleTemplate.php';
231  $this->role = new ilObjRoleTemplate();
232  }
233  return true;
234  }
235 
236  protected function parseXmlErrors()
237  {
238  $errors = '';
239 
240  foreach(libxml_get_errors() as $err)
241  {
242  $errors .= $err->code.'<br/>';
243  }
244  return $errors;
245  }
246 }
247 ?>
importSimpleXml(SimpleXMLElement $role)
Import using simplexml.
Class ilObjRole.
Class ilObjRoleTemplate.
setRole(ilObject $role)
Set role or role template.
Class ilObject Basic functions for all objects.
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
Add rich text string
The name of the decorator.
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
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.