dynammic user role assignment : Autoassginrole
the module auto assign role , provides an option for the administrator to decide which role a new user is assigned to . By default the new user is assigned the authenticated user role . But through this module you can either automatically assign a particular role to every user or allow the user to select from a list of roles during registration .
But there are situations when u need more than this . Suppose we have a university portal , and we have two links like this
Now if we use autoassignrole we will have the same role assigned to both the new teacher and student . Say we have two roles 'teacher' and 'student', what we can be achieved by a small tweak in the autoassignrole module . We can pass the role to be assigned in the URL . The url for registering is always http://www.xyz.com/?q=user/register , now even if we open http://www.xyz.com/?q=user/register/teacher or http://www.xyz.com/?q=user/register/student , it will again open the user registration page because pages with the above URLs are not defined , now we can catch the role in the URL with arg(2) , all you have to do is enable the autoassignrole module , enable it and set the default role assigned to a new user as 'anonymous' and make the following changes to autoassignrole.module , find the following code in the function autoassignrole_form_alter()
'#type' => _autoassignrole_user_input('type'),
'#title' => variable_get('AUTOASSIGNROLE_ROLE_USER_TITLE',""),
'#options' => _autoassignrole_intersect(),
'#required' => _autoassignrole_user_input('required'),
'#description' => variable_get('AUTOASSIGNROLE_ROLE_USER_DESCRIPTION',""),
);
replace it by :
$rolename = arg(2);
if(empty($rolename)){
$rolename = 'authenticated user';
}
$actual_role_id = db_result(db_query("SELECT rid FROM {role} WHERE name = '".$rolename."'"));
$roles = array(); // we need to do this since we ned to add this as the input for ['roles'] has to be an array
$roles[0] = $actual_role_id;
/**********************************************************************************************************************/
$form['autoassignrole_user']['AUTOASSIGNROLE_ROLE_USER'] = array(
'#type' => 'value',//_autoassignrole_user_input('type'),
'#title' => variable_get('AUTOASSIGNROLE_ROLE_USER_TITLE',""),
'#value' => $roles,//added this
// '#options' => _autoassignrole_intersect(),
'#required' => _autoassignrole_user_input('required'),
'#description' => variable_get('AUTOASSIGNROLE_ROLE_USER_DESCRIPTION',""),
);
now attach the role u want to be assigned when a particular link is clicked to the URL as i have described above in the teacher and student example above .Also note that when the normal user regostration URL is used the default role of an authenticated user is assigned !
DISCLAIMER: passing the user role may not be a good and secure idea in all cases , ... it suits best in my case , ... so the developer should be careful about what he is doing !, happy hacking :)
- Article Type:
- Drupal Version:
- Tag Base:
- Login to post comments
