How to set the permission for particular role user to access the user profiles of users with a particular role? I have two user roles:


business_users
company_users


I want to set the permission for all users to see the user profile of users in "business_users" role only.


User with the "company_user" role should not to see user profile of user in their role but they can see user profile of user in "business_users" role.
User with the "business_users" role can see user profile of users with their role but they should not see the user profile of users with the "company_user" role.
Anonymous users should see user profile of user in "business_users" role only.


Can you tell how can I implement it?
You can make a template file for your profile content type, probably called something like node_profile.tpl.

At the top of this file, write some code that will get the user object of the node owner, and check this against user's roles in $user-&gt;roles.

It's easier if you do this with the roleID numbers (you can see these by going to user>role and hovering your mouse over the various links for the roles - the roleID appears as a number somewhere in the link).

Your code in the template then may look something like this.  You'll need to know php to understand how it executes.

$userRoles = $GLOBALS['user']-&gt;roles;
$account = user_load($node-&gt;nid);
if(!empty($userRoles[2])) {
  // user is a company user
  if(!empty($account-&gt;roles[2])) {
    // profile belongs to a company user
    print 'Company users are only allowed to see business user profiles';
    return; // this will stop execution of the template file, and return to the page template
  } elseif(!empty($account-&gt;roles[1])) {
    // profile belongs to a business user
    // do nothing - just continue code execution of this template
  }
}


For your listings of users, you'll want to make three different views, with views filters in each to determine which users you want displayed.  For this, you can create a page which chooses the name of the view based on the user's role(s) - either a "page" with the php code in the body (if you have php code as one of your input types), or else a page you create with a small custom module using hook_menu().