Guest User

Untitled

a guest
Jan 23rd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. /**
  2. * Implements hook_permission().
  3. */
  4. function example_permission() {
  5. return array(
  6. 'edit own department users' => array(
  7. 'title' => t('Edit own department users'),
  8. 'description' => t('Allowed department manager to edit own department users.'),
  9. ),
  10. );
  11. }
  12.  
  13. /**
  14. * Implements hook_menu_alter().
  15. */
  16. function example_menu_alter(&$items) {
  17. $items['user/%user/edit']['access callback'] = 'example_custom_user_edit_access';
  18. }
  19.  
  20. /**
  21. * Custom access callback for user account editing.
  22. */
  23. function example_custom_user_edit_access($account) {
  24. if (user_access('edit own department users')) {
  25. // We perform our check only for user with edit own department permissions.
  26. global $user;
  27.  
  28. // Load user ( Department manager) via entity api.
  29. $user_wrapper = entity_metadata_wrapper('user', $user);
  30.  
  31. // Get array of managed department terms id.
  32. $user_departments = $user_wrapper->field_user_department_managed->value();
  33.  
  34. // Load user (Editor) via entity api.
  35. $account_wrapper = entity_metadata_wrapper('user', $account);
  36. // Get array of department belongs to terms id.
  37. $account_departments = $account_wrapper->field_user_department_belongs->value();
  38.  
  39. // Check any common term id.
  40. $common_departments = array_intersect($user_departments, $account_departments);
  41.  
  42. return !empty($common_departments);
  43. }
  44. // For other user we perform default check.
  45. return user_edit_access($account);
  46. }
Add Comment
Please, Sign In to add comment