ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilLocalUserGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 require_once("./Services/User/classes/class.ilUserTableGUI.php");
4 require_once("./Services/User/classes/class.ilLocalUser.php");
5 require_once("./Services/User/classes/class.ilObjUserGUI.php");
6 require_once("./Services/User/classes/class.ilObjUserFolderGUI.php");
14 
18  protected $tabs_gui;
22  protected $form;
26  protected $toolbar;
30  protected $ctrl;
34  protected $tpl;
38  public $object;
42  protected $lng;
46  protected $ilAccess;
47 
48 
52  //TODO MST 14.11.2013 - we should split this class into ilLocalUserTableGUI and ilLocalUserRoleGUI
53  function __construct($parent_gui) {
54  global $tpl, $ilCtrl, $ilTabs, $ilToolbar, $lng, $rbacsystem, $ilAccess;
55  $this->tpl = $tpl;
56  $this->ctrl = $ilCtrl;
57  $this->parent_gui = $parent_gui;
58  $this->object = $parent_gui->object;
59  $this->tabs_gui = $this->parent_gui->tabs_gui;
60  $this->toolbar = $ilToolbar;
61  $this->lng = $lng;
62  $this->ilAccess = $ilAccess;
63  $this->lng->loadLanguageModule('user');
64  if (! $rbacsystem->checkAccess("cat_administrate_users", $this->parent_gui->object->getRefId())) {
65  ilUtil::sendFailure($this->lng->txt("msg_no_perm_admin_users"), true);
66  }
67  }
68 
69 
73  public function executeCommand() {
74  $cmd = $this->ctrl->getCmd();
75  switch ($cmd) {
76  case "assignRoles":
77  case "assignSave":
78  $this->tabs_gui->clearTargets();
79  $this->tabs_gui->setBackTarget($this->lng->txt("back"),$this->ctrl->getLinkTargetByClass("illocalusergui", 'index'));
80  $this->$cmd();
81  break;
82  default:
83  $this->$cmd();
84  break;
85  }
86 
87  return true;
88  }
89 
90 
95  protected function resetFilter() {
96  $table = new ilUserTableGUI($this, "index", ilUserTableGUI::MODE_LOCAL_USER);
97  $table->resetOffset();
98  $table->resetFilter();
99  $this->index();
100  }
101 
102 
108  protected function applyFilter() {
109  $table = new ilUserTableGUI($this, "index", ilUserTableGUI::MODE_LOCAL_USER);
110  $table->resetOffset();
111  $table->writeFilterToSession();
112  $this->index();
113  }
114 
115 
116  function index($show_delete = false) {
117  global $ilUser, $rbacreview, $rbacsystem;
118  $this->tpl->addBlockfile('ADM_CONTENT', 'adm_content', 'tpl.cat_admin_users.html',
119  "Modules/Category");
120  if (count($rbacreview->getGlobalAssignableRoles())
121  or in_array(SYSTEM_ROLE_ID, $rbacreview->assignedRoles($ilUser->getId()))
122  ) {
123  $this->toolbar->addButton(
124  $this->lng->txt('add_user'),
125  $this->ctrl->getLinkTargetByClass('ilobjusergui', 'create')
126  );
127  $this->toolbar->addButton(
128  $this->lng->txt('import_users'),
129  $this->ctrl->getLinkTargetByClass('ilobjuserfoldergui', 'importUserForm')
130  );
131  } else {
132  ilUtil::sendInfo($this->lng->txt('no_roles_user_can_be_assigned_to'));
133  }
134  if ($show_delete) {
135  $this->tpl->setCurrentBlock("confirm_delete");
136  $this->tpl->setVariable("CONFIRM_FORMACTION", $this->ctrl->getFormAction($this));
137  $this->tpl->setVariable("TXT_CANCEL", $this->lng->txt('cancel'));
138  $this->tpl->setVariable("CONFIRM_CMD", 'performDeleteUsers');
139  $this->tpl->setVariable("TXT_CONFIRM", $this->lng->txt('delete'));
140  $this->tpl->parseCurrentBlock();
141  }
142  $table = new ilUserTableGUI($this, 'index', ilUserTableGUI::MODE_LOCAL_USER);
143  $this->tpl->setVariable('USERS_TABLE', $table->getHTML());
144 
145  return true;
146  }
147 
148 
152  protected function addUserAutoCompleteObject() {
153  include_once './Services/User/classes/class.ilUserAutoComplete.php';
154  $auto = new ilUserAutoComplete();
155  $auto->setSearchFields(array( 'login', 'firstname', 'lastname', 'email' ));
156  $auto->enableFieldSearchableCheck(true);
157  $auto->setMoreLinkAvailable(true);
158 
159  if(($_REQUEST['fetchall']))
160  {
161  $auto->setLimit(ilUserAutoComplete::MAX_ENTRIES);
162  }
163 
164  echo $auto->getList($_REQUEST['query']);
165  exit();
166  }
167 
168 
172  function performDeleteUsers() {
173  global $ilLog;
174  include_once './Services/User/classes/class.ilLocalUser.php';
175  $this->checkPermission("cat_administrate_users");
176  foreach ($_POST['user_ids'] as $user_id) {
177  if (! in_array($user_id, ilLocalUser::_getAllUserIds($_GET['ref_id']))) {
178  $ilLog->write(__FILE__.":".__LINE__." User with id $user_id could not be found.");
179  ilUtil::sendFailure($this->lng->txt('user_not_found_to_delete'));
180  }
181  if (! $tmp_obj =& ilObjectFactory::getInstanceByObjId($user_id, false)) {
182  continue;
183  }
184  $tmp_obj->delete();
185  }
186  ilUtil::sendSuccess($this->lng->txt('deleted_users'), true);
187  $this->ctrl->redirect($this, 'index');
188 
189  return true;
190  }
191 
192 
193  function deleteUsers() {
194  $this->checkPermission("cat_administrate_users");
195  if (! count($_POST['id'])) {
196  ilUtil::sendFailure($this->lng->txt('no_users_selected'));
197  $this->index();
198 
199  return true;
200  }
201  include_once './Services/Utilities/classes/class.ilConfirmationGUI.php';
202  $confirm = new ilConfirmationGUI();
203  $confirm->setFormAction($this->ctrl->getFormAction($this));
204  $confirm->setHeaderText($this->lng->txt('sure_delete_selected_users'));
205  $confirm->setConfirm($this->lng->txt('delete'), 'performDeleteUsers');
206  $confirm->setCancel($this->lng->txt('cancel'), 'index');
207  foreach ($_POST['id'] as $user) {
208  $name = ilObjUser::_lookupName($user);
209  $confirm->addItem(
210  'user_ids[]',
211  $user,
212  $name['lastname'] . ', ' . $name['firstname'] . ' [' . $name['login'] . ']'
213  );
214  }
215  $this->tpl->setContent($confirm->getHTML());
216  }
217 
218 
219  function assignRoles() {
220  global $rbacreview;
221  if (! $this->ilAccess->checkAccess("cat_administrate_users", "", $_GET["ref_id"])) {
222  ilUtil::sendFailure($this->lng->txt("permission_denied"), true);
223  $this->ctrl->redirect($this, "");
224  }
225  $offset = $_GET["offset"];
226  // init sort_by (unfortunatly sort_by is preset with 'title'
227  if ($_GET["sort_by"] == "title" or empty($_GET["sort_by"])) {
228  $_GET["sort_by"] = "login";
229  }
230  $order = $_GET["sort_by"];
231  $direction = $_GET["sort_order"];
232  include_once './Services/User/classes/class.ilLocalUser.php';
233  if (! isset($_GET['obj_id'])) {
234  ilUtil::sendFailure('no_user_selected');
235  $this->index();
236 
237  return true;
238  }
239  $roles = $this->__getAssignableRoles();
240  $this->tpl->addBlockfile('ADM_CONTENT', 'adm_content', 'tpl.cat_role_assignment.html',
241  "Modules/Category");
242  $ass_roles = $rbacreview->assignedRoles($_GET['obj_id']);
243  $counter = 0;
244  foreach ($roles as $role) {
245  $role_obj =& ilObjectFactory::getInstanceByObjId($role['obj_id']);
246  $disabled = false;
247  $f_result[$counter][] = ilUtil::formCheckbox(in_array($role['obj_id'], $ass_roles) ? 1 : 0,
248  'role_ids[]',
249  $role['obj_id'],
250  $disabled);
251  $f_result[$counter][] = $role_obj->getTitle();
252  $f_result[$counter][] = $role_obj->getDescription();
253  $f_result[$counter][] = $role['role_type'] == 'global' ?
254  $this->lng->txt('global') :
255  $this->lng->txt('local');
256  unset($role_obj);
257  ++$counter;
258  }
259  $this->__showRolesTable($f_result, "assignRolesObject");
260  }
261 
262 
263  function assignSave() {
264  global $rbacreview, $rbacadmin;
265  if (! $this->ilAccess->checkAccess("cat_administrate_users", "", $_GET["ref_id"])) {
266  ilUtil::sendFailure($this->lng->txt("permission_denied"), true);
267  $this->ctrl->redirect($this, "");
268  }
269  include_once './Services/User/classes/class.ilLocalUser.php';
270  // check hack
271  if (! isset($_GET['obj_id']) or ! in_array($_REQUEST['obj_id'], ilLocalUser::_getAllUserIds())) {
272  ilUtil::sendFailure('no_user_selected');
273  $this->index();
274 
275  return true;
276  }
277  $roles = $this->__getAssignableRoles();
278  // check minimum one global role
279  if (! $this->__checkGlobalRoles($_POST['role_ids'])) {
280  ilUtil::sendFailure($this->lng->txt('no_global_role_left'));
281  $this->assignRolesObject();
282 
283  return false;
284  }
285  $new_role_ids = $_POST['role_ids'] ? $_POST['role_ids'] : array();
286  $assigned_roles = $rbacreview->assignedRoles((int)$_REQUEST['obj_id']);
287  foreach ($roles as $role) {
288  if (in_array($role['obj_id'], $new_role_ids) and ! in_array($role['obj_id'], $assigned_roles)) {
289  $rbacadmin->assignUser($role['obj_id'], (int)$_REQUEST['obj_id']);
290  }
291  if (in_array($role['obj_id'], $assigned_roles) and ! in_array($role['obj_id'], $new_role_ids)) {
292  $rbacadmin->deassignUser($role['obj_id'], (int)$_REQUEST['obj_id']);
293  }
294  }
295  ilUtil::sendSuccess($this->lng->txt('role_assignment_updated'));
296  $this->assignRoles();
297 
298  return true;
299  }
300 
301 
302  function __checkGlobalRoles($new_assigned) {
303  global $rbacreview, $ilUser;
304  if (! $this->ilAccess->checkAccess("cat_administrate_users", "", $_GET["ref_id"])) {
305  ilUtil::sendFailure($this->lng->txt("permission_denied"), true);
306  $this->ctrl->redirect($this, "");
307  }
308  // return true if it's not a local user
309  $tmp_obj =& ilObjectFactory::getInstanceByObjId($_REQUEST['obj_id']);
310  if ($tmp_obj->getTimeLimitOwner() != $this->object->getRefId() and
311  ! in_array(SYSTEM_ROLE_ID, $rbacreview->assignedRoles($ilUser->getId()))
312  ) {
313  return true;
314  }
315  // new assignment by form
316  $new_assigned = $new_assigned ? $new_assigned : array();
317  $assigned = $rbacreview->assignedRoles((int)$_GET['obj_id']);
318  // all assignable globals
319  if (! in_array(SYSTEM_ROLE_ID, $rbacreview->assignedRoles($ilUser->getId()))) {
320  $ga = $rbacreview->getGlobalAssignableRoles();
321  } else {
322  $ga = $rbacreview->getGlobalRolesArray();
323  }
324  $global_assignable = array();
325  foreach ($ga as $role) {
326  $global_assignable[] = $role['obj_id'];
327  }
328  $new_visible_assigned_roles = array_intersect($new_assigned, $global_assignable);
329  $all_assigned_roles = array_intersect($assigned, $rbacreview->getGlobalRoles());
330  $main_assigned_roles = array_diff($all_assigned_roles, $global_assignable);
331  if (! count($new_visible_assigned_roles) and ! count($main_assigned_roles)) {
332  return false;
333  }
334 
335  return true;
336  }
337 
338 
339  function __getAssignableRoles() {
340  global $rbacreview, $ilUser;
341  // check local user
342  $tmp_obj =& ilObjectFactory::getInstanceByObjId($_REQUEST['obj_id']);
343  // Admin => all roles
344  if (in_array(SYSTEM_ROLE_ID, $rbacreview->assignedRoles($ilUser->getId()))) {
345  $global_roles = $rbacreview->getGlobalRolesArray();
346  } elseif ($tmp_obj->getTimeLimitOwner() == $this->object->getRefId()) {
347  $global_roles = $rbacreview->getGlobalAssignableRoles();
348  } else {
349  $global_roles = array();
350  }
351 
352  return $roles = array_merge($global_roles, $rbacreview->getAssignableChildRoles($this->object->getRefId()));
353  }
354 
355 
356  function __showRolesTable($a_result_set, $a_from = "") {
357  if (! $this->ilAccess->checkAccess("cat_administrate_users", "", $_GET["ref_id"])) {
358  ilUtil::sendFailure($this->lng->txt("permission_denied"), true);
359  $this->ctrl->redirect($this, "");
360  }
361  $tbl =& $this->parent_gui->__initTableGUI();
362  $tpl =& $tbl->getTemplateObject();
363  // SET FORMAACTION
364  $tpl->setCurrentBlock("tbl_form_header");
365  $this->ctrl->setParameter($this, 'obj_id', $_GET['obj_id']);
366  $tpl->setVariable("FORMACTION", $this->ctrl->getFormAction($this));
367  $tpl->parseCurrentBlock();
368  // SET FOOTER BUTTONS
369  $tpl->setVariable("COLUMN_COUNTS", 4);
370  $tpl->setVariable("IMG_ARROW", ilUtil::getImagePath("arrow_downright.svg"));
371  $tpl->setCurrentBlock("tbl_action_button");
372  $tpl->setVariable("BTN_NAME", "assignSave");
373  $tpl->setVariable("BTN_VALUE", $this->lng->txt("change_assignment"));
374  $tpl->parseCurrentBlock();
375  $tpl->setCurrentBlock("tbl_action_row");
376  $tpl->setVariable("TPLPATH", $this->tpl->tplPath);
377  $tpl->parseCurrentBlock();
378  $tmp_obj =& ilObjectFactory::getInstanceByObjId($_GET['obj_id']);
379  $title = $this->lng->txt('role_assignment') . ' (' . $tmp_obj->getFullname() . ')';
380  $tbl->setTitle($title, "icon_role.svg", $this->lng->txt("role_assignment"));
381  $tbl->setHeaderNames(array(
382  '',
383  $this->lng->txt("title"),
384  $this->lng->txt('description'),
385  $this->lng->txt("type")
386  ));
387  $tbl->setHeaderVars(array(
388  "",
389  "title",
390  "description",
391  "type"
392  ), array(
393  "ref_id" => $this->object->getRefId(),
394  "cmd" => "assignRoles",
395  "obj_id" => $_GET['obj_id'],
396  "cmdClass" => "ilobjcategorygui",
397  "cmdNode" => $_GET["cmdNode"]
398  ));
399  $tbl->setColumnWidth(array( "4%", "35%", "45%", "16%" ));
400  $this->set_unlimited = true;
401  $this->parent_gui->__setTableGUIBasicData($tbl, $a_result_set, $a_from, true);
402  $tbl->render();
403  $this->tpl->setVariable("ROLES_TABLE", $tbl->tpl->get());
404 
405  return true;
406  }
407 
411  protected function checkPermission($permission) {
412  if (! $this->ilAccess->checkAccess($permission, "", $_GET["ref_id"])) {
413  ilUtil::sendFailure($this->lng->txt("permission_denied"), true);
414  $this->ctrl->redirect($this, "");
415  }
416  }
417 }
418