You are here

view_profiles_perms.module in View profiles permissions 6

Restricts viewing user profiles based on user roles

File

view_profiles_perms.module
View source
<?php

/**
 * @file
 * Restricts viewing user profiles based on user roles
 */

/**
 * Implements hook_perm().
 *
 * Provide permissions per role to be viewed
 */
function view_profiles_perms_perm() {
  $perms = array();
  $roles = user_roles(TRUE);

  // remove authenticated role
  unset($roles[2]);
  if (count($roles) > 0) {
    foreach ($roles as $rid => $role_name) {
      $perms[] = view_profiles_perms_permission_name($role_name);
    }
    return $perms;
  }
}

/**
 * Helper function to build the permission name
 */
function view_profiles_perms_permission_name($role_name) {
  return "access {$role_name} profiles";
}

/**
 * Implements hook_menu_alter().
 *
 * Takes over the access callback for user profiles.
 */
function view_profiles_perms_menu_alter(&$items) {
  $items['user/%user_uid_optional']['access callback'] = 'view_profiles_perms_access';
}

/**
 * Access callback for user profiles.
 */
function view_profiles_perms_access($account) {

  // First check user module's access
  $access = user_view_access($account);
  if (!$access) {

    // Check our own permissions.
    foreach ($account->roles as $rid => $role_name) {

      // Annonymous and authenticated user roles get skipped
      if ($rid == 1 || $rid == 2) {
        continue;
      }
      $perm_name = view_profiles_perms_permission_name($role_name);
      $access = user_access($perm_name);
      if ($access == TRUE) {
        break;
      }
    }
  }
  return $access;
}

Functions

Namesort descending Description
view_profiles_perms_access Access callback for user profiles.
view_profiles_perms_menu_alter Implements hook_menu_alter().
view_profiles_perms_perm Implements hook_perm().
view_profiles_perms_permission_name Helper function to build the permission name