Can you filter views user list result based on the current users role Is it possible to have the result of a views user list only contain users with a specific role that the current user also has?

I'm not familiar with Views, but it seems to fit the bill for making a better looking and more usable user list than the one that comes standard with Drupal. However, I need to have the user list filtered.

I setup multiple user groups that are identified by a specific role (like groupmember - group A). Admin users of that group have an additional admin role (like group manager), but they should only see users in their own group when using the view.

Can something like this be done? I'm writing modules so I could use the api if that is the way to go. I'm using Drupal 7.
As I understand, you want to create a view which list users which lists only those users who has a specific role or roles that the current user also has. The below approach can be a solution :


Add a field 'User: Uid' in your view (Let the view is of type 'Users').
Make the field as 'Exclude from display'.
Add a 'global php' field and add some code as below under 'VALUE CODE' :


$user_load = user_load($data->uid);
$data->user_role = $user_load->roles;
return ($data->user_role);


Here, we are getting the roles of each user being rendered.

Here used '$data->uid' instead of '$row->uid' to avoide issue of returning NID with $row in D7. Although in this case '$row->uid' returns correct information.
Make the field as 'Exclude from display' as well.
Under Filter Criteria add a 'global php' field and add the below codes :

i) Under 'Setup Code' :
 
 global $user;
 $roles = array();
 $view->roles = $user->roles;
 

Here we are getting all the roles of the current user.

ii) User 'Filter Code' :
 
 // Suppose you have five roles in the instance with the 'rid's as saved in
 // 'role' table as : 1 (anonymous user), 2(authenticated user), 3(administrator), 4(hr), 5(web admin)
 // Suppose ROLE you want be matched has RID = 4
 if (isset($view->roles[4])) {// Checking if the current user has the role that you want to be matched with
   if (!isset($data->user_role[4])) {// If the user getting rendered doesn't have that role
     return TRUE;// If TRUE is returned, then that row is NOT shown
   }
 }
 


Note : Considering, 'views_php' module is installed.

Note : If the current user doesn't have the role that you want be filtered for, then the view will list the default list of users based on other conditions.

Note : Instead of writing the 'rid' in the filter code directly, we can provide an admin menu which will render a custom form with a text field and, admin or user with required access permission, can set any role(s) through that form and save it in a drupal variable, and we can render the role information we want be matched from the variable inside our filter code.

Hope it helps