You are here

function _contact_attach_get_valid_roles in Contact Attach 7

Gets active user's valid roles to be considered in aggregation of settings.

Parameters

string $contact_form_permission: The contact form permission to check permissions against.

array $contact_attach_numbers: An associative array of the number of attachments allowed for each role.

Return value

array An associative array of the active user's valid roles that will be considered in the aggregation and overriding of settings.

3 calls to _contact_attach_get_valid_roles()
ContactAttachContactFormsTestCase::checkNumberOfAttachmentFields in ./contact_attach.test
Checks the number of attachment fields after setting the variable.
ContactAttachContactFormsTestCase::submitWithAttachments in ./contact_attach.test
Submits contact forms with attachments after setting the settings.
contact_attach_form_alter in ./contact_attach.module
Implements hook_form_alter().

File

./contact_attach.module, line 447
Allows attaching files to messages sent using contact forms.

Code

function _contact_attach_get_valid_roles($contact_form_permission, $contact_attach_numbers) {
  global $user;
  $permitted_roles = user_roles(FALSE, $contact_form_permission);
  if (count($user->roles) === 1) {
    $roles = array_keys($user->roles);
  }
  elseif (array_key_exists(DRUPAL_AUTHENTICATED_RID, $user->roles) && array_key_exists(DRUPAL_AUTHENTICATED_RID, $permitted_roles)) {

    // If the user has the authenticated user role and it is permitted to attach
    // files, all of the user's roles are valid, as all created roles inherit
    // this role. Hence, no use in checking if they have permissions.
    $roles = $user->roles;

    // Exclude the authenticated user role when the user has settings set by
    // other roles, as all created users automatically get this role and it
    // should not override the settings for the explicitly assigned role.
    $has_specific_setting = FALSE;
    foreach ($roles as $rid => $name) {
      if ($rid !== DRUPAL_AUTHENTICATED_RID && array_key_exists($rid, $contact_attach_numbers)) {
        $has_specific_setting = TRUE;
        break;
      }
    }
    if (array_key_exists(DRUPAL_AUTHENTICATED_RID, $roles) && $has_specific_setting) {
      unset($roles[DRUPAL_AUTHENTICATED_RID]);
    }
    $roles = array_keys($roles);
  }
  else {

    // Figure out which of the user's roles are permitted to add attachments.
    $roles = array_keys(array_intersect_assoc($user->roles, $permitted_roles));
  }
  return $roles;
}