| 1 |
<?php |
<?php |
| 2 |
// $Id: og_reg_keys.module,v 1.2 2008/11/26 19:42:21 marcp Exp $ |
// $Id: og_reg_keys.module,v 1.2.2.1 2009/06/09 19:30:40 marcp Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 6 |
* Provides group registration keys to restrict group access. |
* Provides group registration keys to restrict group access. |
| 7 |
*/ |
*/ |
| 8 |
|
|
| 9 |
|
define('OG_REG_KEYS_REQUIRED', 0); |
| 10 |
|
define('OG_REG_KEYS_NOT_REQUIRED_VISIBLE', 1); |
| 11 |
|
define('OG_REG_KEYS_NOT_REQUIRED_NOT_VISIBLE', 2); |
| 12 |
|
|
| 13 |
/** |
/** |
| 14 |
* Implementation of hook_help(). |
* Implementation of hook_help(). |
| 15 |
*/ |
*/ |
| 43 |
'page arguments' => array('og_reg_keys_join'), |
'page arguments' => array('og_reg_keys_join'), |
| 44 |
'weight' => 10, |
'weight' => 10, |
| 45 |
'type' => MENU_LOCAL_TASK, |
'type' => MENU_LOCAL_TASK, |
| 46 |
'access callback' => 'user_is_logged_in', |
'access callback' => 'user_is_logged_in', |
| 47 |
); |
); |
| 48 |
$items['admin/og/reg-keys-settings'] = array( |
$items['admin/og/reg-keys-settings'] = array( |
| 49 |
'title' => 'OG Reg Keys Settings', |
'title' => 'OG Reg Keys Settings', |
| 122 |
} |
} |
| 123 |
} |
} |
| 124 |
} |
} |
| 125 |
break; |
else if (og_reg_keys_required_for_registration()) { |
| 126 |
|
form_set_error('og_reg_key', t('Group registration key is required.')); |
| 127 |
|
} |
| 128 |
|
break; |
| 129 |
case 'insert': |
case 'insert': |
| 130 |
// Reg key is only present during registration |
// Reg key is only present during registration |
| 131 |
if (isset($edit['og_reg_key']) && og_reg_keys_valid_key($edit['og_reg_key'])) { |
if (isset($edit['og_reg_key']) && og_reg_keys_valid_key($edit['og_reg_key'])) { |
| 138 |
if ($rid) { |
if ($rid) { |
| 139 |
// Let the core user_save() code deal with setting the user's role |
// Let the core user_save() code deal with setting the user's role |
| 140 |
$edit['roles'][$rid] = 1; |
$edit['roles'][$rid] = 1; |
| 141 |
} |
} |
| 142 |
} |
} |
| 143 |
// no redirect to node as this is someone adding users through the standard interface |
// no redirect to node as this is someone adding users through the standard interface |
| 144 |
break; |
break; |
| 145 |
case 'register': |
case 'register': |
| 146 |
$form['og_reg_key'] = array( |
if (og_reg_keys_visible_on_registration_form()) { |
| 147 |
'#type' => 'textfield', |
$form['og_reg_key'] = array( |
| 148 |
'#title' => t('Group registration key'), |
'#type' => 'textfield', |
| 149 |
'#required' => variable_get('og_reg_key_required', 1), |
'#title' => t('Group registration key'), |
| 150 |
'#description' => '', |
'#required' => og_reg_keys_required_for_registration(), |
| 151 |
'#size' => 64, |
'#description' => _og_reg_keys_help_text(), |
| 152 |
); |
'#size' => 64, |
| 153 |
|
); |
| 154 |
|
} |
| 155 |
|
|
| 156 |
return $form; |
return $form; |
| 157 |
break; |
break; |
| 158 |
} |
} |
| 181 |
if (!empty($node->nid)) { |
if (!empty($node->nid)) { |
| 182 |
$result = db_query('SELECT (nid) FROM {og_reg_keys} WHERE nid = %d', $node->nid); |
$result = db_query('SELECT (nid) FROM {og_reg_keys} WHERE nid = %d', $node->nid); |
| 183 |
$r = db_fetch_array($result); |
$r = db_fetch_array($result); |
| 184 |
|
|
| 185 |
if ($node->og_reg_keys != '') { |
if ($node->og_reg_keys != '') { |
| 186 |
if ($r) { |
if ($r) { |
| 187 |
db_query("UPDATE {og_reg_keys} SET code = '%s' WHERE nid = %d", $node->og_reg_keys, $node->nid); |
db_query("UPDATE {og_reg_keys} SET code = '%s' WHERE nid = %d", $node->og_reg_keys, $node->nid); |
| 199 |
db_query('DELETE FROM {og_reg_keys} WHERE nid = %d', $node->nid); |
db_query('DELETE FROM {og_reg_keys} WHERE nid = %d', $node->nid); |
| 200 |
break; |
break; |
| 201 |
} |
} |
| 202 |
} |
} |
| 203 |
} |
} |
| 204 |
|
|
| 205 |
/** |
/** |
| 230 |
function og_reg_keys_settings() { |
function og_reg_keys_settings() { |
| 231 |
|
|
| 232 |
$roles = user_roles(TRUE); |
$roles = user_roles(TRUE); |
| 233 |
$roles[DRUPAL_AUTHENTICATED_RID] = '<'. t('None') .'>'; |
$roles[DRUPAL_AUTHENTICATED_RID] = '<'. t('None') .'>'; |
| 234 |
|
|
| 235 |
$form['og_reg_keys_role'] = array( |
$form['og_reg_keys_role'] = array( |
| 236 |
'#type' => 'select', |
'#type' => 'select', |
| 237 |
'#title' => t('Role for new registrants'), |
'#title' => t('Role for new registrants'), |
| 241 |
); |
); |
| 242 |
|
|
| 243 |
$form['og_reg_keys_message'] = array( |
$form['og_reg_keys_message'] = array( |
| 244 |
'#type' => 'textarea', |
'#type' => 'textarea', |
| 245 |
'#title' => t('Text to be added to group invitation e-mails'), |
'#title' => t('Text to be added to group invitation e-mails'), |
| 246 |
'#default_value' => _og_reg_keys_message(), |
'#default_value' => _og_reg_keys_message(), |
| 247 |
'#required' => TRUE, |
'#required' => TRUE, |
| 248 |
'#description' => t("The token '%reg_key' must be included and will be replaced by the group's registration key."), |
'#description' => t("The token '%reg_key' must be included and will be replaced by the group's registration key."), |
| 249 |
); |
); |
| 250 |
|
|
| 251 |
$form['og_reg_key_required'] = array( |
$form['og_reg_key_required'] = array( |
| 252 |
'#type' => 'checkbox', |
'#type' => 'radios', |
| 253 |
'#title' => t('Registration Key Required for new account creation'), |
'#title' => t('Registration key control for new account creation'), |
| 254 |
'#default_value' => variable_get('og_reg_key_required', 1), |
'#required' => TRUE, |
| 255 |
|
'#options' => array( |
| 256 |
|
OG_REG_KEYS_REQUIRED => t('Registration key is required for new account creation.'), |
| 257 |
|
OG_REG_KEYS_NOT_REQUIRED_VISIBLE => |
| 258 |
|
t('Registration key is not required for new account creation. Display it on the registration form.'), |
| 259 |
|
OG_REG_KEYS_NOT_REQUIRED_NOT_VISIBLE => |
| 260 |
|
t('Registration key is not required for new account creation. Do not display it on the registration form.'), |
| 261 |
|
), |
| 262 |
|
'#default_value' => variable_get('og_reg_key_required', OG_REG_KEYS_REQUIRED), |
| 263 |
|
); |
| 264 |
|
|
| 265 |
|
$form['og_reg_keys_help_text'] = array( |
| 266 |
|
'#type' => 'textarea', |
| 267 |
|
'#title' => t('Registration help text'), |
| 268 |
|
'#description' => t('Additional help text to be added to the user registration form.'), |
| 269 |
|
'#default_value' => _og_reg_keys_help_text(), |
| 270 |
|
'#required' => TRUE, |
| 271 |
); |
); |
| 272 |
|
|
| 273 |
return system_settings_form($form); |
return system_settings_form($form); |
| 274 |
|
|
| 275 |
} |
} |
| 276 |
|
|
| 277 |
/** |
/** |
| 278 |
* Validation routine for form submission of 'admin/og/reg-keys-settings' |
* Validation routine for form submission of 'admin/og/reg-keys-settings' |
| 279 |
*/ |
*/ |
| 280 |
function og_reg_keys_settings_validate($form, &$form_state) { |
function og_reg_keys_settings_validate($form, &$form_state) { |
| 281 |
$op = isset($_POST['op']) ? $_POST['op'] : ''; |
$op = isset($_POST['op']) ? $_POST['op'] : ''; |
| 282 |
|
|
| 283 |
if ($op == t('Save configuration') && !strstr($form_state['values']['og_reg_keys_message'], '%reg_key')) { |
if ($op == t('Save configuration') && !strstr($form_state['values']['og_reg_keys_message'], '%reg_key')) { |
| 284 |
form_set_error('og_reg_keys_message', t("You must include in your message the token '%reg_key'")); |
form_set_error('og_reg_keys_message', t("You must include in your message the token '%reg_key'")); |
| 285 |
} |
} |
| 300 |
); |
); |
| 301 |
|
|
| 302 |
$form['#after_build'] = array('og_reg_keys_add_preview'); |
$form['#after_build'] = array('og_reg_keys_add_preview'); |
| 303 |
|
|
| 304 |
$form['preview'] = array( |
$form['preview'] = array( |
| 305 |
'#type' => 'button', |
'#type' => 'button', |
| 306 |
'#value' => t('Check key'), |
'#value' => t('Check key'), |
| 307 |
); |
); |
| 308 |
|
|
| 309 |
if ($user->uid > 0) { |
if ($user->uid > 0) { |
| 310 |
$form['submit'] = array( |
$form['submit'] = array( |
| 311 |
'#type' => 'submit', |
'#type' => 'submit', |
| 315 |
return $form; |
return $form; |
| 316 |
} |
} |
| 317 |
|
|
| 318 |
/** |
/** |
| 319 |
* Validation routine for 'og/reg-key-join' and the block submitted reg-key form |
* Validation routine for 'og/reg-key-join' and the block submitted reg-key form |
| 320 |
*/ |
*/ |
| 321 |
function og_reg_keys_join_validate($form, &$form_state) { |
function og_reg_keys_join_validate($form, &$form_state) { |
| 326 |
} |
} |
| 327 |
} |
} |
| 328 |
|
|
| 329 |
/** |
/** |
| 330 |
* Routine to handle form submission for 'og/reg-key-join' and the block submitted reg-key form |
* Routine to handle form submission for 'og/reg-key-join' and the block submitted reg-key form |
| 331 |
*/ |
*/ |
| 332 |
function og_reg_keys_join_submit($form, &$form_state) { |
function og_reg_keys_join_submit($form, &$form_state) { |
| 337 |
// Use og_save_subscription() to get around og_subscribe_user()'s |
// Use og_save_subscription() to get around og_subscribe_user()'s |
| 338 |
// selectivity checks. |
// selectivity checks. |
| 339 |
og_save_subscription($gid, $user->uid, array('is_active' => 1)); |
og_save_subscription($gid, $user->uid, array('is_active' => 1)); |
| 340 |
|
|
| 341 |
// Existing user so preserve roles as user_save is destructive |
// Existing user so preserve roles as user_save is destructive |
| 342 |
$roles = $user->roles + array(variable_get('og_reg_keys_role', DRUPAL_AUTHENTICATED_RID) => 1); |
$roles = $user->roles + array(variable_get('og_reg_keys_role', DRUPAL_AUTHENTICATED_RID) => 1); |
| 343 |
user_save($user, array('roles' => $roles)); |
user_save($user, array('roles' => $roles)); |
| 344 |
|
|
| 345 |
// Delete the user's menu cache in order to provide access |
// Delete the user's menu cache in order to provide access |
| 346 |
// to menu items secured by their newly granted role. |
// to menu items secured by their newly granted role. |
| 347 |
cache_clear_all($user->uid .':', 'cache_menu', TRUE); |
cache_clear_all($user->uid .':', 'cache_menu', TRUE); |
| 348 |
|
|
| 349 |
// Load the group node to get the name of the group. |
// Load the group node to get the name of the group. |
| 350 |
$group = node_load(array('nid' => $gid)); |
$group = node_load(array('nid' => $gid)); |
| 351 |
drupal_set_message(t('You are now a member of !group_name.', array('!group_name' => $group->title))); |
drupal_set_message(t('You are now a member of !group_name.', array('!group_name' => $group->title))); |
| 383 |
$output .= '<h3>'. t('By submitting this key, you will join this group:') .'</h3>'; |
$output .= '<h3>'. t('By submitting this key, you will join this group:') .'</h3>'; |
| 384 |
$output .= node_view($node, TRUE, FALSE, FALSE); |
$output .= node_view($node, TRUE, FALSE, FALSE); |
| 385 |
$output .= "</div>\n"; |
$output .= "</div>\n"; |
| 386 |
|
|
| 387 |
return $output; |
return $output; |
| 388 |
} |
} |
| 389 |
|
|
| 491 |
'#size' => 16, |
'#size' => 16, |
| 492 |
'#maxlength' => 60 |
'#maxlength' => 60 |
| 493 |
); |
); |
| 494 |
|
|
| 495 |
$form['pass'] = array( |
$form['pass'] = array( |
| 496 |
'#type' => 'password', |
'#type' => 'password', |
| 497 |
'#title' => t('Password'), |
'#title' => t('Password'), |
| 509 |
'#size' => 28, |
'#size' => 28, |
| 510 |
'#maxlength' => 64 |
'#maxlength' => 64 |
| 511 |
); |
); |
| 512 |
|
|
| 513 |
$form['og_reg_key'] = array( |
if (og_reg_keys_visible_on_registration_form()) { |
| 514 |
'#type' => 'textfield', |
$form['og_reg_key'] = array( |
| 515 |
'#title' => t('Group registration key'), |
'#type' => 'textfield', |
| 516 |
'#required' => variable_get('og_reg_key_required', 1), |
'#title' => t('Group registration key'), |
| 517 |
'#size' => 28, |
'#required' => og_reg_keys_required_for_registration(), |
| 518 |
); |
'#size' => 28, |
| 519 |
|
); |
| 520 |
|
} |
| 521 |
|
|
| 522 |
$form['submit'] = array('#type' => 'submit', '#value' => t('Create account')); |
$form['submit'] = array('#type' => 'submit', '#value' => t('Create account')); |
| 523 |
return $form; |
return $form; |
| 554 |
'status' => 1)); |
'status' => 1)); |
| 555 |
|
|
| 556 |
$user = user_authenticate($form_state['values']); |
$user = user_authenticate($form_state['values']); |
| 557 |
|
|
| 558 |
$gid = og_reg_keys_get_gid($form_state['values']['og_reg_key']); |
$gid = og_reg_keys_get_gid($form_state['values']['og_reg_key']); |
| 559 |
|
|
| 560 |
// Use og_save_subscription() to get around og_subscribe_user()'s |
// Use og_save_subscription() to get around og_subscribe_user()'s |
| 638 |
} |
} |
| 639 |
|
|
| 640 |
/** |
/** |
| 641 |
|
* Return the user account registration key help text. |
| 642 |
|
*/ |
| 643 |
|
function _og_reg_keys_help_text() { |
| 644 |
|
return variable_get('og_reg_keys_help_text', t("If you were given a registration key for this site you can use it here to join a group.")); |
| 645 |
|
} |
| 646 |
|
|
| 647 |
|
/** |
| 648 |
* Return the group join message. |
* Return the group join message. |
| 649 |
*/ |
*/ |
| 650 |
function _og_reg_keys_message() { |
function _og_reg_keys_message() { |
| 684 |
$error['name'] = t('The name %name has been denied access.', array('%name' => $name)); |
$error['name'] = t('The name %name has been denied access.', array('%name' => $name)); |
| 685 |
} |
} |
| 686 |
} |
} |
| 687 |
|
|
| 688 |
// Validate the e-mail address: (empty is okay); |
// Validate the e-mail address: (empty is okay); |
| 689 |
if (!empty($mail)) { |
if (!empty($mail)) { |
| 690 |
if ($err = user_validate_mail($mail)) { |
if ($err = user_validate_mail($mail)) { |
| 700 |
|
|
| 701 |
return $error; |
return $error; |
| 702 |
} |
} |
| 703 |
|
|
| 704 |
|
function og_reg_keys_required_for_registration() { |
| 705 |
|
if (variable_get('og_reg_key_required', OG_REG_KEYS_REQUIRED) == OG_REG_KEYS_REQUIRED) { |
| 706 |
|
return TRUE; |
| 707 |
|
} |
| 708 |
|
else { |
| 709 |
|
return FALSE; |
| 710 |
|
} |
| 711 |
|
} |
| 712 |
|
|
| 713 |
|
function og_reg_keys_visible_on_registration_form() { |
| 714 |
|
$val = variable_get('og_reg_key_required', OG_REG_KEYS_REQUIRED); |
| 715 |
|
if (($val == OG_REG_KEYS_REQUIRED) || ($val == OG_REG_KEYS_NOT_REQUIRED_VISIBLE)) { |
| 716 |
|
return TRUE; |
| 717 |
|
} |
| 718 |
|
else { |
| 719 |
|
return TRUE; |
| 720 |
|
} |
| 721 |
|
} |