Open Journal Systems  3.0.0
 All Classes Namespaces Functions Variables Groups Pages
UserGridHandler.inc.php
1 <?php
2 
16 import('lib.pkp.classes.controllers.grid.GridHandler');
17 import('lib.pkp.classes.controllers.grid.DataObjectGridCellProvider');
18 
19 import('lib.pkp.controllers.grid.settings.user.UserGridRow');
20 import('lib.pkp.controllers.grid.settings.user.form.UserDetailsForm');
21 
25 
29  function UserGridHandler() {
30  parent::GridHandler();
31  $this->addRoleAssignment(array(
32  ROLE_ID_MANAGER),
33  array('fetchGrid', 'fetchRow', 'editUser', 'updateUser', 'updateUserRoles',
34  'editDisableUser', 'disableUser', 'removeUser', 'addUser',
35  'editEmail', 'sendEmail', 'suggestUsername')
36  );
37 
38  $this->addRoleAssignment(array(ROLE_ID_SITE_ADMIN), array('mergeUsers'));
39  }
40 
41 
42  //
43  // Implement template methods from PKPHandler.
44  //
48  function authorize($request, &$args, $roleAssignments) {
49  import('lib.pkp.classes.security.authorization.PkpContextAccessPolicy');
50  $this->addPolicy(new PkpContextAccessPolicy($request, $roleAssignments));
51  return parent::authorize($request, $args, $roleAssignments);
52  }
53 
57  function initialize($request) {
58  parent::initialize($request);
59 
60  // Load user-related translations.
62  LOCALE_COMPONENT_PKP_USER,
63  LOCALE_COMPONENT_PKP_MANAGER,
64  LOCALE_COMPONENT_APP_MANAGER
65  );
66 
67  $this->_oldUserId = (int) $request->getUserVar('oldUserId');
68  // Basic grid configuration.
69  $this->setTitle('grid.user.currentUsers');
70 
71  // Grid actions.
72  $router = $request->getRouter();
73 
74  import('lib.pkp.classes.linkAction.request.AjaxModal');
75  $this->addAction(
76  new LinkAction(
77  'addUser',
78  new AjaxModal(
79  $router->url($request, null, null, 'addUser', null, null),
80  __('grid.user.add'),
81  'modal_add_user',
82  true
83  ),
84  __('grid.user.add'),
85  'add_user')
86  );
87 
88  //
89  // Grid columns.
90  //
91 
92  // First Name.
93  $cellProvider = new DataObjectGridCellProvider();
94  $this->addColumn(
95  new GridColumn(
96  'firstName',
97  'user.firstName',
98  null,
99  'controllers/grid/gridCell.tpl',
100  $cellProvider
101  )
102  );
103 
104  // Last Name.
105  $cellProvider = new DataObjectGridCellProvider();
106  $this->addColumn(
107  new GridColumn(
108  'lastName',
109  'user.lastName',
110  null,
111  'controllers/grid/gridCell.tpl',
112  $cellProvider
113  )
114  );
115 
116  // User name.
117  $cellProvider = new DataObjectGridCellProvider();
118  $this->addColumn(
119  new GridColumn(
120  'username',
121  'user.username',
122  null,
123  'controllers/grid/gridCell.tpl',
124  $cellProvider
125  )
126  );
127 
128  // Email.
129  $cellProvider = new DataObjectGridCellProvider();
130  $this->addColumn(
131  new GridColumn(
132  'email',
133  'user.email',
134  null,
135  'controllers/grid/gridCell.tpl',
136  $cellProvider
137  )
138  );
139  }
140 
141 
142  //
143  // Implement methods from GridHandler.
144  //
149  function getRowInstance() {
150  return new UserGridRow($this->_oldUserId);
151  }
152 
156  function initFeatures($request, $args) {
157  import('lib.pkp.classes.controllers.grid.feature.PagingFeature');
158  return array(new PagingFeature());
159  }
160 
166  function loadData($request, $filter) {
167  // Get the context.
168  $context = $request->getContext();
169 
170  // Get all users for this context that match search criteria.
171  $userGroupDao = DAORegistry::getDAO('UserGroupDAO');
172  $rangeInfo = $this->getGridRangeInfo($request, $this->getId());
173 
174  return $users = $userGroupDao->getUsersById(
175  $filter['userGroup'],
176  $filter['includeNoRole']?null:$context->getId(),
177  $filter['searchField'],
178  $filter['search']?$filter['search']:null,
179  $filter['searchMatch'],
180  $rangeInfo
181  );
182  }
183 
187  function renderFilter($request) {
188  $context = $request->getContext();
189  $userGroupDao = DAORegistry::getDAO('UserGroupDAO');
190  $userGroups = $userGroupDao->getByContextId($context->getId());
191  $userGroupOptions = array('' => __('grid.user.allRoles'));
192  while ($userGroup = $userGroups->next()) {
193  $userGroupOptions[$userGroup->getId()] = $userGroup->getLocalizedName();
194  }
195 
196  // Import PKPUserDAO to define the USER_FIELD_* constants.
197  import('lib.pkp.classes.user.PKPUserDAO');
198  $fieldOptions = array(
199  USER_FIELD_FIRSTNAME => 'user.firstName',
200  USER_FIELD_LASTNAME => 'user.lastName',
201  USER_FIELD_USERNAME => 'user.username',
202  USER_FIELD_EMAIL => 'user.email'
203  );
204 
205  $matchOptions = array(
206  'contains' => 'form.contains',
207  'is' => 'form.is'
208  );
209 
210  $filterData = array(
211  'userGroupOptions' => $userGroupOptions,
212  'fieldOptions' => $fieldOptions,
213  'matchOptions' => $matchOptions
214  );
215 
216  return parent::renderFilter($request, $filterData);
217  }
218 
223  function getFilterSelectionData($request) {
224  // Get the search terms.
225  $includeNoRole = $request->getUserVar('includeNoRole') ? (int) $request->getUserVar('includeNoRole') : null;
226  $userGroup = $request->getUserVar('userGroup') ? (int)$request->getUserVar('userGroup') : null;
227  $searchField = $request->getUserVar('searchField');
228  $searchMatch = $request->getUserVar('searchMatch');
229  $search = $request->getUserVar('search');
230 
231  return $filterSelectionData = array(
232  'includeNoRole' => $includeNoRole,
233  'userGroup' => $userGroup,
234  'searchField' => $searchField,
235  'searchMatch' => $searchMatch,
236  'search' => $search ? $search : ''
237  );
238  }
239 
244  function getFilterForm() {
245  return 'controllers/grid/settings/user/userGridFilter.tpl';
246  }
247 
248 
249  //
250  // Public grid actions.
251  //
257  function suggestUsername($args, $request) {
258  $suggestion = Validation::suggestUsername(
259  $request->getUserVar('firstName'),
260  $request->getUserVar('lastName')
261  );
262 
263  $json = new JSONMessage(true, $suggestion);
264  return $json->getString();
265  }
266 
272  function addUser($args, $request) {
273  // Calling editUser with an empty row id will add a new user.
274  return $this->editUser($args, $request);
275  }
276 
283  function editUser($args, $request) {
284  // Identify the user Id.
285  $userId = $request->getUserVar('rowId');
286  if (!$userId) $userId = $request->getUserVar('userId');
287 
288  $user = $request->getUser();
289  if ($userId !== null && !Validation::canAdminister($userId, $user->getId())) {
290  // We don't have administrative rights over this user.
291  $json = new JSONMessage(false, __('grid.user.cannotAdminister'));
292  } else {
293  // Form handling.
294  $userForm = new UserDetailsForm($request, $userId);
295  $userForm->initData($args, $request);
296 
297  $json = new JSONMessage(true, $userForm->display($args, $request));
298  }
299  return $json->getString();
300  }
301 
308  function updateUser($args, $request) {
309  $user = $request->getUser();
310 
311  // Identify the user Id.
312  $userId = $request->getUserVar('userId');
313 
314  if ($userId !== null && !Validation::canAdminister($userId, $user->getId())) {
315  // We don't have administrative rights over this user.
316  $json = new JSONMessage(false, __('grid.user.cannotAdminister'));
317  } else {
318  // Form handling.
319  $userForm = new UserDetailsForm($request, $userId);
320  $userForm->readInputData();
321 
322  if ($userForm->validate()) {
323  $user = $userForm->execute($args, $request);
324 
325  // If this is a newly created user, show role management form.
326  if (!$userId) {
327  import('lib.pkp.controllers.grid.settings.user.form.UserRoleForm');
328  $userRoleForm = new UserRoleForm($user->getId(), $user->getFullName());
329  $userRoleForm->initData($args, $request);
330  $json = new JSONMessage(true, $userRoleForm->display($args, $request));
331  } else {
332 
333  // Successful edit of an existing user.
334  $notificationManager = new NotificationManager();
335  $user = $request->getUser();
336  $notificationManager->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_SUCCESS, array('contents' => __('notification.editedUser')));
337 
338  // Prepare the grid row data.
339  return DAO::getDataChangedEvent($userId);
340  }
341  } else {
342  $json = new JSONMessage(false);
343  }
344  }
345  return $json->getString();
346  }
347 
354  function updateUserRoles($args, $request) {
355  $user = $request->getUser();
356 
357  // Identify the user Id.
358  $userId = $request->getUserVar('userId');
359 
360  if ($userId !== null && !Validation::canAdminister($userId, $user->getId())) {
361  // We don't have administrative rights over this user.
362  $json = new JSONMessage(false, __('grid.user.cannotAdminister'));
363  } else {
364  // Form handling.
365  import('lib.pkp.controllers.grid.settings.user.form.UserRoleForm');
366  $userRoleForm = new UserRoleForm($userId, $user->getFullName());
367  $userRoleForm->readInputData();
368 
369  if ($userRoleForm->validate()) {
370  $userRoleForm->execute($args, $request);
371 
372  // Successfully managed newly created user's roles.
373  return DAO::getDataChangedEvent($userId);
374  } else {
375  $json = new JSONMessage(false);
376  }
377  }
378  return $json->getString();
379  }
380 
387  function editDisableUser($args, $request) {
388  $user = $request->getUser();
389 
390  // Identify the user Id.
391  $userId = $request->getUserVar('rowId');
392  if (!$userId) $userId = $request->getUserVar('userId');
393 
394  // Are we enabling or disabling this user.
395  $enable = isset($args['enable']) ? (bool) $args['enable'] : false;
396 
397  if ($userId !== null && !Validation::canAdminister($userId, $user->getId())) {
398  // We don't have administrative rights over this user.
399  $json = new JSONMessage(false, __('grid.user.cannotAdminister'));
400  } else {
401  // Form handling
402  import('lib.pkp.controllers.grid.settings.user.form.UserDisableForm');
403  $userForm = new UserDisableForm($userId, $enable);
404 
405  $userForm->initData($args, $request);
406 
407  $json = new JSONMessage(true, $userForm->display($args, $request));
408  }
409  return $json->getString();
410  }
411 
418  function disableUser($args, $request) {
419  $user = $request->getUser();
420 
421  // Identify the user Id.
422  $userId = $request->getUserVar('userId');
423 
424  // Are we enabling or disabling this user.
425  $enable = (bool) $request->getUserVar('enable');
426 
427  if ($userId !== null && !Validation::canAdminister($userId, $user->getId())) {
428  // We don't have administrative rights over this user.
429  $json = new JSONMessage(false, __('grid.user.cannotAdminister'));
430  } else {
431  // Form handling.
432  import('lib.pkp.controllers.grid.settings.user.form.UserDisableForm');
433  $userForm = new UserDisableForm($userId, $enable);
434 
435  $userForm->readInputData();
436 
437  if ($userForm->validate()) {
438  $user = $userForm->execute($args, $request);
439 
440  // Successful enable/disable of an existing user.
441  // Update grid data.
442  return DAO::getDataChangedEvent($userId);
443 
444  } else {
445  $json = new JSONMessage(false, $userForm->display($args, $request));
446  }
447  }
448  return $json->getString();
449  }
450 
457  function removeUser($args, $request) {
458  $context = $request->getContext();
459  $user = $request->getUser();
460 
461  // Identify the user Id.
462  $userId = $request->getUserVar('rowId');
463 
464  if ($userId !== null && !Validation::canAdminister($userId, $user->getId())) {
465  // We don't have administrative rights over this user.
466  $json = new JSONMessage(false, __('grid.user.cannotAdminister'));
467  } else {
468  // Remove user from all user group assignments for this context.
469  $userGroupDao = DAORegistry::getDAO('UserGroupDAO');
470 
471  // Check if this user has any user group assignments for this context.
472  if (!$userGroupDao->userInAnyGroup($userId, $context->getId())) {
473  $json = new JSONMessage(false, __('grid.user.userNoRoles'));
474  } else {
475  $userGroupDao->deleteAssignmentsByContextId($context->getId(), $userId);
476  return DAO::getDataChangedEvent($userId);
477  }
478  }
479  return $json->getString();
480  }
481 
488  function editEmail($args, $request) {
489  $user = $request->getUser();
490 
491  // Identify the user Id.
492  $userId = $request->getUserVar('rowId');
493 
494  if ($userId !== null && !Validation::canAdminister($userId, $user->getId())) {
495  // We don't have administrative rights over this user.
496  $json = new JSONMessage(false, __('grid.user.cannotAdminister'));
497  } else {
498  // Form handling.
499  import('lib.pkp.controllers.grid.settings.user.form.UserEmailForm');
500  $userEmailForm = new UserEmailForm($userId);
501  $userEmailForm->initData($args, $request);
502 
503  $json = new JSONMessage(true, $userEmailForm->display($args, $request));
504  }
505  return $json->getString();
506  }
507 
514  function sendEmail($args, $request) {
515  $user = $request->getUser();
516 
517  // Identify the user Id.
518  $userId = $request->getUserVar('userId');
519 
520  if ($userId !== null && !Validation::canAdminister($userId, $user->getId())) {
521  // We don't have administrative rights over this user.
522  $json = new JSONMessage(false, __('grid.user.cannotAdminister'));
523  } else {
524  // Form handling.
525  import('lib.pkp.controllers.grid.settings.user.form.UserEmailForm');
526  $userEmailForm = new UserEmailForm($userId);
527  $userEmailForm->readInputData();
528 
529  if ($userEmailForm->validate()) {
530  $userEmailForm->execute($args, $request);
531  $json = new JSONMessage(true);
532  } else {
533  $json = new JSONMessage(false, $userEmailForm->display($args, $request));
534  }
535  }
536  return $json->getString();
537  }
538 
544  function mergeUsers($args, $request) {
545 
546  // if there is a $newUserId, this is the second time through, so merge the users.
547  $newUserId = (int) $request->getUserVar('newUserId');
548  $oldUserId = (int) $request->getUserVar('oldUserId');
549  if ($newUserId > 0 && $oldUserId > 0) {
550  import('classes.user.UserAction');
551  $userAction = new UserAction();
552  $userAction->mergeUsers($oldUserId, $newUserId);
553  return DAO::getDataChangedEvent();
554  } else {
555  // this shouldn't happen since the first time this action is
556  // selected on the grid there is no call to the handler.
557  $json = new JSONMessage(false, __('grid.user.cannotAdminister'));
558  return $json->getString();
559  }
560  }
561 
565  function getRequestArgs() {
566  $requestArgs = (array) parent::getRequestArgs();
567  $requestArgs['oldUserId'] = $this->_oldUserId;
568  return $requestArgs;
569  }
570 }
571 
572 ?>
static & getDAO($name, $dbconn=null)
addPolicy($authorizationPolicy, $addToTop=false)
Form for enabling/disabling a user.
initFeatures($request, $args)
loadData($request, $filter)
static requireComponents()
authorize($request, &$args, $roleAssignments)
Form for managing roles for a newly created user.
updateUser($args, $request)
addAction($action, $position=GRID_ACTION_POSITION_ABOVE)
addColumn($column)
editDisableUser($args, $request)
Class defining basic operations for handling HTML grids.
initData()
Definition: Form.inc.php:228
User grid row definition.
static canAdminister($administeredUserId, $administratorUserId)
Class to control access to PKP applications&#39; setup components.
addRoleAssignment($roleIds, $operations)
A modal that retrieves its content from via AJAX.
mergeUsers($args, $request)
Class to represent a JSON (Javascript Object Notation) message.
sendEmail($args, $request)
static getDataChangedEvent($elementId=null, $parentElementId=null)
Definition: DAO.inc.php:606
suggestUsername($args, $request)
updateUserRoles($args, $request)
Handle user grid requests.
UserAction class.
static suggestUsername($firstName, $lastName)
Form for editing user profiles.
Add paging functionality to grids.
Base class for a cell provider that can retrieve simple labels from DataObjects. If you need more com...
removeUser($args, $request)
Form for sending an email to a user.
getGridRangeInfo($request, $rangeName, $contextData=null)
getFilterSelectionData($request)
editEmail($args, $request)
editUser($args, $request)
Represents a column within a grid. It is used to configure the way cells within a column are displaye...
disableUser($args, $request)
addUser($args, $request)
Base class defining an action that can be performed by the user in the user interface.