ILIAS  Release_4_4_x_branch Revision 61816
 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->isGlobalRole($role['obj_id']) and
220  $rbacreview->isAssignable($role['obj_id'],$role['parent']) and
221  $rbacreview->isSystemGeneratedRole($role['obj_id']))
222  {
223  $this->revertLocalPolicy($source, $role);
224  }
225  else
226  {
227  // delete local role and change exiting objects
228  $rbacadmin->deleteLocalRole($role_id,$rolf);
229  // Change existing object
230  include_once './Services/AccessControl/classes/class.ilObjRole.php';
231  $role_obj = new ilObjRole($role_id);
232  $role_obj->changeExistingObjects(
233  $source->getRefId(),
234  $role['protected'] ?
237  array('all')
238  );
239  }
240 
241  }
242  return true;
243  }
244 
249  public function getType()
250  {
252  }
253 
259  public function toXml(ilXmlWriter $writer)
260  {
261  $writer->xmlStartTag('localPolicyAction');
262 
263  switch($this->getFilterType())
264  {
265  case self::FILTER_SOURCE_TITLE:
266  $writer->xmlStartTag('roleFilter',array('source' => 'title'));
267  break;
268 
269  case self::FILTER_SOURCE_OBJ_ID:
270  $writer->xmlStartTag('roleFilter',array('source' => 'objId'));
271  break;
272 
273  }
274 
275  foreach($this->getFilterPattern() as $pattern)
276  {
277  $pattern->toXml($writer);
278  }
279  $writer->xmlEndTag('roleFilter');
280 
281  $il_role_id = 'il_'.IL_INST_ID.'_'.ilObject::_lookupType($this->getRoleTemplateId()).'_'.$this->getRoleTemplateId();
282 
283  switch($this->getRoleTemplateType())
284  {
285  case self::TPL_ACTION_OVERWRITE:
286  $writer->xmlStartTag(
287  'localPolicyTemplate',
288  array(
289  'type' => 'overwrite',
290  'id' => $il_role_id
291  )
292  );
293  break;
294 
295  case self::TPL_ACTION_INTERSECT:
296  $writer->xmlStartTag(
297  'localPolicyTemplate',
298  array(
299  'type' => 'intersect',
300  'id' => $il_role_id
301  )
302  );
303  break;
304 
305  case self::TPL_ACTION_UNION:
306  $writer->xmlStartTag(
307  'localPolicyTemplate',
308  array(
309  'type' => 'union',
310  'id' => $il_role_id
311  )
312  );
313  break;
314  }
315 
316  include_once './Services/AccessControl/classes/class.ilRoleXmlExport.php';
317  $exp = new ilRoleXmlExport();
318  $exp->setMode(ilRoleXmlExport::MODE_DTPL);
319  $exp->addRole($this->getRoleTemplateId(), ROLE_FOLDER_ID);
320  $exp->write();
321  $writer->appendXML($exp->xmlDumpMem(FALSE));
322  $writer->xmlEndTag('localPolicyTemplate');
323  $writer->xmlEndTag('localPolicyAction');
324  return void;
325  }
326 
330  public function __clone()
331  {
332  parent::__clone();
333 
334  // Clone patterns
335  $cloned = array();
336  foreach($this->getFilterPattern() as $pattern)
337  {
338  $clones[] = clone $pattern;
339  }
340  $this->setFilterPatterns($clones);
341  }
342 
343  public function read()
344  {
345  global $ilDB;
346 
347  if(!parent::read())
348  {
349  return false;
350  }
351 
352  $query = 'SELECT * FROM didactic_tpl_alp '.
353  'WHERE action_id = '.$ilDB->quote($this->getActionId());
354  $res = $ilDB->query($query);
355  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
356  {
357  $this->setFilterType($row->filter_type);
358  $this->setRoleTemplateType($row->template_type);
359  $this->setRoleTemplateId($row->template_id);
360  }
361 
362  // Read filter
363  include_once './Services/DidacticTemplate/classes/class.ilDidacticTemplateFilterPatternFactory.php';
364  foreach(ilDidacticTemplateFilterPatternFactory::lookupPatternsByParentId($this->getActionId(),self::PATTERN_PARENT_TYPE) as $pattern)
365  {
366  $this->addFilterPattern($pattern);
367  }
368  }
369 
370 
377  protected function createLocalPolicy(ilObject $source, $role)
378  {
379  global $rbacreview, $rbacadmin;
380 
381  $GLOBALS['ilLog']->write(__METHOD__.': Using role: '.print_r($role,true));
382 
383  $role_folder_id = $rbacreview->getRoleFolderIdOfObject($source->getRefId());
384 
385  $GLOBALS['ilLog']->write(__METHOD__.': Role folder id: '.$role_folder_id);
386 
387  // Add local policy
388 
389  if(!$rbacreview->isRoleAssignedToFolder($role['obj_id'],$role_folder_id))
390  {
391  $rbacadmin->assignRoleToFolder($role['obj_id'],$role_folder_id,'n');
392  }
393 
394  switch($this->getRoleTemplateType())
395  {
396  case self::TPL_ACTION_UNION:
397 
398  $GLOBALS['ilLog']->write(__METHOD__.': Using ilRbacAdmin::copyRolePermissionUnion()');
399  $rbacadmin->copyRolePermissionUnion(
400  $role['obj_id'],
401  $role['parent'],
402  $this->getRoleTemplateId(),
403  ROLE_FOLDER_ID,
404  $role['obj_id'],
405  $role_folder_id
406  );
407  break;
408 
409  case self::TPL_ACTION_OVERWRITE:
410 
411  $GLOBALS['ilLog']->write(__METHOD__.': Using ilRbacAdmin::copyRoleTemplatePermissions()');
412  $rbacadmin->copyRoleTemplatePermissions(
413  $this->getRoleTemplateId(),
414  ROLE_FOLDER_ID,
415  $role_folder_id,
416  $role['obj_id'],
417  true
418  );
419  break;
420 
421  case self::TPL_ACTION_INTERSECT:
422 
423  $GLOBALS['ilLog']->write(__METHOD__.': Using ilRbacAdmin::copyRolePermissionIntersection()');
424  $rbacadmin->copyRolePermissionIntersection(
425  $role['obj_id'],
426  $role['parent'],
427  $this->getRoleTemplateId(),
428  ROLE_FOLDER_ID,
429  $role_folder_id,
430  $role['obj_id']
431  );
432  break;
433 
434  }
435 
436  // Change existing object
437  include_once './Services/AccessControl/classes/class.ilObjRole.php';
438  $role_obj = new ilObjRole($role['obj_id']);
439  $role_obj->changeExistingObjects(
440  $source->getRefId(),
442  array('all')
443  );
444 
445  return true;
446  }
447 
448  protected function revertLocalPolicy(ilObject $source, $role)
449  {
450  global $rbacadmin, $rbacreview, $ilDB;
451 
452  $GLOBALS['ilLog']->write(__METHOD__.': Reverting policy for role: '.print_r($role,true));
453 
454  // Local policies can only be reverted for auto generated roles. Otherwise the
455  // original role settings are unknown
456  if(substr($role['title'],0,3) != 'il_')
457  {
458  $GLOBALS['ilLog']->write(__METHOD__.': Cannot revert local policy for role '. $role['title']);
459  return false;
460  }
461 
462 
463  $role_folder_id = $rbacreview->getRoleFolderIdOfObject($source->getRefId());
464  // No role folder found
465  if(!$role_folder_id)
466  {
467  return false;
468  }
469 
470  $exploded_title = explode('_',$role['title']);
471  $rolt_title = $exploded_title[0].'_'.$exploded_title[1].'_'.$exploded_title[2];
472 
473  // Lookup role template
474  $query = 'SELECT obj_id FROM object_data '.
475  'WHERE title = '.$ilDB->quote($rolt_title,'text').' '.
476  'AND type = '.$ilDB->quote('rolt','text');
477  $res = $ilDB->query($query);
478  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
479  {
480  $rolt_id = $row->obj_id;
481  }
482 
483  // No template found
484  if(!$rolt_id)
485  {
486  return false;
487  }
488 
489  $rbacadmin->copyRoleTemplatePermissions(
490  $rolt_id,
491  ROLE_FOLDER_ID,
492  $role_folder_id,
493  $role['obj_id'],
494  true
495  );
496 
497  // Change existing object
498  include_once './Services/AccessControl/classes/class.ilObjRole.php';
499  $role_obj = new ilObjRole($role['obj_id']);
500  $role_obj->changeExistingObjects(
501  $source->getRefId(),
503  array('all')
504  );
505  }
506 }
507 ?>