ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups 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  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 
91  public function importSimpleXml(SimpleXMLElement $role)
92  {
93  global $rbacadmin, $rbacreview;
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 
107  // Create or update
108  if($this->getRole()->getId())
109  {
110  $this->getRole()->update();
111  }
112  else
113  {
114  $this->getRole()->create();
115  }
116 
117 
118  $this->assignToRoleFolder();
119 
120  $protected = (string) $role['protected'];
121  if($protected)
122  {
123  $rbacadmin->setProtected(0,$this->getRole()->getId(),'y');
124  }
125 
126  // Add operations
127  $ops = $rbacreview->getOperations();
128  $operations = array();
129  foreach($ops as $ope)
130  {
131  $operations[$ope['operation']] = $ope['ops_id'];
132  }
133 
134  foreach($role->operations as $sxml_operations)
135  {
136  foreach($sxml_operations as $sxml_op)
137  {
138  $ops_group = (string) $sxml_op['group'];
139  $ops_id = (int) $operations[trim((string) $sxml_op)];
140  $ops = trim((string) $sxml_op);
141 
142  if($ops_group and $ops_id)
143  {
144  $rbacadmin->setRolePermission(
145  $this->getRole()->getId(),
146  $ops_group,
147  array($ops_id),
148  $this->getRoleFolderId() // #10161
149  );
150  }
151  else
152  {
153  $GLOBALS['ilLog']->write(__METHOD__.': Cannot create operation for...');
154  $GLOBALS['ilLog']->write(__METHOD__.': New operation for group '. $ops_group);
155  $GLOBALS['ilLog']->write(__METHOD__.': New operation '.$ops);
156  $GLOBALS['ilLog']->write(__METHOD__.': New operation '. $ops_id);
157  }
158 
159  }
160  }
161 
162  return $this->getRole()->getId();
163  }
164 
170  protected function assigntoRoleFolder()
171  {
172  global $rbacadmin;
173 
174  if(!$this->getRoleFolderId())
175  {
176  return;
177  }
178 
179  $rbacadmin->assignRoleToFolder(
180  $this->getRole()->getId(),
181  $this->getRoleFolderId(),
182  $this->getRole() instanceof ilObjRole ? 'y' : 'n'
183  );
184  }
185 
186 
187  protected function initRole($import_id)
188  {
189  if($this->getRole())
190  {
191  return true;
192  }
193 
194  $obj_id = ilObject::_lookupObjIdByImportId($import_id);
195  include_once './Services/Object/classes/class.ilObjectFactory.php';
196  if($obj_id)
197  {
198  $this->role = ilObjectFactory::getInstanceByObjId($obj_id,false);
199  }
200  if(!$this->getRole() instanceof ilObjRole or !$this->getRole() instanceof ilObjRoleTemplate)
201  {
202  include_once './Services/AccessControl/classes/class.ilObjRoleTemplate.php';
203  $this->role = new ilObjRoleTemplate();
204  }
205  return true;
206  }
207 
208  protected function parseXmlErrors()
209  {
210  $errors = '';
211 
212  foreach(libxml_get_errors() as $err)
213  {
214  $errors .= $err->code.'<br/>';
215  }
216  return $errors;
217  }
218 }
219 ?>