forked from ManatiCR/user_login_redirect
/
user_login_redirect.module
234 lines (221 loc) · 7.29 KB
/
user_login_redirect.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
* @file
* This file provides a Configuration form and redirect functionality
*/
/**
* Implements hook_help().
*/
function user_login_redirect_help($path, $arg) {
switch ($path) {
case 'admin/help#user_login_redirect':
return '<p>' . t('This module provides redirects on login by role') . '</p>';
}
}
/**
* Implements hook_menu().
*/
function user_login_redirect_menu() {
$items = array();
$items['admin/config/people/user-login-redirect'] = array(
'title' => 'User login redirect config',
'page callback' => 'drupal_get_form',
'page arguments' => array('user_login_redirect_config_form'),
'access arguments' => array('configure login redirect'),
);
return $items;
}
/**
* Implements hook_permission().
*/
function user_login_redirect_permission() {
$permissions['configure login redirect'] = array(
'title' => t('Configure Settings for User login redirect'),
'description' => t('Access to configure User Login redirect'),
'restrict access' => TRUE,
);
return $permissions;
}
/**
* Configuration form.
*/
function user_login_redirect_config_form($form, $form_state) {
$configured_roles = user_login_redirect_get_all_data();
$roles = user_roles(TRUE);
foreach ($roles as $key => $value) {
$default_weight = '';
$default_path = '';
if (!empty($configured_roles[$key])) {
$default_weight = $configured_roles[$key]->weight;
$default_path = $configured_roles[$key]->path;
}
$form['roles_properties_fieldset_' . $key] = array(
'#type' => 'fieldset',
'#title' => t('@role_name role', array('@role_name' => $value)),
'#collapsible' => TRUE,
'#colapsed' => FALSE,
);
$form['roles_properties_fieldset_' . $key]['role_redirect_weight_' . $key] = array(
'#type' => 'weight',
'#title' => t('Weight for role @role', array('@role' => $value)),
'#default_value' => $default_weight,
'#description' => t('The lower weight, the more importance the role will have.'),
);
$form['roles_properties_fieldset_' . $key]['role_redirect_path_' . $key] = array(
'#type' => 'textfield',
'#title' => t('Redirect path for role @role', array('@role' => $value)),
'#default_value' => $default_path,
);
}
$form['submit_button'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
);
return $form;
}
/**
* User login redirect config form submit.
*/
function user_login_redirect_config_form_submit($form, &$form_state) {
$roles = user_roles(TRUE);
foreach ($roles as $key => $value) {
if (!empty($form_state['values']['role_redirect_path_' . $key])) {
$redirect_data = user_login_redirect_get_data_by_rid($key);
if (empty($redirect_data)) {
$weight = check_plain($form_state['values']['role_redirect_weight_' . $key]);
$path = $form_state['values']['role_redirect_path_' . $key];
$result = user_login_redirect_insert_data($key, $weight, $path);
if (!is_null($result)) {
drupal_set_message(t('The configuration for the role "@role" has been saved.', array('@role' => $value)));
}
else {
drupal_set_message(t('There was an error and the configuration of the role "@role" could not be saved.', array('@role' => $value)), 'error');
}
}
elseif ($redirect_data['path'] != $form_state['values']['role_redirect_path_' . $key]
|| $redirect_data['weight'] != $form_state['values']['role_redirect_weight_' . $key]) {
$weight = check_plain($form_state['values']['role_redirect_weight_' . $key]);
$path = $form_state['values']['role_redirect_path_' . $key];
$result = user_login_redirect_update_data($key, $weight, $path);
if ($result) {
drupal_set_message(t('The configuration of the role "@role" has been updated correctly.', array('@role' => $value)));
}
else {
drupal_set_message(t('There was an error during the update, the configuration of the role "@role" could not be saved.', array('@role' => $value)), 'error');
}
}
}
else {
$redirect_data = user_login_redirect_get_data_by_rid($key);
if (!empty($redirect_data) && $redirect_data['path']) {
$result = user_login_redirect_delete_data_by_rid($key);
if ($result) {
drupal_set_message(t('The configuration of the role "@role" has been deleted .', array('@role' => $value)));
}
else {
drupal_set_message(t('The configuration of the role "@role" could not be deleted.', array('@role' => $value)), 'error');
}
}
}
}
}
/**
* Retrive specific rid info.
*/
function user_login_redirect_get_data_by_rid($rid) {
$query = db_select('user_login_redirect', 'u')
->fields('u', array('path', 'weight'))
->condition('rid', $rid, '=');
$result = $query->execute();
return $result->fetchAssoc();
}
/**
* Update specific rid info.
*/
function user_login_redirect_update_data($rid, $weight, $path) {
$update = db_update('user_login_redirect')
->fields(array(
'weight' => (int) $weight,
'path' => $path,
))
->condition('rid', $rid, '=')
->execute();
return $update;
}
/**
* Delete specific rid info.
*/
function user_login_redirect_delete_data_by_rid($rid) {
$delete = db_delete('user_login_redirect')
->condition('rid', $rid)
->execute();
return $delete;
}
/**
* Insert Redirect Data.
*/
function user_login_redirect_insert_data($rid, $weight, $path) {
$insert = db_insert('user_login_redirect')
->fields(array(
'rid' => $rid,
'weight' => (int) $weight,
'path' => $path,
))
->execute();
return $insert;
}
/**
* Return all roles redirect info.
*/
function user_login_redirect_get_all_data() {
$query = db_select('user_login_redirect', 'u')
->fields('u', array('rid', 'path', 'weight'));
$result = $query->execute();
return $result->fetchAllAssoc('rid');
}
/**
* Validate for user_login_redirect_config_form.
*/
function user_login_redirect_config_form_validate($form, &$form_state) {
$roles = user_roles(TRUE);
$data = $form_state['values'];
foreach ($roles as $key => $value) {
if (!empty($data['role_redirect_path_' . $key])) {
if (!valid_url($data['role_redirect_path_' . $key])) {
form_set_error('role_redirect_path_' . $key . '][', t('You must enter a relative path for the role @role', array('@role' => $value)));
}
}
}
}
/**
* Implements hook_user_login().
*/
function user_login_redirect_user_login(&$edit, $account) {
$wrapper = entity_metadata_wrapper('user', $account);
$configured_roles = user_login_redirect_get_all_data();
$user_roles = $wrapper->roles->value();
$roles_redirect = array();
foreach ($user_roles as $rid) {
if (!empty($configured_roles[$rid])) {
$roles_redirect[$rid] = array(
'weight' => $configured_roles[$rid]->weight,
'path' => $configured_roles[$rid]->path,
);
}
}
$role_weights = array();
foreach ($roles_redirect as $role) {
$role_weights[] = $role['weight'];
}
$min_weight = min($role_weights);
$redirect_path = '';
foreach ($roles_redirect as $role) {
if ($role['weight'] == $min_weight) {
$redirect_path = $role['path'];
}
}
$edit['redirect'] = $redirect_path;
unset($_GET['destination']);
drupal_static_reset('drupal_get_destination');
drupal_get_destination();
}