ILIAS  Release_4_4_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  echo $auto->getList($_REQUEST['query']);
158  exit();
159  }
160 
161 
166  include_once './Services/User/classes/class.ilLocalUser.php';
167  $this->checkPermission("cat_administrate_users");
168  foreach ($_POST['user_ids'] as $user_id) {
169  if (! in_array($user_id, ilLocalUser::_getAllUserIds($this->obj->getRefId()))) {
170  die('user id not valid');
171  }
172  if (! $tmp_obj =& ilObjectFactory::getInstanceByObjId($user_id, false)) {
173  continue;
174  }
175  $tmp_obj->delete();
176  }
177  ilUtil::sendSuccess($this->lng->txt('deleted_users'));
178  $this->listUser();
179 
180  return true;
181  }
182 
183 
184  function deleteUsersObject() {
185  $this->checkPermission("cat_administrate_users");
186  if (! count($_POST['id'])) {
187  ilUtil::sendFailure($this->lng->txt('no_users_selected'));
188  $this->index();
189 
190  return true;
191  }
192  include_once './Services/Utilities/classes/class.ilConfirmationGUI.php';
193  $confirm = new ilConfirmationGUI();
194  $confirm->setFormAction($this->ctrl->getFormAction($this));
195  $confirm->setHeaderText($this->lng->txt('sure_delete_selected_users'));
196  $confirm->setConfirm($this->lng->txt('delete'), 'performDeleteUsers');
197  $confirm->setCancel($this->lng->txt('cancel'), 'index');
198  foreach ($_POST['id'] as $user) {
199  $name = ilObjUser::_lookupName($user);
200  $confirm->addItem(
201  'user_ids[]',
202  $user,
203  $name['lastname'] . ', ' . $name['firstname'] . ' [' . $name['login'] . ']'
204  );
205  }
206  $this->tpl->setContent($confirm->getHTML());
207  }
208 
209 
210  function assignRoles() {
211  global $rbacreview;
212  if (! $this->ilAccess->checkAccess("cat_administrate_users", "", $_GET["ref_id"])) {
213  ilUtil::sendFailure($this->lng->txt("permission_denied"), true);
214  $this->ctrl->redirect($this, "");
215  }
216  $offset = $_GET["offset"];
217  // init sort_by (unfortunatly sort_by is preset with 'title'
218  if ($_GET["sort_by"] == "title" or empty($_GET["sort_by"])) {
219  $_GET["sort_by"] = "login";
220  }
221  $order = $_GET["sort_by"];
222  $direction = $_GET["sort_order"];
223  include_once './Services/User/classes/class.ilLocalUser.php';
224  if (! isset($_GET['obj_id'])) {
225  ilUtil::sendFailure('no_user_selected');
226  $this->index();
227 
228  return true;
229  }
230  $roles = $this->__getAssignableRoles();
231  $this->tpl->addBlockfile('ADM_CONTENT', 'adm_content', 'tpl.cat_role_assignment.html',
232  "Modules/Category");
233  $ass_roles = $rbacreview->assignedRoles($_GET['obj_id']);
234  $counter = 0;
235  foreach ($roles as $role) {
236  $role_obj =& ilObjectFactory::getInstanceByObjId($role['obj_id']);
237  $disabled = false;
238  $f_result[$counter][] = ilUtil::formCheckbox(in_array($role['obj_id'], $ass_roles) ? 1 : 0,
239  'role_ids[]',
240  $role['obj_id'],
241  $disabled);
242  $f_result[$counter][] = $role_obj->getTitle();
243  $f_result[$counter][] = $role_obj->getDescription();
244  $f_result[$counter][] = $role['role_type'] == 'global' ?
245  $this->lng->txt('global') :
246  $this->lng->txt('local');
247  unset($role_obj);
248  ++$counter;
249  }
250  $this->__showRolesTable($f_result, "assignRolesObject");
251  }
252 
253 
254  function assignSave() {
255  global $rbacreview, $rbacadmin;
256  if (! $this->ilAccess->checkAccess("cat_administrate_users", "", $_GET["ref_id"])) {
257  ilUtil::sendFailure($this->lng->txt("permission_denied"), true);
258  $this->ctrl->redirect($this, "");
259  }
260  include_once './Services/User/classes/class.ilLocalUser.php';
261  // check hack
262  if (! isset($_GET['obj_id']) or ! in_array($_REQUEST['obj_id'], ilLocalUser::_getAllUserIds())) {
263  ilUtil::sendFailure('no_user_selected');
264  $this->index();
265 
266  return true;
267  }
268  $roles = $this->__getAssignableRoles();
269  // check minimum one global role
270  if (! $this->__checkGlobalRoles($_POST['role_ids'])) {
271  ilUtil::sendFailure($this->lng->txt('no_global_role_left'));
272  $this->assignRolesObject();
273 
274  return false;
275  }
276  $new_role_ids = $_POST['role_ids'] ? $_POST['role_ids'] : array();
277  $assigned_roles = $rbacreview->assignedRoles((int)$_REQUEST['obj_id']);
278  foreach ($roles as $role) {
279  if (in_array($role['obj_id'], $new_role_ids) and ! in_array($role['obj_id'], $assigned_roles)) {
280  $rbacadmin->assignUser($role['obj_id'], (int)$_REQUEST['obj_id']);
281  }
282  if (in_array($role['obj_id'], $assigned_roles) and ! in_array($role['obj_id'], $new_role_ids)) {
283  $rbacadmin->deassignUser($role['obj_id'], (int)$_REQUEST['obj_id']);
284  }
285  }
286  ilUtil::sendSuccess($this->lng->txt('role_assignment_updated'));
287  $this->assignRoles();
288 
289  return true;
290  }
291 
292 
293  function __checkGlobalRoles($new_assigned) {
294  global $rbacreview, $ilUser;
295  if (! $this->ilAccess->checkAccess("cat_administrate_users", "", $_GET["ref_id"])) {
296  ilUtil::sendFailure($this->lng->txt("permission_denied"), true);
297  $this->ctrl->redirect($this, "");
298  }
299  // return true if it's not a local user
300  $tmp_obj =& ilObjectFactory::getInstanceByObjId($_REQUEST['obj_id']);
301  if ($tmp_obj->getTimeLimitOwner() != $this->object->getRefId() and
302  ! in_array(SYSTEM_ROLE_ID, $rbacreview->assignedRoles($ilUser->getId()))
303  ) {
304  return true;
305  }
306  // new assignment by form
307  $new_assigned = $new_assigned ? $new_assigned : array();
308  $assigned = $rbacreview->assignedRoles((int)$_GET['obj_id']);
309  // all assignable globals
310  if (! in_array(SYSTEM_ROLE_ID, $rbacreview->assignedRoles($ilUser->getId()))) {
311  $ga = $rbacreview->getGlobalAssignableRoles();
312  } else {
313  $ga = $rbacreview->getGlobalRolesArray();
314  }
315  $global_assignable = array();
316  foreach ($ga as $role) {
317  $global_assignable[] = $role['obj_id'];
318  }
319  $new_visible_assigned_roles = array_intersect($new_assigned, $global_assignable);
320  $all_assigned_roles = array_intersect($assigned, $rbacreview->getGlobalRoles());
321  $main_assigned_roles = array_diff($all_assigned_roles, $global_assignable);
322  if (! count($new_visible_assigned_roles) and ! count($main_assigned_roles)) {
323  return false;
324  }
325 
326  return true;
327  }
328 
329 
330  function __getAssignableRoles() {
331  global $rbacreview, $ilUser;
332  // check local user
333  $tmp_obj =& ilObjectFactory::getInstanceByObjId($_REQUEST['obj_id']);
334  // Admin => all roles
335  if (in_array(SYSTEM_ROLE_ID, $rbacreview->assignedRoles($ilUser->getId()))) {
336  $global_roles = $rbacreview->getGlobalRolesArray();
337  } elseif ($tmp_obj->getTimeLimitOwner() == $this->object->getRefId()) {
338  $global_roles = $rbacreview->getGlobalAssignableRoles();
339  } else {
340  $global_roles = array();
341  }
342 
343  return $roles = array_merge($global_roles, $rbacreview->getAssignableChildRoles($this->object->getRefId()));
344  }
345 
346 
347  function __showRolesTable($a_result_set, $a_from = "") {
348  if (! $this->ilAccess->checkAccess("cat_administrate_users", "", $_GET["ref_id"])) {
349  ilUtil::sendFailure($this->lng->txt("permission_denied"), true);
350  $this->ctrl->redirect($this, "");
351  }
352  $tbl =& $this->parent_gui->__initTableGUI();
353  $tpl =& $tbl->getTemplateObject();
354  // SET FORMAACTION
355  $tpl->setCurrentBlock("tbl_form_header");
356  $this->ctrl->setParameter($this, 'obj_id', $_GET['obj_id']);
357  $tpl->setVariable("FORMACTION", $this->ctrl->getFormAction($this));
358  $tpl->parseCurrentBlock();
359  // SET FOOTER BUTTONS
360  $tpl->setVariable("COLUMN_COUNTS", 4);
361  $tpl->setVariable("IMG_ARROW", ilUtil::getImagePath("arrow_downright.png"));
362  $tpl->setCurrentBlock("tbl_action_button");
363  $tpl->setVariable("BTN_NAME", "assignSave");
364  $tpl->setVariable("BTN_VALUE", $this->lng->txt("change_assignment"));
365  $tpl->parseCurrentBlock();
366  $tpl->setCurrentBlock("tbl_action_row");
367  $tpl->setVariable("TPLPATH", $this->tpl->tplPath);
368  $tpl->parseCurrentBlock();
369  $tmp_obj =& ilObjectFactory::getInstanceByObjId($_GET['obj_id']);
370  $title = $this->lng->txt('role_assignment') . ' (' . $tmp_obj->getFullname() . ')';
371  $tbl->setTitle($title, "icon_role.png", $this->lng->txt("role_assignment"));
372  $tbl->setHeaderNames(array(
373  '',
374  $this->lng->txt("title"),
375  $this->lng->txt('description'),
376  $this->lng->txt("type")
377  ));
378  $tbl->setHeaderVars(array(
379  "",
380  "title",
381  "description",
382  "type"
383  ), array(
384  "ref_id" => $this->object->getRefId(),
385  "cmd" => "assignRoles",
386  "obj_id" => $_GET['obj_id'],
387  "cmdClass" => "ilobjcategorygui",
388  "cmdNode" => $_GET["cmdNode"]
389  ));
390  $tbl->setColumnWidth(array( "4%", "35%", "45%", "16%" ));
391  $this->set_unlimited = true;
392  $this->parent_gui->__setTableGUIBasicData($tbl, $a_result_set, $a_from, true);
393  $tbl->render();
394  $this->tpl->setVariable("ROLES_TABLE", $tbl->tpl->get());
395 
396  return true;
397  }
398 }
399