Drupal: Allow registrations through Invite or Referral modules only

The Invite module provides invitations from existing users to their contacts. The Referral module, in contrary, creates a special URL for each existing user, which can be found in each user's profile, and allows new user registration. Even though, these two modules seem to provide the same functionality, they don't (and they shouldn't). Invite module, provides a mechanism for a site administrator to limit new registrations to "Invitations only". Referral module doesn't provide any of this functionality. Some users have requested the Invite module and Referral module to join in one module. Until now, there isn't anything to that direction. Wouldn't it be great though if there was a solution to limit drupal registration to referral or invitation only? Copy the functions below to a refinvite.module file in your sites→all→modules→refinvite folder and enable the module. Then go to http://<your-address-here>/admin/user/settings and enable the new registration mode provided by this module:
<?php
/* This module creates a new way of limiting new user registration by invites or referrals only.*/

/*
* Implement hook_menu_alter()
*/
function refinvite_menu_alter(&$items){
  if(refinvite_user_registration_by_invite_or_referral_only()){
    $items['user/register']['access callback'] = 'refinvite_user_register_access';
  }
}

/*Determine if user registration mode is set to referral or invite only*/
function refinvite_user_registration_by_invite_or_referral_only(){
  return(variable_get('user_register', 1) === 'ev_referral_or_invite_only');
}

/**
 * This function is an access callback and
 * determines access to user registration form
 */
function refinvite_user_register_access(){
  $access = TRUE;
  if(!isset($_COOKIE[REFERRAL_COOKIE])){
    $invite = invite_load_from_session();
    if((!$invite)&&(!user_access('administer users'))){
      if(arg(1) === 'register'){
        $access = FALSE;
      }
    }
  }
  if($access){
    return user_register_access();
  }else{
    drupal_set_message(t('Registrations are closed.<br/>New registrations are only allowed through invitation or referral from existing users.'));
    return FALSE;
  }
}

/*
 * Implement hook_form_alter()
 */
function refinvite_form_alter(&$form, $form_state, $form_id){
  if($form_id == 'user_admin_settings'){
    /*
     * Setting the new mode is only allowed if no other
     * module has overridden the menu access handler for
     * the user registration form.
     */
    $item = menu_get_item('user/register');
    if(in_array($item['access_callback'], array('user_register_access','refinvite_user_register_access'))){
      $form['registration']['user_register']['#options']['ev_referral_or_invite_only'] = t('New user registration by referral or invitation only.');
    }
  }
}

/**
 * Implementation of hook_disable()
 *
 * If disabling the module and its registration
 * method is enabled, reset the registration method
 * to
 */
function refinvite_disable() {
  if (refinvite_user_registration_by_invite_or_referral_only()) {
    variable_set('user_register', 1);
    drupal_set_message(t('User registration option reset to %no_approval.', array('%no_approval' => t('Visitors can create accounts and no administrator approval is required.'))));
  }
}

Comments

Popular Posts