ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilDidacticTemplateLocalPolicyAction.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/DidacticTemplate/classes/class.ilDidacticTemplateAction.php';
5 
13 {
16  const TPL_ACTION_ADD = 3;
18  const TPL_ACTION_UNION = 5;
19 
20 
21  private $pattern = array();
24  private $role_template_id = 0;
25 
26 
31  public function __construct($action_id = 0)
32  {
34  }
35 
41  {
42  $this->pattern[] = $pattern;
43  }
44 
49  public function setFilterPatterns(Array $patterns)
50  {
51  $this->pattern = $patterns;
52  }
53 
58  public function getFilterPattern()
59  {
60  return $this->pattern;
61  }
62 
67  public function setFilterType($a_type)
68  {
69  $this->filter_type = $a_type;
70  }
71 
76  public function getFilterType()
77  {
78  return $this->filter_type;
79  }
80 
85  public function setRoleTemplateType($a_tpl_type)
86  {
87  $this->role_template_type = $a_tpl_type;
88  }
89 
93  public function getRoleTemplateType()
94  {
96  }
97 
102  public function setRoleTemplateId($a_id)
103  {
104  $this->role_template_id = $a_id;
105  }
106 
111  public function getRoleTemplateId()
112  {
114  }
115 
119  public function save()
120  {
121  global $ilDB;
122 
123  parent::save();
124 
125  $query = 'INSERT INTO didactic_tpl_alp (action_id,filter_type,template_type,template_id) '.
126  'VALUES( '.
127  $ilDB->quote($this->getActionId(),'integer').', '.
128  $ilDB->quote($this->getFilterType(),'integer').', '.
129  $ilDB->quote($this->getRoleTemplateType(),'integer').', '.
130  $ilDB->quote($this->getRoleTemplateId(),'integer').' '.
131  ')';
132  $ilDB->manipulate($query);
133 
134  foreach($this->getFilterPattern() as $pattern)
135  {
136  /* @var ilDidacticTemplateFilterPattern $pattern */
137  $pattern->setParentId($this->getActionId());
138  $pattern->setParentType(self::PATTERN_PARENT_TYPE);
139  $pattern->save();
140  }
141  }
142 
148  public function delete()
149  {
150  global $ilDB;
151 
152  parent::delete();
153 
154  $query = 'DELETE FROM didactic_tpl_alp '.
155  'WHERE action_id = '.$ilDB->quote($this->getActionId(),'integer');
156  $ilDB->manipulate($query);
157 
158  foreach($this->getFilterPattern() as $pattern)
159  {
160  $pattern->delete();
161  }
162  return true;
163  }
164 
165 
166 
167 
171  public function apply()
172  {
173  global $rbacreview;
174 
175  $source = $this->initSourceObject();
176  // Create a role folder for the new local policies
177 
178  $roles = $this->filterRoles($source);
179 
180  // Create role folder if there is any local role left
181  $rolf = $rbacreview->getRoleFolderIdOfObject($source->getRefId());
182 
183  if(count($roles) and !$rolf)
184  {
185  $rolf = $source->createRoleFolder();
186  $rolf = $rolf->getRefId();
187  }
188  // Create local policy for filtered roles
189  foreach($roles as $role_id => $role)
190  {
191  // No local policies for protected roles of higher context
192  if($role['protected'] and $role['parent'] != $rolf)
193  {
194  continue;
195  }
196  $this->createLocalPolicy($source,$role);
197  }
198  return true;
199  }
200 
206  public function revert()
207  {
208  global $rbacreview,$rbacadmin,$tree;
209 
210  $source = $this->initSourceObject();
211  $rolf = $rbacreview->getRoleFolderIdOfObject($source->getRefId());
212 
213  $roles = $this->filterRoles($source);
214 
215  // Delete local policy for filtered roles
216  foreach($roles as $role_id => $role)
217  {
218  // Do not delete local roles of auto genrated roles
219  if($rbacreview->isLocalRole($role['obj_id'],$role['parent']) and
220  $rbacreview->isSystemGeneratedRole($role['obj_id']))
221  {
222  $this->revertLocalPolicy($source, $role);
223  }
224  else
225  {
226  $rbacadmin->deleteLocalRole($role_id,$rolf);
227  }
228 
229  }
230  return true;
231  }
232 
237  public function getType()
238  {
240  }
241 
247  public function toXml(ilXmlWriter $writer)
248  {
249  $writer->xmlStartTag('localPolicyAction');
250 
251  switch($this->getFilterType())
252  {
253  case self::FILTER_SOURCE_TITLE:
254  $writer->xmlStartTag('roleFilter',array('source' => 'title'));
255  break;
256 
257  case self::FILTER_SOURCE_OBJ_ID:
258  $writer->xmlStartTag('roleFilter',array('source' => 'objId'));
259  break;
260 
261  }
262 
263  foreach($this->getFilterPattern() as $pattern)
264  {
265  $pattern->toXml($writer);
266  }
267  $writer->xmlEndTag('roleFilter');
268 
269  $il_role_id = 'il_'.IL_INST_ID.'_'.ilObject::_lookupType($this->getRoleTemplateId()).'_'.$this->getRoleTemplateId();
270 
271  switch($this->getRoleTemplateType())
272  {
273  case self::TPL_ACTION_OVERWRITE:
274  $writer->xmlStartTag(
275  'localPolicyTemplate',
276  array(
277  'type' => 'overwrite',
278  'id' => $il_role_id
279  )
280  );
281  break;
282 
283  case self::TPL_ACTION_INTERSECT:
284  $writer->xmlStartTag(
285  'localPolicyTemplate',
286  array(
287  'type' => 'intersect',
288  'id' => $il_role_id
289  )
290  );
291  break;
292 
293  case self::TPL_ACTION_UNION:
294  $writer->xmlStartTag(
295  'localPolicyTemplate',
296  array(
297  'type' => 'union',
298  'id' => $il_role_id
299  )
300  );
301  break;
302  }
303 
304  include_once './Services/AccessControl/classes/class.ilRoleXmlExport.php';
305  $exp = new ilRoleXmlExport();
306  $exp->setMode(ilRoleXmlExport::MODE_DTPL);
307  $exp->addRole($this->getRoleTemplateId(), ROLE_FOLDER_ID);
308  $exp->write();
309  $writer->appendXML($exp->xmlDumpMem(FALSE));
310  $writer->xmlEndTag('localPolicyTemplate');
311  $writer->xmlEndTag('localPolicyAction');
312  return void;
313  }
314 
318  public function __clone()
319  {
320  parent::__clone();
321 
322  // Clone patterns
323  $cloned = array();
324  foreach($this->getFilterPattern() as $pattern)
325  {
326  $clones[] = clone $pattern;
327  }
328  $this->setFilterPatterns($clones);
329  }
330 
331  public function read()
332  {
333  global $ilDB;
334 
335  if(!parent::read())
336  {
337  return false;
338  }
339 
340  $query = 'SELECT * FROM didactic_tpl_alp '.
341  'WHERE action_id = '.$ilDB->quote($this->getActionId());
342  $res = $ilDB->query($query);
343  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
344  {
345  $this->setFilterType($row->filter_type);
346  $this->setRoleTemplateType($row->template_type);
347  $this->setRoleTemplateId($row->template_id);
348  }
349 
350  // Read filter
351  include_once './Services/DidacticTemplate/classes/class.ilDidacticTemplateFilterPatternFactory.php';
352  foreach(ilDidacticTemplateFilterPatternFactory::lookupPatternsByParentId($this->getActionId(),self::PATTERN_PARENT_TYPE) as $pattern)
353  {
354  $this->addFilterPattern($pattern);
355  }
356  }
357 
358 
365  protected function createLocalPolicy(ilObject $source, $role)
366  {
367  global $rbacreview, $rbacadmin;
368 
369  $GLOBALS['ilLog']->write(__METHOD__.': Using role: '.print_r($role,true));
370 
371  $role_folder_id = $rbacreview->getRoleFolderIdOfObject($source->getRefId());
372 
373  $GLOBALS['ilLog']->write(__METHOD__.': Role folder id: '.$role_folder_id);
374 
375  // Add local policy
376 
377  if(!$rbacreview->isRoleAssignedToFolder($role['obj_id'],$role_folder_id))
378  {
379  $rbacadmin->assignRoleToFolder($role['obj_id'],$role_folder_id,'n');
380  }
381 
382  switch($this->getRoleTemplateType())
383  {
384  case self::TPL_ACTION_UNION:
385 
386  $GLOBALS['ilLog']->write(__METHOD__.': Using ilRbacAdmin::copyRolePermissionUnion()');
387  $rbacadmin->copyRolePermissionUnion(
388  $role['obj_id'],
389  $role['parent'],
390  $this->getRoleTemplateId(),
391  ROLE_FOLDER_ID,
392  $role['obj_id'],
393  $role_folder_id
394  );
395  break;
396 
397  case self::TPL_ACTION_OVERWRITE:
398 
399  $GLOBALS['ilLog']->write(__METHOD__.': Using ilRbacAdmin::copyRoleTemplatePermissions()');
400  $rbacadmin->copyRoleTemplatePermissions(
401  $this->getRoleTemplateId(),
402  ROLE_FOLDER_ID,
403  $role_folder_id,
404  $role['obj_id'],
405  true
406  );
407  break;
408 
409  case self::TPL_ACTION_INTERSECT:
410 
411  $GLOBALS['ilLog']->write(__METHOD__.': Using ilRbacAdmin::copyRolePermissionIntersection()');
412  $rbacadmin->copyRolePermissionIntersection(
413  $role['obj_id'],
414  $role['parent'],
415  $this->getRoleTemplateId(),
416  ROLE_FOLDER_ID,
417  $role_folder_id,
418  $role['obj_id']
419  );
420  break;
421 
422  }
423 
424  // Change existing object
425  include_once './Services/AccessControl/classes/class.ilObjRole.php';
426  $role_obj = new ilObjRole($role['obj_id']);
427  $role_obj->changeExistingObjects(
428  $source->getRefId(),
430  array('all')
431  );
432 
433  return true;
434  }
435 
436  protected function revertLocalPolicy(ilObject $source, $role)
437  {
438  global $rbacadmin, $rbacreview, $ilDB;
439 
440  $GLOBALS['ilLog']->write(__METHOD__.': Reverting policy for role: '.print_r($role,true));
441 
442  // Local policies can only be reverted for auto generated roles. Otherwise the
443  // original role settings are unknown
444  if(substr($role['title'],0,3) != 'il_')
445  {
446  $GLOBALS['ilLog']->write(__METHOD__.': Cannot revert local policy for role '. $role['title']);
447  return false;
448  }
449 
450 
451  $role_folder_id = $rbacreview->getRoleFolderIdOfObject($source->getRefId());
452  // No role folder found
453  if(!$role_folder_id)
454  {
455  return false;
456  }
457 
458  $exploded_title = explode('_',$role['title']);
459  $rolt_title = $exploded_title[0].'_'.$exploded_title[1].'_'.$exploded_title[2];
460 
461  // Lookup role template
462  $query = 'SELECT obj_id FROM object_data '.
463  'WHERE title = '.$ilDB->quote($rolt_title,'text').' '.
464  'AND type = '.$ilDB->quote('rolt','text');
465  $res = $ilDB->query($query);
466  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
467  {
468  $rolt_id = $row->obj_id;
469  }
470 
471  // No template found
472  if(!$rolt_id)
473  {
474  return false;
475  }
476 
477  $rbacadmin->copyRoleTemplatePermissions(
478  $rolt_id,
479  ROLE_FOLDER_ID,
480  $role_folder_id,
481  $role['obj_id'],
482  true
483  );
484 
485  // Change existing object
486  include_once './Services/AccessControl/classes/class.ilObjRole.php';
487  $role_obj = new ilObjRole($role['obj_id']);
488  $role_obj->changeExistingObjects(
489  $source->getRefId(),
491  array('all')
492  );
493  }
494 }
495 ?>