| 1 |
|
<?php |
| 2 |
|
// $Id:$ |
| 3 |
|
|
| 4 |
|
|
| 5 |
|
function role_confer_menu($may_cache) { |
| 6 |
|
|
| 7 |
|
if (!$may_cache) { |
| 8 |
|
|
| 9 |
|
$items[] = array('path' => 'admin/user/role_confer', |
| 10 |
|
'title' => t('Role Confer'), |
| 11 |
|
'callback' => 'drupal_get_form', |
| 12 |
|
'callback arguments' => array('role_confer_administration'), |
| 13 |
|
'description' => t('Confer a role to a user account based on criteria.'), |
| 14 |
|
); |
| 15 |
|
} |
| 16 |
|
|
| 17 |
|
return $items; |
| 18 |
|
} |
| 19 |
|
|
| 20 |
|
function role_confer_administration() { |
| 21 |
|
|
| 22 |
|
$settings = variable_get('role_confer', array()); |
| 23 |
|
role_confer_load_supported(); |
| 24 |
|
|
| 25 |
|
// select role to confer |
| 26 |
|
$roles = user_roles(TRUE); |
| 27 |
|
|
| 28 |
|
$form['role_confer'] = array( |
| 29 |
|
'#type' => 'fieldset', |
| 30 |
|
'#title' => t('Role To Confer'), |
| 31 |
|
'#description' => t('Select the role(s) to be conferred once the user has met all the criteria.'), |
| 32 |
|
); |
| 33 |
|
|
| 34 |
|
$form['role_confer']['role'] = array( |
| 35 |
|
'#type' => 'checkboxes', |
| 36 |
|
'#title' => t('Roles'), |
| 37 |
|
'#default_value' => $settings['role'], |
| 38 |
|
'#options' => $roles, |
| 39 |
|
); |
| 40 |
|
|
| 41 |
|
// invoke hook to include additional criteria |
| 42 |
|
$additional = module_invoke_all('role_confer_configuration', $settings); |
| 43 |
|
if (is_array($additional)) $form = $form + $additional; |
| 44 |
|
|
| 45 |
|
$form[] = array( |
| 46 |
|
'#type' => 'submit', |
| 47 |
|
'#value' => t('Save'), |
| 48 |
|
); |
| 49 |
|
|
| 50 |
|
return $form; |
| 51 |
|
} |
| 52 |
|
|
| 53 |
|
function role_confer_administration_validate($form_id, $form_values) { |
| 54 |
|
|
| 55 |
|
// check at least one role has been selected |
| 56 |
|
$role_selected = FALSE; |
| 57 |
|
|
| 58 |
|
foreach ($form_values['role'] as $key => $value) { |
| 59 |
|
if ($key > 1 && !empty($value)) $role_selected = TRUE; |
| 60 |
|
} |
| 61 |
|
|
| 62 |
|
if (!$role_selected) { |
| 63 |
|
form_set_error('role', t('At least one role must be selected.')); |
| 64 |
|
} |
| 65 |
|
} |
| 66 |
|
|
| 67 |
|
function role_confer_administration_submit($form_id, $form_values) { |
| 68 |
|
|
| 69 |
|
// invoke hook to get ID of forms affected |
| 70 |
|
$form_values['forms_affected'] = module_invoke_all('role_confer_forms_affected', $form_values); |
| 71 |
|
|
| 72 |
|
variable_set('role_confer', $form_values); |
| 73 |
|
} |
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
/** |
| 78 |
|
* Implementation of hook_form_alter(). |
| 79 |
|
*/ |
| 80 |
|
function role_confer_form_alter($form_id, &$form) { |
| 81 |
|
|
| 82 |
|
$settings = variable_get('role_confer', array()); |
| 83 |
|
|
| 84 |
|
if (!is_array($settings['forms_affected'])) return; |
| 85 |
|
|
| 86 |
|
if (in_array($form_id, $settings['forms_affected'])) { |
| 87 |
|
$form['#submit']['role_confer_form_submit'] = array(); |
| 88 |
|
} |
| 89 |
|
|
| 90 |
|
} |
| 91 |
|
|
| 92 |
|
/** |
| 93 |
|
* If all criteria are met confer the role(s) |
| 94 |
|
*/ |
| 95 |
|
function role_confer_form_submit($form_id, $form_values) { |
| 96 |
|
// check user has logged in at least once |
| 97 |
|
global $user; |
| 98 |
|
if (empty($user->login)) return; |
| 99 |
|
|
| 100 |
|
// check configuration has been set for this module |
| 101 |
|
$settings = variable_get('role_confer', array()); |
| 102 |
|
$roles_setting = role_confer_specified_role($settings['role']); |
| 103 |
|
if (empty($roles_setting)) return; |
| 104 |
|
|
| 105 |
|
// check if user already has role |
| 106 |
|
$roles = $user->roles; |
| 107 |
|
$confered = TRUE; |
| 108 |
|
|
| 109 |
|
foreach ($roles_setting as $rid => $name) { |
| 110 |
|
if (empty($roles[$rid])) $set_already = FALSE; |
| 111 |
|
} |
| 112 |
|
|
| 113 |
|
if ($set_already) return; |
| 114 |
|
|
| 115 |
|
// invoke hook to check criteria for confering role |
| 116 |
|
role_confer_load_supported(); |
| 117 |
|
$missing = module_invoke_all('role_confer_validate', $settings, $form_values); |
| 118 |
|
|
| 119 |
|
if (empty($missing)) { // confer role |
| 120 |
|
role_confer_assign_role($user->uid, $settings['role']); |
| 121 |
|
} |
| 122 |
|
else { // maybe show message here |
| 123 |
|
|
| 124 |
|
} |
| 125 |
|
} |
| 126 |
|
|
| 127 |
|
/** |
| 128 |
|
* set role(s) for a user |
| 129 |
|
*/ |
| 130 |
|
function role_confer_assign_role($uid, $assign_roles) { |
| 131 |
|
|
| 132 |
|
$role_labels = user_roles(); |
| 133 |
|
$roles_already_set = TRUE; |
| 134 |
|
$account = user_load(array('uid' => $uid)); |
| 135 |
|
$account_roles = $account->roles; |
| 136 |
|
|
| 137 |
|
foreach ($assign_roles as $role_id => $assign) { |
| 138 |
|
|
| 139 |
|
if (empty($account_roles[$role_id]) && !empty($assign)) { |
| 140 |
|
$roles_already_set = FALSE; |
| 141 |
|
$account_roles[$role_id] = $role_labels[$role_id]; |
| 142 |
|
} |
| 143 |
|
} |
| 144 |
|
|
| 145 |
|
if (!$roles_already_set) { |
| 146 |
|
user_save($account, array('roles' => $account_roles)); |
| 147 |
|
} |
| 148 |
|
|
| 149 |
|
return; |
| 150 |
|
} |
| 151 |
|
|
| 152 |
|
/** |
| 153 |
|
* Return just the roles that where selected |
| 154 |
|
* formatted ready to be added to the user's role |
| 155 |
|
*/ |
| 156 |
|
function role_confer_specified_role($roles) { |
| 157 |
|
$roles_all = user_roles(TRUE); |
| 158 |
|
$roles_specified = array(); |
| 159 |
|
|
| 160 |
|
foreach ($roles as $key => $value) { |
| 161 |
|
if (!empty($value)) { |
| 162 |
|
$roles_specified[$key] = $roles_all[$key]; |
| 163 |
|
} |
| 164 |
|
} |
| 165 |
|
|
| 166 |
|
return $roles_specified; |
| 167 |
|
} |
| 168 |
|
|
| 169 |
|
|
| 170 |
|
|
| 171 |
|
/** |
| 172 |
|
* Loads the hooks for the supported modules. |
| 173 |
|
*/ |
| 174 |
|
function role_confer_load_supported() { |
| 175 |
|
static $loaded = FALSE; |
| 176 |
|
if (!$loaded) { |
| 177 |
|
$path = drupal_get_path('module', 'role_confer') . '/supported'; |
| 178 |
|
$files = drupal_system_listing('.*\.inc$', $path, 'name', 0); |
| 179 |
|
foreach ($files as $module_name => $file) { |
| 180 |
|
if (module_exists($module_name)) { |
| 181 |
|
include_once($file->filename); |
| 182 |
|
} |
| 183 |
|
} |
| 184 |
|
$loaded = TRUE; |
| 185 |
|
} |
| 186 |
|
} |
| 187 |
|
|
| 188 |
|
|
| 189 |
|
|
| 190 |
|
|
| 191 |
|
|
| 192 |
|
|