-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathding_user.pages.inc
More file actions
106 lines (94 loc) · 3.18 KB
/
ding_user.pages.inc
File metadata and controls
106 lines (94 loc) · 3.18 KB
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
<?php
/**
* @file
* Ding user pages.
*/
/**
* Form builder, user authentication.
*
* This copies the regular user login form on purpose. By making it
* look like the regular login form, modules wanting to make changes
* can just form_alter them both.
*
* @ingroup forms
*/
function ding_user_authenticate_form($form, &$form_state) {
form_load_include($form_state, 'inc', 'ding_user', 'ding_user.pages');
global $user;
try {
$creds = ding_user_get_creds();
// If we are already authenticated on, go to the user page instead.
if (ding_provider_implements('user', 'is_authenticated')) {
if (ding_provider_invoke('user', 'is_authenticated', $creds)) {
drupal_goto('user/' . $user->uid);
}
}
}
catch (DingProviderAuthException $e) {
// Fall through.
}
// Display login form:
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Username'),
'#size' => 60,
'#maxlength' => USERNAME_MAX_LENGTH,
'#required' => TRUE,
);
$form['name']['#description'] = t('Enter your @s username.', array('@s' => variable_get('site_name', 'Drupal')));
$form['pass'] = array(
'#type' => 'password',
'#title' => t('Password'),
'#description' => t('Enter the password that accompanies your username.'),
'#required' => TRUE,
);
// This is where we diverge from the regular login form. We're not
// specifying any special validators, but relying on the normal behaviour.
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Log in'),
);
return $form;
}
/**
* Form validator.
*/
function ding_user_authenticate_form_validate($form, &$form_state) {
global $user;
$auth_res = ding_provider_invoke('user', 'authenticate', $form_state['values']['name'], $form_state['values']['pass']);
if (!is_array($auth_res) || !isset($auth_res['success'])) {
watchdog('ding_user', 'Provider returned invalid result: @res', array('@res' => print_r($auth_res, TRUE)), WATCHDOG_DEBUG);
form_error($form, t("Internal error, please try again later."));
return;
}
$form_state['authentication_success'] = FALSE;
if ($auth_res['success']) {
if (isset($auth_res['authname']) && !empty($auth_res['authname'])) {
// If provider supplied an authname, use it.
$auth_name = $auth_res['authname'];
}
else {
// Else use a standard authname.
$auth_name = ding_user_default_authname($form_state['values']['name']);
}
$account = user_external_load($auth_name);
if ($account && ($account->uid == $user->uid)) {
$form_state['authentication_success'] = TRUE;
$form_state['auth_res'] = $auth_res;
}
}
if (!$form_state['authentication_success']) {
form_set_error('name', t('Sorry, authentication failed, did you type the right username and password?'));
watchdog('user', 'Authentication attempt failed for %uid.', array('%user' => $user->uid));
}
}
/**
* Form submit function.
*/
function ding_user_authenticate_form_submit($form, &$form_state) {
if ($form_state['authentication_success']) {
ding_user_save_creds($form_state['auth_res']);
$form_state['redirect'] = 'user/' . $user->uid;
}
}