single_user_role.module in Single User Role 7
Same filename and directory in other branches
Single User Role module file.
It contains alterations to restric particular user role to authenticated or any other role.
File
single_user_role.moduleView source
<?php
/**
* @file
* Single User Role module file.
*
* It contains alterations to restric particular user role to authenticated or
* any other role.
*/
/**
* Implements hook_form_alter().
*/
function single_user_role_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'user_register_form' || $form_id == 'user_profile_form') {
// Set user role field name.
$role_field_name = 'roles';
if (module_exists('role_delegation')) {
// Based on role delegation module condition only change role field for
// user without administer permissions.
if (!user_access('administer permissions')) {
$role_field_name = 'roles_change';
}
}
// Change role field type from checkbox to select or radio field.
$form['account'][$role_field_name]['#type'] = variable_get('single_user_role_field_type', 'select');
$form['account'][$role_field_name]['#multiple'] = FALSE;
// Set role field helptext.
$form['account'][$role_field_name]['#description'] = variable_get('single_user_role_field_desc', '');
// Set role field title singular.
$form['account'][$role_field_name]['#title'] = t('Role');
// Remove authenticated user checkbox.
$form['account'][$role_field_name][2]['#access'] = FALSE;
// Set default role.
if (isset($form['account'][$role_field_name]['#default_value']) && is_array($form['account'][$role_field_name]['#default_value'])) {
foreach ($form['account'][$role_field_name]['#default_value'] as $key => $value) {
if ($value != 2) {
// Set first assigned role as default.
$form['account'][$role_field_name]['#default_value'] = $value;
break;
}
}
}
$form['account'][$role_field_name][2]['#access'] = FALSE;
// Make our validation first one to change structure of roles field back.
array_unshift($form['#validate'], 'single_user_role_form_validate');
}
}
/**
* Validate handler for user profile forms.
*
* @param $form
* @param $form_state
*/
function single_user_role_form_validate($form, &$form_state) {
$role_field_name = 'roles';
if (module_exists('role_delegation')) {
if (!user_access('administer permissions')) {
$role_field_name = 'roles_change';
}
}
if (!is_array($form_state['values'][$role_field_name])) {
// Set authenticated user role with single role selected.
$form_state['values'][$role_field_name] = array(
$form_state['values'][$role_field_name] => $form_state['values'][$role_field_name],
DRUPAL_AUTHENTICATED_RID => TRUE,
);
}
}
/**
* Implements hook_menu().
*/
function single_user_role_menu() {
// Single user role configuration form callback.
$items['admin/config/people/single-user-role'] = array(
'title' => 'Single role Settings',
'description' => 'Manage Single User Role configurations',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'single_user_role_config_form',
),
'access arguments' => array(
'single user role config',
),
'file' => 'single_user_role.admin.inc',
'weight' => 10,
);
return $items;
}
/**
* Implements hook_permission().
*/
function single_user_role_permission() {
// Set permission for managing module configurations.
return array(
'single user role config' => array(
'title' => t('Perform administration tasks for single user role.'),
),
);
}
/**
* Implements hook_help().
*/
function single_user_role_help($path, $arg) {
switch ($path) {
case 'admin/config/people/single-user-role':
return '<p>' . t("This page allows to administer single user role module's configuration settings.") . '</p>';
}
}
/**
* Implements hook_module_implements_alter().
*/
function single_user_role_module_implements_alter(&$implementations, $hook) {
if ($hook == 'form_alter') {
if (module_exists('role_delegation')) {
// Rearrange form alter to run after role delegation.
$single_user_role = $implementations['single_user_role'];
unset($implementations['single_user_role']);
$role_delegation = $implementations['role_delegation'];
unset($implementations['role_delegation']);
$implementations['role_delegation'] = $role_delegation;
$implementations['single_user_role'] = $single_user_role;
}
}
}
Functions
Name | Description |
---|---|
single_user_role_form_alter | Implements hook_form_alter(). |
single_user_role_form_validate | Validate handler for user profile forms. |
single_user_role_help | Implements hook_help(). |
single_user_role_menu | Implements hook_menu(). |
single_user_role_module_implements_alter | Implements hook_module_implements_alter(). |
single_user_role_permission | Implements hook_permission(). |