1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* User page callback file for the user module.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Menu callback; Retrieve a JSON object containing autocomplete suggestions for existing users.
|
10 |
|
|
*/
|
11 |
|
|
function user_autocomplete($string = '') {
|
12 |
|
|
$matches = array();
|
13 |
|
|
if ($string) {
|
14 |
|
|
$result = db_select('users')->fields('users', array('name'))->condition('name', db_like($string) . '%', 'LIKE')->range(0, 10)->execute();
|
15 |
|
|
foreach ($result as $user) {
|
16 |
|
|
$matches[$user->name] = check_plain($user->name);
|
17 |
|
|
}
|
18 |
|
|
}
|
19 |
|
|
|
20 |
|
|
drupal_json_output($matches);
|
21 |
|
|
}
|
22 |
|
|
|
23 |
|
|
/**
|
24 |
|
|
* Form builder; Request a password reset.
|
25 |
|
|
*
|
26 |
|
|
* @ingroup forms
|
27 |
|
|
* @see user_pass_validate()
|
28 |
|
|
* @see user_pass_submit()
|
29 |
|
|
*/
|
30 |
|
|
function user_pass() {
|
31 |
|
|
global $user;
|
32 |
|
|
|
33 |
|
|
$form['name'] = array(
|
34 |
|
|
'#type' => 'textfield',
|
35 |
|
|
'#title' => t('Username or e-mail address'),
|
36 |
|
|
'#size' => 60,
|
37 |
|
|
'#maxlength' => max(USERNAME_MAX_LENGTH, EMAIL_MAX_LENGTH),
|
38 |
|
|
'#required' => TRUE,
|
39 |
|
|
'#default_value' => isset($_GET['name']) ? $_GET['name'] : '',
|
40 |
|
|
);
|
41 |
|
|
// Allow logged in users to request this also.
|
42 |
|
|
if ($user->uid > 0) {
|
43 |
|
|
$form['name']['#type'] = 'value';
|
44 |
|
|
$form['name']['#value'] = $user->mail;
|
45 |
|
|
$form['mail'] = array(
|
46 |
|
|
'#prefix' => '<p>',
|
47 |
b0dc3a2e
|
Julien Enselme
|
// As of https://www.drupal.org/node/889772 the user no longer must log
|
48 |
|
|
// out (if they are still logged in when using the password reset link,
|
49 |
|
|
// they will be logged out automatically then), but this text is kept as
|
50 |
|
|
// is to avoid breaking translations as well as to encourage the user to
|
51 |
|
|
// log out manually at a time of their own choosing (when it will not
|
52 |
|
|
// interrupt anything else they may have been in the middle of doing).
|
53 |
85ad3d82
|
Assos Assos
|
'#markup' => t('Password reset instructions will be mailed to %email. You must log out to use the password reset link in the e-mail.', array('%email' => $user->mail)),
|
54 |
|
|
'#suffix' => '</p>',
|
55 |
|
|
);
|
56 |
|
|
}
|
57 |
|
|
$form['actions'] = array('#type' => 'actions');
|
58 |
|
|
$form['actions']['submit'] = array('#type' => 'submit', '#value' => t('E-mail new password'));
|
59 |
|
|
|
60 |
|
|
return $form;
|
61 |
|
|
}
|
62 |
|
|
|
63 |
b0dc3a2e
|
Julien Enselme
|
/**
|
64 |
|
|
* Form validation handler for user_pass().
|
65 |
|
|
*
|
66 |
|
|
* @see user_pass_submit()
|
67 |
|
|
*/
|
68 |
85ad3d82
|
Assos Assos
|
function user_pass_validate($form, &$form_state) {
|
69 |
01dfd3b5
|
Assos Assos
|
if (isset($form_state['values']['name']) && !is_scalar($form_state['values']['name'])) {
|
70 |
|
|
form_set_error('name', t('An illegal value has been detected. Please contact the site administrator.'));
|
71 |
|
|
return;
|
72 |
|
|
}
|
73 |
|
|
$user_pass_reset_ip_window = variable_get('user_pass_reset_ip_window', 3600);
|
74 |
|
|
// Do not allow any password reset from the current user's IP if the limit
|
75 |
|
|
// has been reached. Default is 50 attempts allowed in one hour. This is
|
76 |
|
|
// independent of the per-user limit to catch attempts from one IP to request
|
77 |
|
|
// resets for many different user accounts. We have a reasonably high limit
|
78 |
|
|
// since there may be only one apparent IP for all users at an institution.
|
79 |
|
|
if (!flood_is_allowed('pass_reset_ip', variable_get('user_pass_reset_ip_limit', 50), $user_pass_reset_ip_window)) {
|
80 |
|
|
form_set_error('name', t('Sorry, too many password reset attempts from your IP address. This IP address is temporarily blocked. Try again later or <a href="@url">request a new password</a>.', array('@url' => url('user/password'))));
|
81 |
|
|
return;
|
82 |
|
|
}
|
83 |
|
|
// Always register an per-IP event.
|
84 |
|
|
flood_register_event('pass_reset_ip', $user_pass_reset_ip_window);
|
85 |
85ad3d82
|
Assos Assos
|
$name = trim($form_state['values']['name']);
|
86 |
|
|
// Try to load by email.
|
87 |
|
|
$users = user_load_multiple(array(), array('mail' => $name, 'status' => '1'));
|
88 |
|
|
$account = reset($users);
|
89 |
|
|
if (!$account) {
|
90 |
|
|
// No success, try to load by name.
|
91 |
|
|
$users = user_load_multiple(array(), array('name' => $name, 'status' => '1'));
|
92 |
|
|
$account = reset($users);
|
93 |
|
|
}
|
94 |
|
|
if (isset($account->uid)) {
|
95 |
01dfd3b5
|
Assos Assos
|
// Register user flood events based on the uid only, so they can be cleared
|
96 |
|
|
// when a password is reset successfully.
|
97 |
|
|
$identifier = $account->uid;
|
98 |
|
|
$user_pass_reset_user_window = variable_get('user_pass_reset_user_window', 21600);
|
99 |
|
|
$user_pass_reset_user_limit = variable_get('user_pass_reset_user_limit', 5);
|
100 |
|
|
// Don't allow password reset if the limit for this user has been reached.
|
101 |
|
|
// Default is to allow 5 passwords resets every 6 hours.
|
102 |
|
|
if (!flood_is_allowed('pass_reset_user', $user_pass_reset_user_limit, $user_pass_reset_user_window, $identifier)) {
|
103 |
|
|
form_set_error('name', format_plural($user_pass_reset_user_limit, 'Sorry, there has been more than one password reset attempt for this account. It is temporarily blocked. Try again later or <a href="@url">login with your password</a>.', 'Sorry, there have been more than @count password reset attempts for this account. It is temporarily blocked. Try again later or <a href="@url">login with your password</a>.', array('@url' => url('user/login'))));
|
104 |
|
|
return;
|
105 |
|
|
}
|
106 |
|
|
// Register a per-user event.
|
107 |
|
|
flood_register_event('pass_reset_user', $user_pass_reset_user_window, $identifier);
|
108 |
85ad3d82
|
Assos Assos
|
form_set_value(array('#parents' => array('account')), $account, $form_state);
|
109 |
|
|
}
|
110 |
|
|
else {
|
111 |
|
|
form_set_error('name', t('Sorry, %name is not recognized as a user name or an e-mail address.', array('%name' => $name)));
|
112 |
|
|
}
|
113 |
|
|
}
|
114 |
|
|
|
115 |
b0dc3a2e
|
Julien Enselme
|
/**
|
116 |
|
|
* Form submission handler for user_pass().
|
117 |
|
|
*
|
118 |
|
|
* @see user_pass_validate()
|
119 |
|
|
*/
|
120 |
85ad3d82
|
Assos Assos
|
function user_pass_submit($form, &$form_state) {
|
121 |
|
|
global $language;
|
122 |
|
|
|
123 |
|
|
$account = $form_state['values']['account'];
|
124 |
|
|
// Mail one time login URL and instructions using current language.
|
125 |
|
|
$mail = _user_mail_notify('password_reset', $account, $language);
|
126 |
|
|
if (!empty($mail)) {
|
127 |
|
|
watchdog('user', 'Password reset instructions mailed to %name at %email.', array('%name' => $account->name, '%email' => $account->mail));
|
128 |
|
|
drupal_set_message(t('Further instructions have been sent to your e-mail address.'));
|
129 |
|
|
}
|
130 |
|
|
|
131 |
|
|
$form_state['redirect'] = 'user';
|
132 |
|
|
return;
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
/**
|
136 |
|
|
* Menu callback; process one time login link and redirects to the user page on success.
|
137 |
|
|
*/
|
138 |
|
|
function user_pass_reset($form, &$form_state, $uid, $timestamp, $hashed_pass, $action = NULL) {
|
139 |
|
|
global $user;
|
140 |
|
|
|
141 |
|
|
// When processing the one-time login link, we have to make sure that a user
|
142 |
|
|
// isn't already logged in.
|
143 |
|
|
if ($user->uid) {
|
144 |
b0dc3a2e
|
Julien Enselme
|
// The existing user is already logged in. Log them out and reload the
|
145 |
|
|
// current page so the password reset process can continue.
|
146 |
85ad3d82
|
Assos Assos
|
if ($user->uid == $uid) {
|
147 |
b0dc3a2e
|
Julien Enselme
|
// Preserve the current destination (if any) and ensure the redirect goes
|
148 |
|
|
// back to the current page; any custom destination set in
|
149 |
|
|
// hook_user_logout() and intended for regular logouts would not be
|
150 |
|
|
// appropriate here.
|
151 |
|
|
$destination = array();
|
152 |
|
|
if (isset($_GET['destination'])) {
|
153 |
|
|
$destination = drupal_get_destination();
|
154 |
|
|
}
|
155 |
|
|
user_logout_current_user();
|
156 |
|
|
unset($_GET['destination']);
|
157 |
|
|
drupal_goto(current_path(), array('query' => drupal_get_query_parameters() + $destination));
|
158 |
85ad3d82
|
Assos Assos
|
}
|
159 |
|
|
// A different user is already logged in on the computer.
|
160 |
|
|
else {
|
161 |
|
|
$reset_link_account = user_load($uid);
|
162 |
|
|
if (!empty($reset_link_account)) {
|
163 |
|
|
drupal_set_message(t('Another user (%other_user) is already logged into the site on this computer, but you tried to use a one-time link for user %resetting_user. Please <a href="!logout">logout</a> and try using the link again.',
|
164 |
582db59d
|
Assos Assos
|
array('%other_user' => $user->name, '%resetting_user' => $reset_link_account->name, '!logout' => url('user/logout'))), 'warning');
|
165 |
85ad3d82
|
Assos Assos
|
} else {
|
166 |
|
|
// Invalid one-time link specifies an unknown user.
|
167 |
582db59d
|
Assos Assos
|
drupal_set_message(t('The one-time login link you clicked is invalid.'), 'error');
|
168 |
85ad3d82
|
Assos Assos
|
}
|
169 |
b0dc3a2e
|
Julien Enselme
|
drupal_goto();
|
170 |
85ad3d82
|
Assos Assos
|
}
|
171 |
|
|
}
|
172 |
|
|
else {
|
173 |
|
|
// Time out, in seconds, until login URL expires. Defaults to 24 hours =
|
174 |
|
|
// 86400 seconds.
|
175 |
|
|
$timeout = variable_get('user_password_reset_timeout', 86400);
|
176 |
|
|
$current = REQUEST_TIME;
|
177 |
|
|
// Some redundant checks for extra security ?
|
178 |
|
|
$users = user_load_multiple(array($uid), array('status' => '1'));
|
179 |
|
|
if ($timestamp <= $current && $account = reset($users)) {
|
180 |
|
|
// No time out for first time login.
|
181 |
|
|
if ($account->login && $current - $timestamp > $timeout) {
|
182 |
582db59d
|
Assos Assos
|
drupal_set_message(t('You have tried to use a one-time login link that has expired. Please request a new one using the form below.'), 'error');
|
183 |
85ad3d82
|
Assos Assos
|
drupal_goto('user/password');
|
184 |
|
|
}
|
185 |
bceb9b7a
|
Florent Torregrosa
|
elseif ($account->uid && $timestamp >= $account->login && $timestamp <= $current && $hashed_pass == user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid)) {
|
186 |
85ad3d82
|
Assos Assos
|
// First stage is a confirmation form, then login
|
187 |
|
|
if ($action == 'login') {
|
188 |
|
|
// Set the new user.
|
189 |
|
|
$user = $account;
|
190 |
|
|
// user_login_finalize() also updates the login timestamp of the
|
191 |
|
|
// user, which invalidates further use of the one-time login link.
|
192 |
|
|
user_login_finalize();
|
193 |
01dfd3b5
|
Assos Assos
|
// Clear any password reset flood events for this user.
|
194 |
|
|
flood_clear_event('pass_reset_user', $account->uid);
|
195 |
85ad3d82
|
Assos Assos
|
watchdog('user', 'User %name used one-time login link at time %timestamp.', array('%name' => $account->name, '%timestamp' => $timestamp));
|
196 |
|
|
drupal_set_message(t('You have just used your one-time login link. It is no longer necessary to use this link to log in. Please change your password.'));
|
197 |
|
|
// Let the user's password be changed without the current password check.
|
198 |
|
|
$token = drupal_random_key();
|
199 |
|
|
$_SESSION['pass_reset_' . $user->uid] = $token;
|
200 |
|
|
drupal_goto('user/' . $user->uid . '/edit', array('query' => array('pass-reset-token' => $token)));
|
201 |
|
|
}
|
202 |
|
|
else {
|
203 |
|
|
$form['message'] = array('#markup' => t('<p>This is a one-time login for %user_name and will expire on %expiration_date.</p><p>Click on this button to log in to the site and change your password.</p>', array('%user_name' => $account->name, '%expiration_date' => format_date($timestamp + $timeout))));
|
204 |
|
|
$form['help'] = array('#markup' => '<p>' . t('This login can be used only once.') . '</p>');
|
205 |
|
|
$form['actions'] = array('#type' => 'actions');
|
206 |
|
|
$form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Log in'));
|
207 |
|
|
$form['#action'] = url("user/reset/$uid/$timestamp/$hashed_pass/login");
|
208 |
|
|
return $form;
|
209 |
|
|
}
|
210 |
|
|
}
|
211 |
|
|
else {
|
212 |
582db59d
|
Assos Assos
|
drupal_set_message(t('You have tried to use a one-time login link that has either been used or is no longer valid. Please request a new one using the form below.'), 'error');
|
213 |
85ad3d82
|
Assos Assos
|
drupal_goto('user/password');
|
214 |
|
|
}
|
215 |
|
|
}
|
216 |
|
|
else {
|
217 |
|
|
// Deny access, no more clues.
|
218 |
|
|
// Everything will be in the watchdog's URL for the administrator to check.
|
219 |
|
|
drupal_access_denied();
|
220 |
|
|
drupal_exit();
|
221 |
|
|
}
|
222 |
|
|
}
|
223 |
|
|
}
|
224 |
|
|
|
225 |
|
|
/**
|
226 |
|
|
* Menu callback; logs the current user out, and redirects to the home page.
|
227 |
|
|
*/
|
228 |
|
|
function user_logout() {
|
229 |
b0dc3a2e
|
Julien Enselme
|
user_logout_current_user();
|
230 |
|
|
drupal_goto();
|
231 |
|
|
}
|
232 |
|
|
|
233 |
|
|
/**
|
234 |
|
|
* Logs the current user out.
|
235 |
|
|
*/
|
236 |
|
|
function user_logout_current_user() {
|
237 |
85ad3d82
|
Assos Assos
|
global $user;
|
238 |
|
|
|
239 |
|
|
watchdog('user', 'Session closed for %name.', array('%name' => $user->name));
|
240 |
|
|
|
241 |
|
|
module_invoke_all('user_logout', $user);
|
242 |
|
|
|
243 |
|
|
// Destroy the current session, and reset $user to the anonymous user.
|
244 |
|
|
session_destroy();
|
245 |
|
|
}
|
246 |
|
|
|
247 |
|
|
/**
|
248 |
|
|
* Process variables for user-profile.tpl.php.
|
249 |
|
|
*
|
250 |
e33d3026
|
Julien Enselme
|
* @param array $variables
|
251 |
|
|
* An associative array containing:
|
252 |
|
|
* - elements: An associative array containing the user information and any
|
253 |
|
|
* fields attached to the user. Properties used:
|
254 |
|
|
* - #account: The user account of the profile being viewed.
|
255 |
85ad3d82
|
Assos Assos
|
*
|
256 |
|
|
* @see user-profile.tpl.php
|
257 |
|
|
*/
|
258 |
|
|
function template_preprocess_user_profile(&$variables) {
|
259 |
|
|
$account = $variables['elements']['#account'];
|
260 |
|
|
|
261 |
|
|
// Helpful $user_profile variable for templates.
|
262 |
|
|
foreach (element_children($variables['elements']) as $key) {
|
263 |
|
|
$variables['user_profile'][$key] = $variables['elements'][$key];
|
264 |
|
|
}
|
265 |
|
|
|
266 |
|
|
// Preprocess fields.
|
267 |
|
|
field_attach_preprocess('user', $account, $variables['elements'], $variables);
|
268 |
|
|
}
|
269 |
|
|
|
270 |
|
|
/**
|
271 |
|
|
* Process variables for user-profile-item.tpl.php.
|
272 |
|
|
*
|
273 |
|
|
* The $variables array contains the following arguments:
|
274 |
|
|
* - $element
|
275 |
|
|
*
|
276 |
|
|
* @see user-profile-item.tpl.php
|
277 |
|
|
*/
|
278 |
|
|
function template_preprocess_user_profile_item(&$variables) {
|
279 |
|
|
$variables['title'] = $variables['element']['#title'];
|
280 |
|
|
$variables['value'] = $variables['element']['#markup'];
|
281 |
|
|
$variables['attributes'] = '';
|
282 |
|
|
if (isset($variables['element']['#attributes'])) {
|
283 |
|
|
$variables['attributes'] = drupal_attributes($variables['element']['#attributes']);
|
284 |
|
|
}
|
285 |
|
|
}
|
286 |
|
|
|
287 |
|
|
/**
|
288 |
|
|
* Process variables for user-profile-category.tpl.php.
|
289 |
|
|
*
|
290 |
|
|
* The $variables array contains the following arguments:
|
291 |
|
|
* - $element
|
292 |
|
|
*
|
293 |
|
|
* @see user-profile-category.tpl.php
|
294 |
|
|
*/
|
295 |
|
|
function template_preprocess_user_profile_category(&$variables) {
|
296 |
|
|
$variables['title'] = check_plain($variables['element']['#title']);
|
297 |
|
|
$variables['profile_items'] = $variables['element']['#children'];
|
298 |
|
|
$variables['attributes'] = '';
|
299 |
|
|
if (isset($variables['element']['#attributes'])) {
|
300 |
|
|
$variables['attributes'] = drupal_attributes($variables['element']['#attributes']);
|
301 |
|
|
}
|
302 |
|
|
}
|
303 |
|
|
|
304 |
|
|
/**
|
305 |
|
|
* Form builder; edit a user account or one of their profile categories.
|
306 |
|
|
*
|
307 |
|
|
* @ingroup forms
|
308 |
|
|
* @see user_account_form()
|
309 |
|
|
* @see user_account_form_validate()
|
310 |
|
|
* @see user_profile_form_validate()
|
311 |
|
|
* @see user_profile_form_submit()
|
312 |
|
|
* @see user_cancel_confirm_form_submit()
|
313 |
|
|
*/
|
314 |
|
|
function user_profile_form($form, &$form_state, $account, $category = 'account') {
|
315 |
|
|
global $user;
|
316 |
|
|
|
317 |
|
|
// During initial form build, add the entity to the form state for use during
|
318 |
|
|
// form building and processing. During a rebuild, use what is in the form
|
319 |
|
|
// state.
|
320 |
|
|
if (!isset($form_state['user'])) {
|
321 |
|
|
$form_state['user'] = $account;
|
322 |
|
|
}
|
323 |
|
|
else {
|
324 |
|
|
$account = $form_state['user'];
|
325 |
|
|
}
|
326 |
|
|
|
327 |
|
|
// @todo Legacy support. Modules are encouraged to access the entity using
|
328 |
|
|
// $form_state. Remove in Drupal 8.
|
329 |
|
|
$form['#user'] = $account;
|
330 |
|
|
$form['#user_category'] = $category;
|
331 |
|
|
|
332 |
|
|
if ($category == 'account') {
|
333 |
|
|
user_account_form($form, $form_state);
|
334 |
|
|
// Attach field widgets.
|
335 |
|
|
$langcode = entity_language('user', $account);
|
336 |
|
|
field_attach_form('user', $account, $form, $form_state, $langcode);
|
337 |
|
|
}
|
338 |
|
|
|
339 |
|
|
$form['actions'] = array('#type' => 'actions');
|
340 |
|
|
$form['actions']['submit'] = array(
|
341 |
|
|
'#type' => 'submit',
|
342 |
|
|
'#value' => t('Save'),
|
343 |
|
|
);
|
344 |
|
|
if ($category == 'account') {
|
345 |
|
|
$form['actions']['cancel'] = array(
|
346 |
|
|
'#type' => 'submit',
|
347 |
|
|
'#value' => t('Cancel account'),
|
348 |
|
|
'#submit' => array('user_edit_cancel_submit'),
|
349 |
|
|
'#access' => $account->uid > 1 && (($account->uid == $user->uid && user_access('cancel account')) || user_access('administer users')),
|
350 |
|
|
);
|
351 |
|
|
}
|
352 |
|
|
|
353 |
|
|
$form['#validate'][] = 'user_profile_form_validate';
|
354 |
|
|
// Add the final user profile form submit handler.
|
355 |
|
|
$form['#submit'][] = 'user_profile_form_submit';
|
356 |
|
|
|
357 |
|
|
return $form;
|
358 |
|
|
}
|
359 |
|
|
|
360 |
|
|
/**
|
361 |
b0dc3a2e
|
Julien Enselme
|
* Form validation handler for user_profile_form().
|
362 |
|
|
*
|
363 |
|
|
* @see user_profile_form_submit()
|
364 |
85ad3d82
|
Assos Assos
|
*/
|
365 |
|
|
function user_profile_form_validate($form, &$form_state) {
|
366 |
|
|
entity_form_field_validate('user', $form, $form_state);
|
367 |
|
|
}
|
368 |
|
|
|
369 |
|
|
/**
|
370 |
b0dc3a2e
|
Julien Enselme
|
* Form submission handler for user_profile_form().
|
371 |
|
|
*
|
372 |
|
|
* @see user_profile_form_validate()
|
373 |
85ad3d82
|
Assos Assos
|
*/
|
374 |
|
|
function user_profile_form_submit($form, &$form_state) {
|
375 |
|
|
$account = $form_state['user'];
|
376 |
|
|
$category = $form['#user_category'];
|
377 |
|
|
// Remove unneeded values.
|
378 |
|
|
form_state_values_clean($form_state);
|
379 |
|
|
|
380 |
|
|
// Before updating the account entity, keep an unchanged copy for use with
|
381 |
|
|
// user_save() later. This is necessary for modules implementing the user
|
382 |
|
|
// hooks to be able to react on changes by comparing the values of $account
|
383 |
|
|
// and $edit.
|
384 |
|
|
$account_unchanged = clone $account;
|
385 |
|
|
|
386 |
|
|
entity_form_submit_build_entity('user', $account, $form, $form_state);
|
387 |
|
|
|
388 |
|
|
// Populate $edit with the properties of $account, which have been edited on
|
389 |
|
|
// this form by taking over all values, which appear in the form values too.
|
390 |
|
|
$edit = array_intersect_key((array) $account, $form_state['values']);
|
391 |
|
|
|
392 |
|
|
user_save($account_unchanged, $edit, $category);
|
393 |
|
|
$form_state['values']['uid'] = $account->uid;
|
394 |
|
|
|
395 |
|
|
if ($category == 'account' && !empty($edit['pass'])) {
|
396 |
|
|
// Remove the password reset tag since a new password was saved.
|
397 |
|
|
unset($_SESSION['pass_reset_'. $account->uid]);
|
398 |
|
|
}
|
399 |
|
|
// Clear the page cache because pages can contain usernames and/or profile information:
|
400 |
|
|
cache_clear_all();
|
401 |
|
|
|
402 |
|
|
drupal_set_message(t('The changes have been saved.'));
|
403 |
|
|
}
|
404 |
|
|
|
405 |
|
|
/**
|
406 |
|
|
* Submit function for the 'Cancel account' button on the user edit form.
|
407 |
|
|
*/
|
408 |
|
|
function user_edit_cancel_submit($form, &$form_state) {
|
409 |
|
|
$destination = array();
|
410 |
|
|
if (isset($_GET['destination'])) {
|
411 |
|
|
$destination = drupal_get_destination();
|
412 |
|
|
unset($_GET['destination']);
|
413 |
|
|
}
|
414 |
|
|
// Note: We redirect from user/uid/edit to user/uid/cancel to make the tabs disappear.
|
415 |
|
|
$form_state['redirect'] = array("user/" . $form['#user']->uid . "/cancel", array('query' => $destination));
|
416 |
|
|
}
|
417 |
|
|
|
418 |
|
|
/**
|
419 |
|
|
* Form builder; confirm form for cancelling user account.
|
420 |
|
|
*
|
421 |
|
|
* @ingroup forms
|
422 |
|
|
* @see user_edit_cancel_submit()
|
423 |
|
|
*/
|
424 |
|
|
function user_cancel_confirm_form($form, &$form_state, $account) {
|
425 |
|
|
global $user;
|
426 |
|
|
|
427 |
|
|
$form['_account'] = array('#type' => 'value', '#value' => $account);
|
428 |
|
|
|
429 |
|
|
// Display account cancellation method selection, if allowed.
|
430 |
|
|
$admin_access = user_access('administer users');
|
431 |
|
|
$can_select_method = $admin_access || user_access('select account cancellation method');
|
432 |
|
|
$form['user_cancel_method'] = array(
|
433 |
|
|
'#type' => 'item',
|
434 |
|
|
'#title' => ($account->uid == $user->uid ? t('When cancelling your account') : t('When cancelling the account')),
|
435 |
|
|
'#access' => $can_select_method,
|
436 |
|
|
);
|
437 |
|
|
$form['user_cancel_method'] += user_cancel_methods();
|
438 |
|
|
|
439 |
|
|
// Allow user administrators to skip the account cancellation confirmation
|
440 |
|
|
// mail (by default), as long as they do not attempt to cancel their own
|
441 |
|
|
// account.
|
442 |
|
|
$override_access = $admin_access && ($account->uid != $user->uid);
|
443 |
|
|
$form['user_cancel_confirm'] = array(
|
444 |
|
|
'#type' => 'checkbox',
|
445 |
|
|
'#title' => t('Require e-mail confirmation to cancel account.'),
|
446 |
|
|
'#default_value' => ($override_access ? FALSE : TRUE),
|
447 |
|
|
'#access' => $override_access,
|
448 |
|
|
'#description' => t('When enabled, the user must confirm the account cancellation via e-mail.'),
|
449 |
|
|
);
|
450 |
|
|
// Also allow to send account canceled notification mail, if enabled.
|
451 |
|
|
$default_notify = variable_get('user_mail_status_canceled_notify', FALSE);
|
452 |
|
|
$form['user_cancel_notify'] = array(
|
453 |
|
|
'#type' => 'checkbox',
|
454 |
|
|
'#title' => t('Notify user when account is canceled.'),
|
455 |
|
|
'#default_value' => ($override_access ? FALSE : $default_notify),
|
456 |
|
|
'#access' => $override_access && $default_notify,
|
457 |
|
|
'#description' => t('When enabled, the user will receive an e-mail notification after the account has been cancelled.'),
|
458 |
|
|
);
|
459 |
|
|
|
460 |
|
|
// Prepare confirmation form page title and description.
|
461 |
|
|
if ($account->uid == $user->uid) {
|
462 |
|
|
$question = t('Are you sure you want to cancel your account?');
|
463 |
|
|
}
|
464 |
|
|
else {
|
465 |
|
|
$question = t('Are you sure you want to cancel the account %name?', array('%name' => $account->name));
|
466 |
|
|
}
|
467 |
|
|
$description = '';
|
468 |
|
|
if ($can_select_method) {
|
469 |
|
|
$description = t('Select the method to cancel the account above.');
|
470 |
|
|
foreach (element_children($form['user_cancel_method']) as $element) {
|
471 |
|
|
unset($form['user_cancel_method'][$element]['#description']);
|
472 |
|
|
}
|
473 |
|
|
}
|
474 |
|
|
else {
|
475 |
|
|
// The radio button #description is used as description for the confirmation
|
476 |
|
|
// form.
|
477 |
|
|
foreach (element_children($form['user_cancel_method']) as $element) {
|
478 |
|
|
if ($form['user_cancel_method'][$element]['#default_value'] == $form['user_cancel_method'][$element]['#return_value']) {
|
479 |
|
|
$description = $form['user_cancel_method'][$element]['#description'];
|
480 |
|
|
}
|
481 |
|
|
unset($form['user_cancel_method'][$element]['#description']);
|
482 |
|
|
}
|
483 |
|
|
}
|
484 |
|
|
|
485 |
|
|
// Always provide entity id in the same form key as in the entity edit form.
|
486 |
|
|
$form['uid'] = array('#type' => 'value', '#value' => $account->uid);
|
487 |
|
|
return confirm_form($form,
|
488 |
|
|
$question,
|
489 |
|
|
'user/' . $account->uid,
|
490 |
|
|
$description . ' ' . t('This action cannot be undone.'),
|
491 |
|
|
t('Cancel account'), t('Cancel'));
|
492 |
|
|
}
|
493 |
|
|
|
494 |
|
|
/**
|
495 |
|
|
* Submit handler for the account cancellation confirm form.
|
496 |
|
|
*
|
497 |
|
|
* @see user_cancel_confirm_form()
|
498 |
|
|
* @see user_multiple_cancel_confirm_submit()
|
499 |
|
|
*/
|
500 |
|
|
function user_cancel_confirm_form_submit($form, &$form_state) {
|
501 |
|
|
global $user;
|
502 |
|
|
$account = $form_state['values']['_account'];
|
503 |
|
|
|
504 |
|
|
// Cancel account immediately, if the current user has administrative
|
505 |
|
|
// privileges, no confirmation mail shall be sent, and the user does not
|
506 |
|
|
// attempt to cancel the own account.
|
507 |
|
|
if (user_access('administer users') && empty($form_state['values']['user_cancel_confirm']) && $account->uid != $user->uid) {
|
508 |
|
|
user_cancel($form_state['values'], $account->uid, $form_state['values']['user_cancel_method']);
|
509 |
|
|
|
510 |
|
|
$form_state['redirect'] = 'admin/people';
|
511 |
|
|
}
|
512 |
|
|
else {
|
513 |
|
|
// Store cancelling method and whether to notify the user in $account for
|
514 |
|
|
// user_cancel_confirm().
|
515 |
|
|
$edit = array(
|
516 |
|
|
'user_cancel_method' => $form_state['values']['user_cancel_method'],
|
517 |
|
|
'user_cancel_notify' => $form_state['values']['user_cancel_notify'],
|
518 |
|
|
);
|
519 |
|
|
$account = user_save($account, $edit);
|
520 |
|
|
_user_mail_notify('cancel_confirm', $account);
|
521 |
|
|
drupal_set_message(t('A confirmation request to cancel your account has been sent to your e-mail address.'));
|
522 |
|
|
watchdog('user', 'Sent account cancellation request to %name %email.', array('%name' => $account->name, '%email' => '<' . $account->mail . '>'), WATCHDOG_NOTICE);
|
523 |
|
|
|
524 |
|
|
$form_state['redirect'] = "user/$account->uid";
|
525 |
|
|
}
|
526 |
|
|
}
|
527 |
|
|
|
528 |
|
|
/**
|
529 |
|
|
* Helper function to return available account cancellation methods.
|
530 |
|
|
*
|
531 |
|
|
* See documentation of hook_user_cancel_methods_alter().
|
532 |
|
|
*
|
533 |
|
|
* @return
|
534 |
|
|
* An array containing all account cancellation methods as form elements.
|
535 |
|
|
*
|
536 |
|
|
* @see hook_user_cancel_methods_alter()
|
537 |
|
|
* @see user_admin_settings()
|
538 |
|
|
* @see user_cancel_confirm_form()
|
539 |
|
|
* @see user_multiple_cancel_confirm()
|
540 |
|
|
*/
|
541 |
|
|
function user_cancel_methods() {
|
542 |
|
|
$methods = array(
|
543 |
|
|
'user_cancel_block' => array(
|
544 |
|
|
'title' => t('Disable the account and keep its content.'),
|
545 |
|
|
'description' => t('Your account will be blocked and you will no longer be able to log in. All of your content will remain attributed to your user name.'),
|
546 |
|
|
),
|
547 |
|
|
'user_cancel_block_unpublish' => array(
|
548 |
|
|
'title' => t('Disable the account and unpublish its content.'),
|
549 |
|
|
'description' => t('Your account will be blocked and you will no longer be able to log in. All of your content will be hidden from everyone but administrators.'),
|
550 |
|
|
),
|
551 |
|
|
'user_cancel_reassign' => array(
|
552 |
|
|
'title' => t('Delete the account and make its content belong to the %anonymous-name user.', array('%anonymous-name' => variable_get('anonymous', t('Anonymous')))),
|
553 |
|
|
'description' => t('Your account will be removed and all account information deleted. All of your content will be assigned to the %anonymous-name user.', array('%anonymous-name' => variable_get('anonymous', t('Anonymous')))),
|
554 |
|
|
),
|
555 |
|
|
'user_cancel_delete' => array(
|
556 |
|
|
'title' => t('Delete the account and its content.'),
|
557 |
|
|
'description' => t('Your account will be removed and all account information deleted. All of your content will also be deleted.'),
|
558 |
|
|
'access' => user_access('administer users'),
|
559 |
|
|
),
|
560 |
|
|
);
|
561 |
|
|
// Allow modules to customize account cancellation methods.
|
562 |
|
|
drupal_alter('user_cancel_methods', $methods);
|
563 |
|
|
|
564 |
|
|
// Turn all methods into real form elements.
|
565 |
|
|
$default_method = variable_get('user_cancel_method', 'user_cancel_block');
|
566 |
|
|
foreach ($methods as $name => $method) {
|
567 |
|
|
$form[$name] = array(
|
568 |
|
|
'#type' => 'radio',
|
569 |
|
|
'#title' => $method['title'],
|
570 |
|
|
'#description' => (isset($method['description']) ? $method['description'] : NULL),
|
571 |
|
|
'#return_value' => $name,
|
572 |
|
|
'#default_value' => $default_method,
|
573 |
|
|
'#parents' => array('user_cancel_method'),
|
574 |
|
|
);
|
575 |
|
|
}
|
576 |
|
|
return $form;
|
577 |
|
|
}
|
578 |
|
|
|
579 |
|
|
/**
|
580 |
|
|
* Menu callback; Cancel a user account via e-mail confirmation link.
|
581 |
|
|
*
|
582 |
|
|
* @see user_cancel_confirm_form()
|
583 |
|
|
* @see user_cancel_url()
|
584 |
|
|
*/
|
585 |
|
|
function user_cancel_confirm($account, $timestamp = 0, $hashed_pass = '') {
|
586 |
|
|
// Time out in seconds until cancel URL expires; 24 hours = 86400 seconds.
|
587 |
|
|
$timeout = 86400;
|
588 |
|
|
$current = REQUEST_TIME;
|
589 |
|
|
|
590 |
|
|
// Basic validation of arguments.
|
591 |
|
|
if (isset($account->data['user_cancel_method']) && !empty($timestamp) && !empty($hashed_pass)) {
|
592 |
|
|
// Validate expiration and hashed password/login.
|
593 |
bceb9b7a
|
Florent Torregrosa
|
if ($timestamp <= $current && $current - $timestamp < $timeout && $account->uid && $timestamp >= $account->login && $hashed_pass == user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid)) {
|
594 |
85ad3d82
|
Assos Assos
|
$edit = array(
|
595 |
|
|
'user_cancel_notify' => isset($account->data['user_cancel_notify']) ? $account->data['user_cancel_notify'] : variable_get('user_mail_status_canceled_notify', FALSE),
|
596 |
|
|
);
|
597 |
|
|
user_cancel($edit, $account->uid, $account->data['user_cancel_method']);
|
598 |
|
|
// Since user_cancel() is not invoked via Form API, batch processing needs
|
599 |
|
|
// to be invoked manually and should redirect to the front page after
|
600 |
|
|
// completion.
|
601 |
|
|
batch_process('');
|
602 |
|
|
}
|
603 |
|
|
else {
|
604 |
582db59d
|
Assos Assos
|
drupal_set_message(t('You have tried to use an account cancellation link that has expired. Please request a new one using the form below.'), 'error');
|
605 |
85ad3d82
|
Assos Assos
|
drupal_goto("user/$account->uid/cancel");
|
606 |
|
|
}
|
607 |
|
|
}
|
608 |
|
|
return MENU_ACCESS_DENIED;
|
609 |
|
|
}
|
610 |
|
|
|
611 |
|
|
/**
|
612 |
|
|
* Page callback: Displays the user page.
|
613 |
|
|
*
|
614 |
|
|
* Displays user profile if user is logged in, or login form for anonymous
|
615 |
|
|
* users.
|
616 |
|
|
*
|
617 |
|
|
* @return
|
618 |
|
|
* A render array for either a user profile or a login form.
|
619 |
|
|
*
|
620 |
|
|
* @see user_view_page()
|
621 |
|
|
* @see user_login()
|
622 |
|
|
*/
|
623 |
|
|
function user_page() {
|
624 |
|
|
global $user;
|
625 |
|
|
if ($user->uid) {
|
626 |
|
|
menu_set_active_item('user/' . $user->uid);
|
627 |
|
|
return menu_execute_active_handler(NULL, FALSE);
|
628 |
|
|
}
|
629 |
|
|
else {
|
630 |
|
|
return drupal_get_form('user_login');
|
631 |
|
|
}
|
632 |
|
|
} |