You are here

function userprotect_get_user_protection in User protect 5

Same name and namespace in other branches
  1. 6 userprotect.module \userprotect_get_user_protection()
  2. 7 userprotect.module \userprotect_get_user_protection()

Checks to see if the specified user has the specified protection.

Parameters

$account The user object to check.:

$protection The protection to check for.:

Return value

TRUE if the user has the specified protection, FALSE otherwise.

3 calls to userprotect_get_user_protection()
userprotect_form_alter in ./userprotect.module
Alters forms for user protection.
userprotect_menu in ./userprotect.module
Implementation of hook_menu().
userprotect_user_admin_account_validate in ./userprotect.module
Custom validation function for protecting users from the user administration operations.

File

./userprotect.module, line 1074

Code

function userprotect_get_user_protection($account, $protection) {
  global $user;
  static $protections = array();
  static $role_protections;
  $uid = $account->uid;
  $roles = $account->roles;

  // Users editing their own accounts have the permissions for e-mail
  // and password determined by the role-based setting in the userprotect
  // section at admin/user/access. This is done for consistency with the
  // way core handles the self-editing of usernames.
  if ($uid == $user->uid && in_array($protection, array(
    'up_name',
    'up_mail',
    'up_pass',
    'up_edit',
  ))) {
    switch ($protection) {
      case 'up_name':
        return !user_access('change own username');
      case 'up_mail':
        return !user_access('change own e-mail');
      case 'up_pass':
        return !user_access('change own password');

      // Always let user access their own edit page.
      case 'up_edit':
        return FALSE;
    }
  }

  // If this user hasn't been added to the result array yet, then pull their information.
  if (!isset($protections[$uid])) {
    $result = db_query("SELECT * FROM {userprotect} WHERE uid = %d AND up_type = 'user'", $uid);
    if (db_num_rows($result)) {
      $protections[$uid] = db_fetch_array($result);
    }
  }

  // If per-user protections exist for this user, stop here and return the value of the protection.
  if (isset($protections[$uid][$protection])) {
    return $protections[$uid][$protection];
  }

  // Grab the role protections if they haven't already been initialized.
  if (!isset($role_protections)) {
    $role_protections = variable_get('userprotect_role_protections', array());
  }
  if (!empty($role_protections)) {

    // For each role, check to see if it's enabled for that protection.
    // Return TRUE as soon as we find a protected role.
    foreach ($roles as $rid => $role) {
      if ($role_protections[$rid][$protection]) {
        return TRUE;
      }
    }
  }

  // No protection enabled.
  return FALSE;
}