You can hide the administrator
role (and its users) for users with not sufficient permissions by doing this:
1) I would suggest you to install the admin views module, in case you haven't done already and override the admin/people
page.
2) Implement the following hooks: hook_form_alter and hook_views_query_alter
/**
* Implements hook_form_alter().
*/
function my_module_form_alter(&$form, &$form_state, $form_id) {
global $user;
// Hide the 'administrator' user role filter option.
if ($form_id == 'views_form_custom_view_name_system_1' && !_allowed_roles($user)) {
$role = user_role_load_by_name('administrator');
unset($form['rid']['#options'][$role->rid]);
}
}
// Return if the logged in user can view the administrator role.
function _allowed_roles($user) {
return in_array('Role 1', $user->roles) || in_array('Role 2', $user->roles);
}
/**
* Implements hook_views_query_alter().
*/
function my_module_views_query_alter(&$view, &$query) {
global $user;
if ($view->name == 'custom_view_name' && !_allowed_roles($user)) {
$role = user_role_load_by_name('administrator');
$join = new views_join();
$join->table = 'users_roles';
$join->field = 'uid';
$join->left_table = 'users';
$join->left_field = 'uid';
$join->type = 'INNER';
$join->adjusted = TRUE;
$query->add_relationship('users_roles', $join, 'uid');
// Hide the 'administrator' role users from the actual users' list.
$query->where[] = create_views_where_expression($field = 'users_roles.rid', $value = $role->rid, $operator = '<>');
}
}
3) Don't forget to add to the (above) users overridden view the User: Roles (exposed)
filter.
So far we have the administrator role hidden from the users list, let's now restrict the access (edit) to the actual accounts as well.
4) Again, we implement some hooks and an access callback function: - hook_permission, it let us to define user permissions (UI) - hook_menu_alter allows us to alter the default callback function and _admin_edit_callback
the custom callback to implement our logic.
/**
* Implements hook_permission().
*/
function my_module_permission() {
return array(
'edit administrator profile' => array(
'title' => t('Edit administrators profiles'),
'description' => t('Perform changes to administrators accounts'),
),
);
}
/**
* Implements hook_menu_alter().
*/
function my_module_menu_alter(&$items) {
$items['user/%user/edit']['access callback'] = '_admin_edit_callback';
$items['user/%user/edit']['access arguments'] = array(0, 1, 'edit');
}
/**
* Access callback for admin user edit operations
* @param $type
* The entity type.
* @param $entity
* The entity object.
* @param $op
* The operation to the entity.
*
* @return bool
*/
function _admin_edit_callback($type = NULL, $entity = NULL, $op = NULL) {
global $user;
$account = user_load($user->uid);
// Selected users with full access.
if (_allowed_roles($user)) {
return user_edit_access($account);
}
else {
// Check users access.
if ($op === 'edit') {
if ((user_access('edit administrator profile', $account) || $user->uid == $entity->uid) && $account->uid > 0) {
return TRUE;
}
else {
return FALSE;
}
}
}
}
Apparently this is a custom way to resolve your issue, not a module, but taking advantage of the Drupal's API will benefit you more in the future, happy coding!