Is there a possibility to add an addititional password query in the registration process.
Only people with the password should be able to register.

Thanks
Chris

Comments

nevets’s picture

You could make a module that implements the user hook or form_alter hook to achieve this. You would probably want an admin settings module (settings hook) to allow this 'password' to be set.

schmitty’s picture

Thanks nevets, i have read a bit around, but I can't figure out how to do this.
I'm very new in drupal an php, so could you tell me a bit more?

nevets’s picture

Copy the code below to a file call regcode.module and place the file in a directory call regcode under the modules directory.
If the registration code is not set (empty) under administer -> settings -> regcode the functionality is disabled.
Please note the registration code is stored as is in the database (no encoding)

<?php
// $Id$

/**
 * @file
 * regcode.module
 */

/**
 * Implementation of hook_help().
 */
function regcode_help($section) {
  switch ($section) {
    case 'admin/help#regcode':
      return t('Allows the administrator to set a registration code that new users must enter before they can complete the registration process');
    case 'admin/modules#description':
      return t('If enabled user must enter a registration code to register');
  }
}


/**
 * Implementation of hook_settings().
 */
function regcode_settings() {
	$form['regcode_code'] = array(
		'#type' => 'textfield',
		'#title' => t('Registration Code'),
		'#required' => TRUE,
		'#default_value' => variable_get('regcode_code', ''),
		'#description' => t('If set, new user must correctly enter this code in order to register')
	);
	
	return $form;
}


/**
 * Implementation of hook_user().
 */
function regcode_user($op, &$edit, &$account, $category = NULL) {
	$regcode =  trim(variable_get('regcode_code', ''));
	if ( $regcode ) {
		switch ( $op ) {
		case 'register':
			$form['regcode_code'] = array(
				'#type' => 'textfield',
				'#title' => t('Registration Code'),
				'#required' => TRUE,
				'#description' => t('Please enter your registration code.')
			);
			return $form;
			
		case 'validate':
			if ( $category == 'account' ) {
				if ( $regcode != $edit['regcode_code'] ) {
					form_set_error('regcode_code', t('Invalid registration code'));
				}
			}
			break;
		
		}
		
	}
}

schmitty’s picture

This is exactly what i mean! You are my hero ;)
Thanks!

schmitty’s picture

As I said, this works perfect for me, but...
...there is a validation when you want to edit your account details, such as pw, or timezone.
Is there a way to correct this???

Many Thanks

Chris

nevets’s picture

Replace the validate case with

 if ( ! $account->uid && $category == 'account' ) {
if ( $regcode != $edit['regcode_code'] ) {
form_set_error('regcode_code', t('Invalid registration code'));
}
}

The extra ! $account->uid should only be true on registration.

schmitty’s picture

great!

Many Thanks again!

colan’s picture

I'm currently working on a project that requires this functionality. Since it hasn't been done yet, I've created a Drupal project for it called Registration Code.

So far, the code is basically the same except that I've added support for multiple registration codes. There is now a configurable list, and if any of the codes in the list are submitted with registration, the user is registered.

paulj2sj’s picture

Do you have a drupal 5 version of the registration code , or how can I adapt it to work with 5.0-1.x ? Thanks.

colan’s picture

I don't have a Drupal 5 version at this time, and won't have time to work on it for the next little while. If you'd like to adapt it, please create a feature request on the project page, have a look at the conversion guide Converting 4.7.x modules to 5.x, and then submit a patch. I'll gladly create a branch and a release for version 5 at that point. Thanks!

paulj2sj’s picture

unfortunately, i'm still fairly new to drupal, so it'd be probably a tougher task for me than for someone who's more acquainted with drupal. Maybe I'll get lucky and someone else will adapt it? anyone? anyone? Dang, and I'm kinda in need of this module...

colan’s picture

If you're able to find funding for its development, I'd be able to find time to work on it. I just don't really have time for unpaid work these days; I'm doing more of that than I can handle right now. Please use my contact form if you'd like to discuss this further. Thanks!

kaushik_sarkar@drupal.org’s picture

Atlast I upgrade the regcode module for Drupal 5.1

The code is here

1. For regcode.info
==================
; $Id: regcode.info,v 1.1 2007/01/03 01:29:15 drewish Exp $
name = Regcode Example
description = An example demonstrating how a registration code appear in user registration form.
package = Regcode

2. For regcode.module
=================
<?php
// $Id: regcode.module,v 1.1 2007/02/22 20:10:18 colan Exp $

/**
* @file
* regcode.module
*/

// Initial written by nevets (http://drupal.org/user/12856).
// See http://drupal.org/node/85861 for details.
// Edited and hacked on by colan (http://drupal.org/user/58704).

/**
* Display help and module information.
*
* @param section
* which section of the site we're displaying help
* @return
* help text for section
*/
function regcode_help($section) {
switch ($section) {
case 'admin/help#regcode':
return t('

Allows administrators to set a registration code or codes
that new users must enter before they can complete the registration
process. If a new user enters a code that is not in the list of
valid codes, he or she is denied registration.

To set the codes, go to Administer->Site Configuration->Regcode and enter
them there. To disable the registration code requirement for new
users, simply disable the module (in administer->modules).

');
case 'admin/modules#description':
return t('If enabled, users must enter a valid registration code to
register');
}
}

/**
* Set valid permissions for this module.
*
* @return
* An array of valid permissions for the module
*/
function regcode_perm() {
return array('administer registration codes');
}

/**
* Declare administrative settings for a module.
*
* @return
* An array containing form items to place on the module settings page
*/
/* Actually hook_setting does not work in Drupal 5.1 environment So, the equivalent is hook_menu($may_cache) api, I implement this api
*/
function regcode_menu($may_cache)
{
$items = array();

$items[] = array('path' => 'admin/settings/regcode',
'title' => t('Regcode'),
'callback' => 'drupal_get_form',
'callback arguments' => array('regcode_admin_settings'),
'access' => user_access('administer registration codes
'),
'description' => t('Display registration code on the registration page.')

);

return $items;

}//end of function regcode_menu

function regcode_admin_settings()
{
$form['regcode_codes'] = array(
'#type' => 'textarea',
'#title' => t('Registration Code(s)'),
'#required' => TRUE,
'#default_value' => variable_get('regcode_codes', ''),
'#description' => t('If set, new users must correctly enter one of these
codes in order to register. Enter one code per line.')
);
$form = system_settings_form($form);
return $form;

}
/**
* Act on user account actions.
*
* @param $op
* The kind of action is being performed
* @param $edit
* The array of form values submitted by the user
* @param $account
* The user object on which the operation is being performed
* @param $category
* The active category of user information being edited
* @return
* This varies depending on the operation.
*/
function regcode_user($op, &$edit, &$account, $category = NULL) {

// Get the array of registration codes.
$regcodes = variable_get('regcode_codes', '');

// Only do the checking if the codes variable is set.
if (strlen(trim($regcodes))) {
switch ($op) {

case 'register':
// Inject the registration code field into the registration form.
$form['regcode_code'] = array(
'#type' => 'textfield',
'#title' => t('Registration Code'),
//'#default_value' => variable_get('regcode_codes', ''),
'#required' => TRUE,
'#description' => t('Please enter your registration code.')
);
return $form;

case 'validate':
if ($category == 'account') {
// Make sure that the entered code is in the list.
$regcodes = explode("\n", $regcodes);
array_walk($regcodes, create_function('&$a', '$a = trim($a);'));
if (!in_array(trim($edit['regcode_code']), $regcodes)) {
form_set_error('regcode_code', t('Invalid registration code'));
watchdog('regcode', t('User entered invalid registration code: ') .
$edit['regcode_code'], WATCHDOG_WARNING);
}
}
break;
}
}
}

paulj2sj’s picture

thank you! Highly appreciated. Though I still had to make a slight modification to get it to work. The same modification as suggested above with replacing the validation case. Thanks, helps me out a ton. Tested and works on drupal 5.x

schmitty’s picture

big thx for the great work

Add the bold text and it should work as in 4.7.
I think it can be added to the Registration Code module. (http://drupal.org/project/regcode)

case 'validate':
if ( ! $account->uid && $category == 'account') {
// Make sure that the entered code is in the list.
$regcodes = explode("\n", $regcodes);
array_walk($regcodes, create_function('&$a', '$a = trim($a);'));
if (!in_array(trim($edit['regcode_code']), $regcodes)) {
form_set_error('regcode_code', t('Ungültiges Lehrstuhlpasswort.'));
watchdog('regcode', t('User entered invalid registration code: ') .
$edit['regcode_code'], WATCHDOG_WARNING);
}
}
break;
}
}
}

clarkshahnelson’s picture

this is spot on! why hasn't this been changed in the module??

odeno’s picture

Got this working - thanks.

Is there a way to modify this to post the codes in a graphic so it appears at registration time?

thanks again....

d------’s picture

Hi there,

Great job!

Sounds good, so can it be released as a download for 5.x? Some of us are worried to use it until its officially there as a "stable" download release module.

Cheers!

colan’s picture

The Drupal 5 version has just been released. Please use the project issue cue for new features, bugs, etc.

Enjoy! ;)

cslee’s picture

Is it possible to remove the reg code from the list once a user created an account with that code? This is to prevent someone from using the same reg code to create more than one account. Thanks.

colan’s picture

There's an issue for that. Have a look at http://drupal.org/node/154355.

joffy’s picture

i have been looking at this module all day and it seems to me that the new user needs to know the registration code before hand. is it not possible for it to display a random code that the admin can set and thus showing the new user what code to input?

Johnboat’s picture

Is there the possibility to set certain permissions then create a regcode based on those permissions, then give the code to the appropriate person so he/she can access whats defined in the permission that were set?

Bassel’s picture

Please have a look at the code I posted @:
http://drupal.org/node/154355#comment-639100

to solve the issus of allowing only one registration per code.

The code work 100% okay, but I am sure something better could be achieved.