How to grant referenced user access to Profile page I use Node access user reference module in many projects. I have a very similar task: Grant access to a profile page for those user who is referenced in that profile. Unfortunately, Node access user reference doesn't work in this case, since it's not a node but a different kind of entity. Is there any solution for this task?
Here is ready to use code. For this to work you need to enable the Entity reference module, add entity reference field on admin/config/people/accounts/fields page targeting user entity and setting to unlimited, and finally giving access to View user profiles to all authenticated user role members.

Create my_module.info file and paste:

name = My Module
description = Does awesome things. Makes tea. Washes up. Favours of a personal nature.
dependencies[] = entityreference
core = 7.x


Create my_module.module file and paste:

/**
 * Implements hook_user_view_alter().
 */
function my_module_user_view_alter(&amp;$build) {
  global $user;
  if ($build['#account']-&gt;uid == $user-&gt;uid || in_array('administrator', array_values($user-&gt;roles))) {
    // Return early if user is administrator or is at their own profile.
    return;
  } elseif (isset($build['field_users'])) {
    foreach ($build['field_users']['#object']-&gt;field_users[LANGUAGE_NONE] as $key =&gt; $referenced) {
      if ($user-&gt;uid == $referenced['target_id']) {
        return;
      }
    }
  }
  drupal_access_denied();
}


Go to a user profile, add some user references and try to view that user's profile as the referenced user to gain access and then as non-referenced user to get denied access.