ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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();
22  private $filter_type = self::FILTER_SOURCE_TITLE;
23  private $role_template_type = self::TPL_ACTION_OVERWRITE;
24  private $role_template_id = 0;
25 
26 
31  public function __construct($action_id = 0)
32  {
33  parent::__construct($action_id);
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  $rbacreview = $GLOBALS['DIC']->rbac()->review();
174 
175  $source = $this->initSourceObject();
176  // Create a role folder for the new local policies
177 
178  $roles = $this->filterRoles($source);
179 
180  // Create local policy for filtered roles
181  foreach($roles as $role_id => $role)
182  {
183  $this->getLogger()->debug('Apply to role: ' . $role['title']);
184 
185  // No local policies for protected roles of higher context
186  if(
187  $rbacreview->isProtected($role['parent'], $role_id) &&
188  $role['parent'] != $source->getRefId()
189  )
190  {
191  $this->getLogger()->debug('Ignoring protected role.');
192  continue;
193  }
194  $this->createLocalPolicy($source,$role);
195  }
196  return true;
197  }
198 
204  public function revert()
205  {
206  global $rbacadmin,$tree;
207  $rbacreview = $GLOBALS['DIC']->rbac()->review();
208 
209  $source = $this->initSourceObject();
210  $roles = $this->filterRoles($source);
211 
212  // Delete local policy for filtered roles
213  foreach($roles as $role_id => $role)
214  {
215  // Do not delete local policies of auto genrated roles
216  if(!$rbacreview->isGlobalRole($role['obj_id']) and
217  $rbacreview->isAssignable($role['obj_id'],$source->getRefId()) and
218  $rbacreview->isSystemGeneratedRole($role['obj_id']))
219  {
220  $this->getLogger()->debug('Reverting local policy of auto generated role: ' . $role['title']);
221  $this->revertLocalPolicy($source, $role);
222  }
223  else
224  {
225  $this->getLogger()->debug('Reverting local policy and deleting local role: ' . $role['title']);
226 
227  // delete local role and change exiting objects
228  $rbacadmin->deleteLocalRole($role_id,$source->getRefId());
229  // Change existing object
230  include_once './Services/AccessControl/classes/class.ilObjRole.php';
231  $role_obj = new ilObjRole($role_id);
232 
233  $protected = $rbacreview->isProtected($role['parent'], $role['rol_id']);
234 
235  $role_obj->changeExistingObjects(
236  $source->getRefId(),
237  $protected ?
240  array('all')
241  );
242  }
243 
244  }
245  return true;
246  }
247 
252  public function getType()
253  {
254  return self::TYPE_LOCAL_POLICY;
255  }
256 
262  public function toXml(ilXmlWriter $writer)
263  {
264  $writer->xmlStartTag('localPolicyAction');
265 
266  switch($this->getFilterType())
267  {
268  case self::FILTER_SOURCE_TITLE:
269  $writer->xmlStartTag('roleFilter',array('source' => 'title'));
270  break;
271 
272  case self::FILTER_SOURCE_OBJ_ID:
273  $writer->xmlStartTag('roleFilter',array('source' => 'objId'));
274  break;
275 
276  }
277 
278  foreach($this->getFilterPattern() as $pattern)
279  {
280  $pattern->toXml($writer);
281  }
282  $writer->xmlEndTag('roleFilter');
283 
284  $il_role_id = 'il_'.IL_INST_ID.'_'.ilObject::_lookupType($this->getRoleTemplateId()).'_'.$this->getRoleTemplateId();
285 
286  switch($this->getRoleTemplateType())
287  {
288  case self::TPL_ACTION_OVERWRITE:
289  $writer->xmlStartTag(
290  'localPolicyTemplate',
291  array(
292  'type' => 'overwrite',
293  'id' => $il_role_id
294  )
295  );
296  break;
297 
298  case self::TPL_ACTION_INTERSECT:
299  $writer->xmlStartTag(
300  'localPolicyTemplate',
301  array(
302  'type' => 'intersect',
303  'id' => $il_role_id
304  )
305  );
306  break;
307 
308  case self::TPL_ACTION_UNION:
309  $writer->xmlStartTag(
310  'localPolicyTemplate',
311  array(
312  'type' => 'union',
313  'id' => $il_role_id
314  )
315  );
316  break;
317  }
318 
319  include_once './Services/AccessControl/classes/class.ilRoleXmlExport.php';
320  $exp = new ilRoleXmlExport();
321  $exp->setMode(ilRoleXmlExport::MODE_DTPL);
322  $exp->addRole($this->getRoleTemplateId(), ROLE_FOLDER_ID);
323  $exp->write();
324  $writer->appendXML($exp->xmlDumpMem(FALSE));
325  $writer->xmlEndTag('localPolicyTemplate');
326  $writer->xmlEndTag('localPolicyAction');
327  return void;
328  }
329 
333  public function __clone()
334  {
335  parent::__clone();
336 
337  // Clone patterns
338  $cloned = array();
339  foreach($this->getFilterPattern() as $pattern)
340  {
341  $clones[] = clone $pattern;
342  }
343  $this->setFilterPatterns($clones);
344  }
345 
346  public function read()
347  {
348  global $ilDB;
349 
350  if(!parent::read())
351  {
352  return false;
353  }
354 
355  $query = 'SELECT * FROM didactic_tpl_alp '.
356  'WHERE action_id = '.$ilDB->quote($this->getActionId());
357  $res = $ilDB->query($query);
358  while($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
359  {
360  $this->setFilterType($row->filter_type);
361  $this->setRoleTemplateType($row->template_type);
362  $this->setRoleTemplateId($row->template_id);
363  }
364 
365  // Read filter
366  include_once './Services/DidacticTemplate/classes/class.ilDidacticTemplateFilterPatternFactory.php';
367  foreach(ilDidacticTemplateFilterPatternFactory::lookupPatternsByParentId($this->getActionId(),self::PATTERN_PARENT_TYPE) as $pattern)
368  {
369  $this->addFilterPattern($pattern);
370  }
371  }
372 
373 
380  protected function createLocalPolicy(ilObject $source, $role)
381  {
382  global $rbacreview, $rbacadmin;
383 
384  // fetch role information
385  $role_data = array();
386  foreach($rbacreview->getParentRoleIds($source->getRefId()) as $role_id => $tmp_role)
387  {
388  if($role_id == $role['obj_id'])
389  {
390  $role_data = $tmp_role;
391  }
392  }
393 
394  // Add local policy
395  if(!$rbacreview->isRoleAssignedToObject($role['obj_id'],$source->getRefId()))
396  {
397  $GLOBALS['DIC']->rbac()->admin()->assignRoleToFolder(
398  $role['obj_id'],
399  $source->getRefId(),
400  'n'
401  );
402  }
403 
404  // do nothing if role is protected in higher context
405  if(
406  $GLOBALS['DIC']->rbac()->review()->isProtected($source->getRefId(),$role['obj_id'])
407  )
408  {
409  $GLOBALS['DIC']->logger()->otpl()->info('Ignoring protected role: ' . $role['title']);
410  return true;
411  }
412 
413  switch($this->getRoleTemplateType())
414  {
415  case self::TPL_ACTION_UNION:
416 
417  ilLoggerFactory::getLogger('otpl')->info('Using ilRbacAdmin::copyRolePermissionUnion()');
418  $rbacadmin->copyRolePermissionUnion(
419  $role_data['obj_id'],
420  $role_data['parent'],
421  $this->getRoleTemplateId(),
422  ROLE_FOLDER_ID,
423  $role_data['obj_id'],
424  $source->getRefId()
425  );
426  break;
427 
428  case self::TPL_ACTION_OVERWRITE:
429 
430  ilLoggerFactory::getLogger('otpl')->info('Using ilRbacAdmin::copyRoleTemplatePermission()');
431  $rbacadmin->copyRoleTemplatePermissions(
432  $this->getRoleTemplateId(),
433  ROLE_FOLDER_ID,
434  $source->getRefId(),
435  $role_data['obj_id'],
436  true
437  );
438  break;
439 
440  case self::TPL_ACTION_INTERSECT:
441 
442  ilLoggerFactory::getLogger('otpl')->info('Using ilRbacAdmin::copyRolePermissionIntersection()'. $this->getRoleTemplateId());
443  $rbacadmin->copyRolePermissionIntersection(
444  $role_data['obj_id'],
445  $role_data['parent'],
446  $this->getRoleTemplateId(),
447  ROLE_FOLDER_ID,
448  $source->getRefId(),
449  $role_data['obj_id']
450  );
451  break;
452 
453  }
454 
455  // Change existing object
456  include_once './Services/AccessControl/classes/class.ilObjRole.php';
457  $role_obj = new ilObjRole($role_data['obj_id']);
458  $role_obj->changeExistingObjects(
459  $source->getRefId(),
461  array('all')
462  );
463 
464  return true;
465  }
466 
467  protected function revertLocalPolicy(ilObject $source, $role)
468  {
469  global $rbacadmin, $rbacreview, $ilDB;
470 
471  ilLoggerFactory::getLogger('otpl')->info('Reverting policy for role '. $role['title']);
472  // Local policies can only be reverted for auto generated roles. Otherwise the
473  // original role settings are unknown
474  if(substr($role['title'],0,3) != 'il_')
475  {
476  ilLoggerFactory::getLogger('otpl')->warning('Cannot revert local policy for role '. $role['title']);
477  return false;
478  }
479 
480 
481  // No local policies
482  if(!$rbacreview->getLocalPolicies($source->getRefId()))
483  {
484  return false;
485  }
486 
487  $exploded_title = explode('_',$role['title']);
488  $rolt_title = $exploded_title[0].'_'.$exploded_title[1].'_'.$exploded_title[2];
489 
490  // Lookup role template
491  $query = 'SELECT obj_id FROM object_data '.
492  'WHERE title = '.$ilDB->quote($rolt_title,'text').' '.
493  'AND type = '.$ilDB->quote('rolt','text');
494  $res = $ilDB->query($query);
495  while($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
496  {
497  $rolt_id = $row->obj_id;
498  }
499 
500  // No template found
501  if(!$rolt_id)
502  {
503  return false;
504  }
505 
506  $rbacadmin->copyRoleTemplatePermissions(
507  $rolt_id,
508  ROLE_FOLDER_ID,
509  $source->getRefId(),
510  $role['obj_id'],
511  true
512  );
513 
514  // Change existing object
515  include_once './Services/AccessControl/classes/class.ilObjRole.php';
516  $role_obj = new ilObjRole($role['obj_id']);
517  $role_obj->changeExistingObjects(
518  $source->getRefId(),
520  array('all')
521  );
522  }
523 }
524 ?>
Class ilObjRole.
static lookupPatternsByParentId($a_parent_id, $a_parent_type)
Get patterns by template id.
const MODE_PROTECTED_DELETE_LOCAL_POLICIES
Class ilObject Basic functions for all objects.
xmlStartTag($tag, $attrs=NULL, $empty=FALSE, $encode=TRUE, $escape=TRUE)
Writes a starttag.
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
XML writer class.
xmlEndTag($tag)
Writes an endtag.
addFilterPattern(ilDidacticTemplateFilterPattern $pattern)
Add filter.
filterRoles(ilObject $source)
Filter roles.
$a_type
Definition: workflow.php:93
createLocalPolicy(ilObject $source, $role)
Create local policy.
Create styles array
The data for the language used.
static _lookupType($a_id, $a_reference=false)
lookup object type
initSourceObject()
Init the source object.
Abstract class for template actions.
const MODE_UNPROTECTED_DELETE_LOCAL_POLICIES
Xml export of roles and role templates.
global $ilDB
getRefId()
get reference id public
appendXML($a_str)
append xml string to document
static getLogger($a_component_id)
Get component logger.
Represents a filter pattern for didactic template actions.