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