administerusersbyrole.module 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * @file
  4. * Allows users with 'administer users' permission and a role (specified in 'Permissions') to edit/delete other users with a specified role. If the user being edited has multiple roles, the user doing the editing must have permission to edit ALL of the user being edited's roles. Also provides control over user creation. Works well in conjunction with <a href='http://drupal.org/project/role_delegation'>role_delegation</a>.
  5. */
  6. /**
  7. * Implements hook_permission().
  8. */
  9. function administerusersbyrole_permission() {
  10. $roles = db_query('SELECT name, rid FROM {role} WHERE rid > 1 ORDER BY name')->fetchAll();
  11. $perms = array();
  12. $perms['create users'] = array('title' => 'Create new users');
  13. foreach ($roles as &$role) {
  14. $perms['edit users with role '. $role->rid] = array('title' => 'Edit users that have the role '. $role->name);
  15. $perms['delete users with role '. $role->rid] = array('title' => 'Delete users that have the role '. $role->name);
  16. }
  17. return $perms;
  18. }
  19. /**
  20. * Implements hook_menu_alter().
  21. */
  22. function administerusersbyrole_menu_alter(&$items) {
  23. $items['user/%user/edit']['access callback'] = '_administerusersbyrole_can_edit_user';
  24. $items['user/%user/cancel']['access callback'] = '_administerusersbyrole_can_delete_user';
  25. $items['admin/people/create']['access arguments'] = array('create users');
  26. }
  27. /**
  28. * Custom access callback for edit user.
  29. */
  30. function _administerusersbyrole_can_edit_user($account) {
  31. global $user;
  32. if($account->uid == $user->uid)
  33. return TRUE;
  34. if($account->uid == 1)
  35. return FALSE;
  36. elseif(user_access('edit users with role '.DRUPAL_AUTHENTICATED_RID))
  37. return TRUE;
  38. foreach ($account->roles as $rid => $role) {
  39. if($rid == DRUPAL_AUTHENTICATED_RID)
  40. continue;
  41. if(!user_access('edit users with role '. $rid))
  42. return FALSE;
  43. }
  44. return TRUE;
  45. }
  46. /**
  47. * Custom access callback for delete user.
  48. */
  49. function _administerusersbyrole_can_delete_user($account) {
  50. if ($account->uid == 1) {
  51. return FALSE;
  52. }
  53. $permitted = TRUE;
  54. foreach ($account->roles as $rid => $role) {
  55. $permitted = user_access('delete users with role '. $rid);
  56. if (!$permitted) {
  57. return FALSE;
  58. }
  59. }
  60. return TRUE;
  61. }
  62. /**
  63. * Implements hook_form_user_admin_account_alter().
  64. */
  65. function administerusersbyrole_form_user_admin_account_alter(&$form, &$form_state, $form_id) {
  66. // Remove edit links if i don't have access to them.
  67. foreach ($form['accounts']['#options'] as $uid => $fields) {
  68. $account = user_load($uid);
  69. if (!_administerusersbyrole_can_edit_user($account)) {
  70. $form['accounts']['#options'][$uid]['operations'] = '';
  71. }
  72. }
  73. }
  74. /**
  75. * Implements hook_form_user_profile_form_alter().
  76. */
  77. function administerusersbyrole_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
  78. // Hide delete link if i don't have access.
  79. $account = $form['#user'];
  80. if (!_administerusersbyrole_can_delete_user($account)) {
  81. $form['actions']['cancel']['#access'] = FALSE;
  82. }
  83. }