ILIAS  trunk Revision v11.0_alpha-1769-g99a433fe2dc
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilObjRoleTemplateGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
24 
33 {
34  private const FORM_KEY_TITLE = 'title';
35  private const FORM_KEY_DESCRIPTION = 'description';
36  private const FORM_KEY_ILIAS_ID = 'ilias_id';
37  private const FORM_KEY_PROTECT = 'protect';
38 
39  private int $rolf_ref_id;
40 
42 
44  protected Factory $refinery;
45 
46  public function __construct($a_data, int $a_id, bool $a_call_by_reference)
47  {
48  global $DIC;
49 
50  $this->rbac_admin = $DIC->rbac()->admin();
51 
52  $this->type = "rolt";
53  parent::__construct($a_data, $a_id, $a_call_by_reference, false);
54  $this->lng->loadLanguageModule('rbac');
55  $this->rolf_ref_id = &$this->ref_id;
56  $this->ctrl->saveParameter($this, "obj_id");
57  $this->http = $DIC->http();
58  $this->refinery = $DIC->refinery();
59  }
60 
61  public function executeCommand(): void
62  {
63  $this->prepareOutput();
64 
65  $next_class = $this->ctrl->getNextClass($this);
66  $cmd = $this->ctrl->getCmd();
67 
68  switch ($next_class) {
69  default:
70  if (!$cmd) {
71  $cmd = "perm";
72  }
73  $cmd .= "Object";
74  $this->$cmd();
75 
76  break;
77  }
78  }
79 
80  protected function getRoleTemplateForm(bool $is_role_creation_form = false): StandardForm
81  {
82  if ($this->creation_mode) {
83  $this->ctrl->setParameter($this, 'new_type', 'rolt');
84  }
85 
86  $ff = $this->ui_factory->input()->field();
87 
88  $title_validation_constraint = $this->refinery->custom()->constraint(
89  fn(string $v): bool => preg_match('/^il_.*$/', $v) ? false : true,
90  $this->lng->txt('msg_role_reserved_prefix')
91  );
92 
93  $inputs = [
94  self::FORM_KEY_TITLE => $ff->text($this->lng->txt('title'))
95  ->withMaxLength(70)
96  ->withRequired(true)
97  ->withAdditionalTransformation($title_validation_constraint)
98  ->withValue(
99  $is_role_creation_form ? ''
100  : ilObjRole::_getTranslation($this->object->getTitle())
101  )->withDisabled($is_role_creation_form ? false : $this->object->isInternalTemplate()),
102  self::FORM_KEY_DESCRIPTION => $ff->textarea($this->lng->txt('description'))
103  ->withMaxLimit(4000)
104  ->withValue($is_role_creation_form ? '' : $this->object->getDescription())
105  ];
106 
107  if (!$is_role_creation_form) {
108  $inputs[self::FORM_KEY_ILIAS_ID] = $ff->text($this->lng->txt('ilias_id'))
109  ->withDisabled(true)
110  ->withValue('il_' . IL_INST_ID . '_'
111  . $this->object->getType() . '_' . $this->object->getId());
112  }
113 
114  $inputs[self::FORM_KEY_PROTECT] = $ff->checkbox($this->lng->txt('role_protect_permissions'))
115  ->withValue(
116  $is_role_creation_form
117  ? false
118  : $this->rbac_review->isProtected($this->rolf_ref_id, $this->object->getId())
119  );
120 
121  return $this->ui_factory->input()->container()->form()->standard(
122  $this->ctrl->getFormActionByClass(
123  self::class,
124  $is_role_creation_form ? 'save' : 'update'
125  ),
126  $inputs
127  )->withSubmitLabel(
128  $is_role_creation_form ? $this->lng->txt('rolt_new') : $this->lng->txt('save')
129  );
130  }
131 
132  public function createObject(): void
133  {
134  if (!$this->rbac_system->checkAccess('create_rolt', $this->rolf_ref_id)) {
135  $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
136  }
137 
138  $this->tabs_gui->setBackTarget(
139  $this->lng->txt('cancel'),
140  $this->ctrl->getParentReturnByClass(self::class)
141  );
142 
143  $this->tpl->setContent(
144  $this->ui_renderer->render(
145  $this->ui_factory->panel()->standard(
146  $this->lng->txt('rolt_new'),
147  $this->getRoleTemplateForm(true)
148  )
149  )
150  );
151  }
152 
156  public function editObject(?ilPropertyFormGUI $form = null): void
157  {
158  $this->tabs_gui->activateTab('settings');
159 
160  if (!$this->rbac_system->checkAccess("write", $this->rolf_ref_id)) {
161  $this->error->raiseError($this->lng->txt("msg_no_perm_write"), $this->error->MESSAGE);
162  }
163 
164  $this->tpl->setContent(
165  $this->ui_renderer->render(
166  $this->ui_factory->panel()->standard(
167  $this->lng->txt('rolt_edit'),
168  $this->getRoleTemplateForm()
169  )
170  )
171  );
172  }
173 
174  public function saveObject(): void
175  {
176  if (!$this->rbac_system->checkAccess("create_rolt", $this->rolf_ref_id)) {
177  $this->ilias->raiseError($this->lng->txt("msg_no_perm_create_rolt"), $this->ilias->error_obj->WARNING);
178  }
179 
180  $form = $this->getRoleTemplateForm(true)->withRequest($this->request);
181  $data = $form->getData();
182  if ($data === null) {
183  $this->tabs_gui->setBackTarget(
184  $this->lng->txt('cancel'),
185  $this->ctrl->getParentReturnByClass(self::class)
186  );
187 
188  $this->tpl->setContent(
189  $this->ui_renderer->render(
190  $this->ui_factory->panel()->standard(
191  $this->lng->txt('rolt_new'),
192  $form
193  )
194  )
195  );
196  return;
197  }
198 
199  $role_template = new ilObjRoleTemplate();
200  $role_template->setTitle($data[self::FORM_KEY_TITLE]);
201  $role_template->setDescription($data[self::FORM_KEY_DESCRIPTION]);
202  $role_template->create();
203  $this->rbac_admin->assignRoleToFolder($role_template->getId(), $this->rolf_ref_id, 'n');
204  $this->rbac_admin->setProtected(
205  $this->rolf_ref_id,
206  $role_template->getId(),
207  $data[self::FORM_KEY_PROTECT] ? 'y' : 'n'
208  );
209  $this->tpl->setOnScreenMessage('success', $this->lng->txt("rolt_added"), true);
210  $this->ctrl->setParameter($this, 'obj_id', $role_template->getId());
211  $this->ctrl->redirect($this, 'perm');
212  }
213 
214  public function updateObject(): void
215  {
216  if (!$this->rbac_system->checkAccess('write', $this->rolf_ref_id)) {
217  $this->error->raiseError($this->lng->txt('msg_no_perm_modify_rolt'), $this->error->WARNING);
218  }
219 
220  $form = $this->getRoleTemplateForm()->withRequest($this->request);
221  $data = $form->getData();
222  if ($data === null) {
223  $this->tpl->setContent(
224  $this->ui_renderer->render(
225  $this->ui_factory->panel()->standard(
226  $this->lng->txt('rolt_edit'),
227  $form
228  )
229  )
230  );
231  return;
232  }
233 
234  if (!$this->object->isInternalTemplate()) {
235  $this->object->setTitle($data[self::FORM_KEY_TITLE]);
236  }
237 
238  $this->object->setDescription($data[self::FORM_KEY_DESCRIPTION]);
239  $this->object->update();
240  $this->rbac_admin->setProtected(
241  $this->rolf_ref_id,
242  $this->object->getId(),
243  $data[self::FORM_KEY_PROTECT] ? 'y' : 'n'
244  );
245  $this->tpl->setOnScreenMessage('success', $this->lng->txt("saved_successfully"), true);
246  $this->ctrl->returnToParent($this);
247  }
248 
249  protected function permObject(): void
250  {
251  if (!$this->rbac_system->checkAccess('edit_permission', $this->ref_id)) {
252  $this->error->raiseError($this->lng->txt('msg_no_perm_perm'), $this->error->MESSAGE);
253  return;
254  }
255  $this->tabs_gui->activateTab('perm');
256 
257  $this->tpl->addBlockFile(
258  'ADM_CONTENT',
259  'adm_content',
260  'tpl.rbac_template_permissions.html',
261  'components/ILIAS/AccessControl'
262  );
263 
264  $this->tpl->setVariable('PERM_ACTION', $this->ctrl->getFormAction($this));
265 
266  $acc = new ilAccordionGUI();
267  $acc->setBehaviour(ilAccordionGUI::FORCE_ALL_OPEN);
268  $acc->setId('template_perm_' . $this->ref_id);
269 
270  $subs = ilObjRole::getSubObjects('root', false);
271 
272  foreach ($subs as $subtype => $def) {
274  $this,
275  'perm',
276  $this->ref_id,
277  $this->obj_id,
278  $subtype,
279  false
280  );
281  $tbl->setShowChangeExistingObjects(false);
282  $tbl->parse();
283 
284  $acc->addItem($def['translation'], $tbl->getHTML());
285  }
286 
287  $this->tpl->setVariable('ACCORDION', $acc->getHTML());
288 
289  // Add options table
291  $this,
292  'perm',
293  $this->ref_id,
294  $this->obj_id,
295  false
296  );
297  $options->setShowOptions(false);
298  $options->addMultiCommand(
299  'permSave',
300  $this->lng->txt('save')
301  );
302 
303  $options->parse();
304  $this->tpl->setVariable('OPTIONS_TABLE', $options->getHTML());
305  }
306 
310  protected function permSaveObject(): void
311  {
312  if (!$this->rbac_system->checkAccess('write', $this->rolf_ref_id)) {
313  $this->error->raiseError($this->lng->txt('msg_no_perm_perm'), $this->error->MESSAGE);
314  return;
315  }
316 
317  $template_permissions = [];
318  if ($this->http->wrapper()->post()->has('template_perm')) {
319  $custom_transformer = $this->refinery->custom()->transformation(
320  function ($array) {
321  return $array;
322  }
323  );
324  $template_permissions = $this->http->wrapper()->post()->retrieve(
325  'template_perm',
326  $custom_transformer
327  );
328  }
329 
330  $subs = ilObjRole::getSubObjects('root', false);
331  foreach (array_keys($subs) as $subtype) {
332  // Delete per object type
333  $this->rbac_admin->deleteRolePermission($this->object->getId(), $this->ref_id, $subtype);
334  }
335 
336  foreach ($template_permissions as $key => $ops_array) {
337  $this->rbac_admin->setRolePermission($this->object->getId(), $key, $ops_array, $this->rolf_ref_id);
338  }
339 
340  // update object data entry (to update last modification date)
341  $this->object->update();
342 
343  $this->tpl->setOnScreenMessage('success', $this->lng->txt("saved_successfully"), true);
344  $this->ctrl->redirect($this, "perm");
345  }
346 
347  public function adoptPermSaveObject(): void
348  {
349  $source = 0;
350  if ($this->http->wrapper()->post()->has('adopt')) {
351  $source = $this->http->wrapper()->post()->retrieve(
352  'adopt',
353  $this->refinery->kindlyTo()->int()
354  );
355  }
356 
357  if (!$this->rbac_system->checkAccess('write', $this->rolf_ref_id)) {
358  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('msg_no_perm_perm'), true);
359  } elseif ($this->obj_id == $source) {
360  $this->tpl->setOnScreenMessage('failure', $this->lng->txt("msg_perm_adopted_from_itself"), true);
361  } else {
362  $this->rbac_admin->deleteRolePermission($this->obj_id, $this->rolf_ref_id);
363  $parentRoles = $this->rbac_review->getParentRoleIds($this->rolf_ref_id, true);
364  $this->rbac_admin->copyRoleTemplatePermissions(
365  $source,
366  $parentRoles[$source]["parent"],
367  $this->rolf_ref_id,
368  $this->obj_id
369  );
370  // update object data entry (to update last modification date)
371  $this->object->update();
372 
373  // send info
374  $title = ilObject::_lookupTitle($source);
375  $this->tpl->setOnScreenMessage('success', $this->lng->txt("msg_perm_adopted_from1") . " '" . $title . "'.<br/>" . $this->lng->txt("msg_perm_adopted_from2"), true);
376  }
377  $this->ctrl->redirect($this, "perm");
378  }
379 
380  public function getAdminTabs(): void
381  {
382  $this->getTabs();
383  }
384 
388  protected function getTabs(): void
389  {
390  $this->tabs_gui->setBackTarget($this->lng->txt('btn_back'), (string) $this->ctrl->getParentReturn($this));
391 
392  if ($this->rbac_system->checkAccess('write', $this->ref_id)) {
393  $this->tabs_gui->addTab(
394  'settings',
395  $this->lng->txt('settings'),
396  $this->ctrl->getLinkTarget($this, 'edit')
397  );
398  }
399  if ($this->rbac_system->checkAccess('edit_permission', $this->ref_id)) {
400  $this->tabs_gui->addTab(
401  'perm',
402  $this->lng->txt('default_perm_settings'),
403  $this->ctrl->getLinkTarget($this, 'perm')
404  );
405  }
406  }
407 
411  protected function addAdminLocatorItems(bool $do_not_add_object = false): void
412  {
413  parent::addAdminLocatorItems(true);
414 
415  $query = $this->http->wrapper()->query();
416 
417  if ($query->has('ref_id')) {
418  $ref_id = $query->retrieve('ref_id', $this->refinery->kindlyTo()->int());
419  $this->locator->addItem(
420  $this->lng->txt('obj_' . ilObject::_lookupType(
422  )),
423  $this->ctrl->getLinkTargetByClass("ilobjrolefoldergui", "view")
424  );
425  }
426 
427  if ($query->has('obj_id')) {
428  $this->locator->addItem(
429  ilObjRole::_getTranslation($this->object->getTitle()),
430  $this->ctrl->getLinkTarget($this, 'perm')
431  );
432  }
433  }
434 } // END class.ilObjRoleTemplateGUI
editObject(?ilPropertyFormGUI $form=null)
Create new object.
addAdminLocatorItems(bool $do_not_add_object=false)
const IL_INST_ID
Definition: constants.php:40
Class ilObjRoleTemplate.
prepareOutput(bool $show_sub_objects=true)
static _lookupObjId(int $ref_id)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static http()
Fetches the global http state from ILIAS.
static _lookupTitle(int $obj_id)
static _getTranslation(string $a_role_title)
Class ilObjectGUI Basic methods of all Output classes.
global $DIC
Definition: shib_login.php:22
Class ilObjForumAdministration.
getRoleTemplateForm(bool $is_role_creation_form=false)
__construct($a_data, int $a_id, bool $a_call_by_reference)
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:61
__construct(Container $dic, ilPlugin $plugin)
Class ilObjRoleTemplateGUI.
Class ilRbacAdmin Core functions for role based access control.
static _lookupType(int $id, bool $reference=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...