You are here

contact_permissions.module in Contact Permissions 6

Provides permissions for having a personal contact form

File

contact_permissions.module
View source
<?php

/**
 * @file
 * Provides permissions for having a personal contact form
 */

/**
 * Implementation of hook_perm
 */
function contact_permissions_perm() {
  return array(
    'Have a personal contact form',
  );
}
function contact_permissions_check_access($account) {
  return user_access('Have a personal contact form', $account);
}

/**
 * Implementation of hook_menu_alter().
 */
function contact_permissions_menu_alter(&$items) {
  $items['user/%user/contact']['access callback'] = '_contact_permissions_user_tab_access';
}

/**
 * Menu access callback for a user's personal contact form.
 *
 * @param $account
 *   A user account object.
 * @return
 *   TRUE if the user is allowed to have a contact form
 *   or FALSE otherwise.
 */
function _contact_permissions_user_tab_access($account) {

  // Check core's contact access callback first
  $access = _contact_user_tab_access($account);
  if ($access) {
    $access = contact_permissions_check_access($account);
    if ($access) {

      // Drupal core contact module does not allow annonymous users.
      // Redirect to user login form and set
      // appropiate message for anonymous users.
      if (user_is_anonymous()) {
        drupal_set_message(t('You must login to contact <em>@name</em>', array(
          '@name' => $account->name,
        )));
        drupal_goto('user/login', drupal_get_destination());
      }
    }
  }
  return $access;
}

/**
 * Implementation of hook_user().
 *
 * Restricts the option of enabling/disabling his personal contact form
 * Based on permissions defined by this module
 */
function contact_permissions_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_profile_form') {
    $account = $form['_account']['#value'];
    $access = contact_permissions_check_access($account);
    if (!$access) {
      unset($form['contact']);
    }
  }
}

Functions

Namesort descending Description
contact_permissions_check_access
contact_permissions_form_alter Implementation of hook_user().
contact_permissions_menu_alter Implementation of hook_menu_alter().
contact_permissions_perm Implementation of hook_perm
_contact_permissions_user_tab_access Menu access callback for a user's personal contact form.