Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Implements hook_permission().
- */
- function example_permission() {
- return array(
- 'edit own department users' => array(
- 'title' => t('Edit own department users'),
- 'description' => t('Allowed department manager to edit own department users.'),
- ),
- );
- }
- /**
- * Implements hook_menu_alter().
- */
- function example_menu_alter(&$items) {
- $items['user/%user/edit']['access callback'] = 'example_custom_user_edit_access';
- }
- /**
- * Custom access callback for user account editing.
- */
- function example_custom_user_edit_access($account) {
- if (user_access('edit own department users')) {
- // We perform our check only for user with edit own department permissions.
- global $user;
- // Load user ( Department manager) via entity api.
- $user_wrapper = entity_metadata_wrapper('user', $user);
- // Get array of managed department terms id.
- $user_departments = $user_wrapper->field_user_department_managed->value();
- // Load user (Editor) via entity api.
- $account_wrapper = entity_metadata_wrapper('user', $account);
- // Get array of department belongs to terms id.
- $account_departments = $account_wrapper->field_user_department_belongs->value();
- // Check any common term id.
- $common_departments = array_intersect($user_departments, $account_departments);
- return !empty($common_departments);
- }
- // For other user we perform default check.
- return user_edit_access($account);
- }
Add Comment
Please, Sign In to add comment