Roles API
Give you some benefit function about roles. This module required a field machine name of role that provide by module role_export.
<?php
// Check if current user/ specific user has a role.
roles_is_role('researcher');
roles_is_role('researcher', FALSE, 'soekarno');
roles_is_role('researcher', FALSE, '1945');
roles_is_role('researcher', FALSE, $user_object);
// Access your custom page by one role.
function MYMODULE_menu(){
$items = array();
$items['my_page'] = array(
'title' => 'Foo',
'page callback' => 'foo_callback',
// Use our function roles_is_role().
'access callback' => 'roles_is_role',
// Set the role machine name.
'access arguments' => array('researcher'),
);
return $items;
}
// Check if current user a member of group roles.
$group_of_role = array('group_1' => array($role_1, $role_2, $role_3));
// You can define a "Group of role" by implements of hook_roles_group().
roles_is_member('group_1');
// Add/remove role to current user
roles_add('researcher');
roles_remove('teacher');
// Add/remove role to specific user
roles_add('researcher', 'soekarno');
roles_add('researcher', 1945);
roles_add('researcher', $user_object);
roles_remove('teacher', 'soekarno');
roles_remove('teacher', 1945);
roles_remove('teacher', $user_object);
?>