/
user_login_redirect.module
110 lines (102 loc) · 3.27 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
<?php
/**
* @file
* Main file of user_login_redirect module.
*/
/**
* 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 callback' => 'user_access',
'access arguments' => array('configure user login redirect'),
);
return $items;
}
/**
* Implements hook_permission().
*/
function user_login_redirect_permission() {
$permissions['configure user login redirect'] = array(
'title' => t('Configure settings for user login redirect'),
'description' => t('Access to configure user login redirect'),
'restrict access' => TRUE,
);
return $permissions;
}
/**
* Generates config form for this module.
*/
function user_login_redirect_config_form($form, &$form_state) {
$roles = user_roles(TRUE);
foreach ($roles as $rid => $role_name) {
$form['user_login_redirect_weight_' . $rid] = array(
'#type' => 'weight',
'#title' => t('Weight for role @role_name',
array('@role_name' => $role_name)),
'#description' => t('The lower weight, the more importance the role will have'),
'#default_value' => variable_get('user_login_redirect_weight_' . $rid, 0),
);
$form['user_login_redirect_path_' . $rid] = array(
'#type' => 'textfield',
'#title' => t('Redirect path for role @role_name',
array('@role_name' => $role_name)),
'#description' => t('The path to redirect this role.'),
'#default_value' => variable_get('user_login_redirect_path_' . $rid, ''),
);
}
return system_settings_form($form);
}
/**
* Validate function for my config form.
*/
function user_login_redirect_config_form_validate($form, &$form_state) {
$values = $form_state['values'];
$roles = user_roles(TRUE);
foreach ($roles as $rid => $role_name) {
if ($values['user_login_redirect_path_' . $rid] &&
!drupal_valid_path($values['user_login_redirect_path_' . $rid], TRUE)) {
form_set_error('user_login_redirect_path_' . $rid . '][',
t('You must set a valid url for the role @role_name',
array('@role_name' => $role_name)));
}
}
}
/**
* Implements hook_user_login().
*/
function user_login_redirect_user_login(&$edit, $account) {
$redirect_roles = array();
foreach ($account->roles as $role_id => $role_name) {
if ($path = variable_get('user_login_redirect_path_' . $role_id, '')) {
$redirect_roles[$role_id] = array(
'weight' => variable_get('user_login_redirect_weight_' . $role_id, 0),
'path' => $path,
);
}
}
$min_weight = FALSE;
$redirect_path = '';
foreach ($redirect_roles as $role_id => $value) {
if ($min_weight === FALSE || $min_weight > $value['weight']) {
$min_weight = $value['weight'];
$redirect_path = $value['path'];
}
}
$edit['redirect'] = $redirect_path;
unset($_GET['destination']);
drupal_static_reset('drupal_get_destination');
}