2007-09-10 13:14:38 +00:00
< ? php
/**
* @ file
* User page callback file for the user module .
*/
2012-07-09 20:20:56 +00:00
use Symfony\Component\HttpFoundation\Request ;
2012-09-20 03:34:47 +00:00
use Symfony\Component\HttpFoundation\RedirectResponse ;
2012-06-04 12:06:09 +00:00
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException ;
2012-07-09 20:20:56 +00:00
use Symfony\Component\HttpKernel\HttpKernelInterface ;
2013-05-02 04:46:53 +00:00
use Drupal\Component\Utility\Crypt ;
2012-06-04 12:06:09 +00:00
2007-09-10 13:14:38 +00:00
/**
* Menu callback ; process one time login link and redirects to the user page on success .
*/
2009-09-18 00:12:48 +00:00
function user_pass_reset ( $form , & $form_state , $uid , $timestamp , $hashed_pass , $action = NULL ) {
2007-09-10 13:14:38 +00:00
global $user ;
2009-08-23 04:50:25 +00:00
// When processing the one-time login link, we have to make sure that a user
// isn't already logged in.
2013-07-11 17:29:02 +00:00
if ( $user -> isAuthenticated ()) {
2009-08-23 04:50:25 +00:00
// The existing user is already logged in.
2013-07-11 17:29:02 +00:00
if ( $user -> id () == $uid ) {
2013-07-24 19:40:03 +00:00
drupal_set_message ( t ( 'You are logged in as %user. <a href="!user_edit">Change your password.</a>' , array ( '%user' => $user -> getUsername (), '!user_edit' => url ( " user/ " . $user -> id () . " /edit " ))));
2009-08-23 04:50:25 +00:00
}
// A different user is already logged in on the computer.
else {
$reset_link_account = user_load ( $uid );
if ( ! empty ( $reset_link_account )) {
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.' ,
2013-07-24 19:40:03 +00:00
array ( '%other_user' => $user -> getUsername (), '%resetting_user' => $reset_link_account -> getUsername (), '!logout' => url ( 'user/logout' ))));
2009-08-23 04:50:25 +00:00
} else {
// Invalid one-time link specifies an unknown user.
drupal_set_message ( t ( 'The one-time login link you clicked is invalid.' ));
}
}
Issue #1668866 by ParisLiakos, aspilicious, tim.plunkett, pdrake, g.oechsler, dawehner, Berdir, corvus_ch, damiankloip, disasm, marcingy, neclimdul: Replace drupal_goto() with RedirectResponse.
2013-06-19 16:07:30 +00:00
return new RedirectResponse ( url ( '<front>' , array ( 'absolute' => TRUE )));
2007-09-10 13:14:38 +00:00
}
else {
2012-11-02 17:35:51 +00:00
// Time out, in seconds, until login URL expires.
2013-09-16 03:58:06 +00:00
$timeout = \Drupal :: config ( 'user.settings' ) -> get ( 'password_reset_timeout' );
2008-09-17 07:11:59 +00:00
$current = REQUEST_TIME ;
2012-08-21 15:38:04 +00:00
$account = user_load ( $uid );
// Verify that the user exists and is active.
2013-07-24 19:40:03 +00:00
if ( $timestamp <= $current && $account && $account -> isActive ()) {
2007-09-10 13:14:38 +00:00
// No time out for first time login.
2013-07-24 19:40:03 +00:00
if ( $account -> getLastLoginTime () && $current - $timestamp > $timeout ) {
2007-09-10 13:14:38 +00:00
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.' ));
Issue #1668866 by ParisLiakos, aspilicious, tim.plunkett, pdrake, g.oechsler, dawehner, Berdir, corvus_ch, damiankloip, disasm, marcingy, neclimdul: Replace drupal_goto() with RedirectResponse.
2013-06-19 16:07:30 +00:00
return new RedirectResponse ( url ( 'user/password' , array ( 'absolute' => TRUE )));
2007-09-10 13:14:38 +00:00
}
2013-07-24 19:40:03 +00:00
elseif ( $account -> isAuthenticated () && $timestamp >= $account -> getLastLoginTime () && $timestamp <= $current && $hashed_pass == user_pass_rehash ( $account -> getPassword (), $timestamp , $account -> getLastLoginTime ())) {
2007-09-10 13:14:38 +00:00
// First stage is a confirmation form, then login
if ( $action == 'login' ) {
2007-12-13 12:53:47 +00:00
// Set the new user.
2009-06-30 11:32:08 +00:00
// user_login_finalize() also updates the login timestamp of the
2007-12-13 12:53:47 +00:00
// user, which invalidates further use of the one-time login link.
2013-06-27 08:23:39 +00:00
user_login_finalize ( $account );
2013-07-24 19:40:03 +00:00
watchdog ( 'user' , 'User %name used one-time login link at time %timestamp.' , array ( '%name' => $account -> getUsername (), '%timestamp' => $timestamp ));
2010-01-11 16:25:16 +00:00
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.' ));
2010-02-11 03:19:21 +00:00
// Let the user's password be changed without the current password check.
2013-05-02 04:46:53 +00:00
$token = Crypt :: randomStringHashed ( 55 );
2013-07-11 17:29:02 +00:00
$_SESSION [ 'pass_reset_' . $user -> id ()] = $token ;
return new RedirectResponse ( url ( 'user/' . $user -> id () . '/edit' , array (
Issue #1668866 by ParisLiakos, aspilicious, tim.plunkett, pdrake, g.oechsler, dawehner, Berdir, corvus_ch, damiankloip, disasm, marcingy, neclimdul: Replace drupal_goto() with RedirectResponse.
2013-06-19 16:07:30 +00:00
'query' => array ( 'pass-reset-token' => $token ),
'absolute' => TRUE ,
)));
2007-09-10 13:14:38 +00:00
}
else {
2013-07-24 19:40:03 +00:00
if ( ! $account -> getLastLoginTime ()) {
2013-03-17 05:42:17 +00:00
// No expiration for first time login.
2013-07-24 19:40:03 +00:00
$form [ 'message' ] = array ( '#markup' => t ( '<p>This is a one-time login for %user_name.</p><p>Click on this button to log in to the site and change your password.</p>' , array ( '%user_name' => $account -> getUsername ())));
2013-03-17 05:42:17 +00:00
}
else {
2013-07-24 19:40:03 +00:00
$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 -> getUsername (), '%expiration_date' => format_date ( $timestamp + $timeout ))));
2013-03-17 05:42:17 +00:00
}
2008-07-16 21:59:29 +00:00
$form [ 'help' ] = array ( '#markup' => '<p>' . t ( 'This login can be used only once.' ) . '</p>' );
2010-04-24 14:49:14 +00:00
$form [ 'actions' ] = array ( '#type' => 'actions' );
$form [ 'actions' ][ 'submit' ] = array ( '#type' => 'submit' , '#value' => t ( 'Log in' ));
2007-09-10 13:14:38 +00:00
$form [ '#action' ] = url ( " user/reset/ $uid / $timestamp / $hashed_pass /login " );
return $form ;
}
}
else {
2010-01-11 16:25:16 +00:00
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.' ));
Issue #1668866 by ParisLiakos, aspilicious, tim.plunkett, pdrake, g.oechsler, dawehner, Berdir, corvus_ch, damiankloip, disasm, marcingy, neclimdul: Replace drupal_goto() with RedirectResponse.
2013-06-19 16:07:30 +00:00
return new RedirectResponse ( url ( 'user/password' , array ( 'absolute' => TRUE )));
2007-09-10 13:14:38 +00:00
}
}
else {
// Deny access, no more clues.
// Everything will be in the watchdog's URL for the administrator to check.
2012-06-04 12:06:09 +00:00
throw new AccessDeniedHttpException ();
2007-09-10 13:14:38 +00:00
}
}
}
/**
2013-05-24 16:55:47 +00:00
* Prepares variables for user templates .
2007-09-10 13:14:38 +00:00
*
2013-05-24 16:55:47 +00:00
* Default template : user . html . twig .
2007-09-10 13:14:38 +00:00
*
2013-05-24 16:55:47 +00:00
* @ param array $variables
* An associative array containing :
* - account : The user account .
2007-09-10 13:14:38 +00:00
*/
2012-12-13 12:11:18 +00:00
function template_preprocess_user ( & $variables ) {
$account = $variables [ 'elements' ][ '#user' ];
2010-01-10 00:41:28 +00:00
2012-12-13 12:11:18 +00:00
// Helpful $content variable for templates.
2009-09-10 12:33:46 +00:00
foreach ( element_children ( $variables [ 'elements' ]) as $key ) {
2012-12-13 12:11:18 +00:00
$variables [ 'content' ][ $key ] = $variables [ 'elements' ][ $key ];
2009-09-10 12:33:46 +00:00
}
2010-01-10 00:41:28 +00:00
// Preprocess fields.
2013-01-07 11:22:28 +00:00
field_attach_preprocess ( $account , $variables [ 'elements' ], $variables );
2013-05-24 16:55:47 +00:00
// Set up attributes.
$variables [ 'attributes' ][ 'class' ][] = 'profile' ;
2007-09-10 13:14:38 +00:00
}
2009-01-08 08:42:13 +00:00
/**
* Menu callback ; Cancel a user account via e - mail confirmation link .
*
* @ see user_cancel_confirm_form ()
* @ see user_cancel_url ()
2013-09-18 18:30:30 +00:00
*
* @ deprecated Use \Drupal\user\Controller\UserController :: confirmCancel ()
2009-01-08 08:42:13 +00:00
*/
function user_cancel_confirm ( $account , $timestamp = 0 , $hashed_pass = '' ) {
// Time out in seconds until cancel URL expires; 24 hours = 86400 seconds.
$timeout = 86400 ;
$current = REQUEST_TIME ;
// Basic validation of arguments.
2013-09-16 03:58:06 +00:00
$account_data = \Drupal :: service ( 'user.data' ) -> get ( 'user' , $account -> id ());
2012-11-27 22:26:22 +00:00
if ( isset ( $account_data [ 'cancel_method' ]) && ! empty ( $timestamp ) && ! empty ( $hashed_pass )) {
2009-01-08 08:42:13 +00:00
// Validate expiration and hashed password/login.
2013-07-24 19:40:03 +00:00
if ( $timestamp <= $current && $current - $timestamp < $timeout && $account -> id () && $timestamp >= $account -> getLastLoginTime () && $hashed_pass == user_pass_rehash ( $account -> getPassword (), $timestamp , $account -> getLastLoginTime ())) {
2009-01-08 08:42:13 +00:00
$edit = array (
2013-09-16 03:58:06 +00:00
'user_cancel_notify' => isset ( $account_data [ 'cancel_notify' ]) ? $account_data [ 'cancel_notify' ] : \Drupal :: config ( 'user.settings' ) -> get ( 'notify.status_canceled' ),
2009-01-08 08:42:13 +00:00
);
2012-11-27 22:26:22 +00:00
user_cancel ( $edit , $account -> id (), $account_data [ 'cancel_method' ]);
2009-01-08 08:42:13 +00:00
// Since user_cancel() is not invoked via Form API, batch processing needs
// to be invoked manually and should redirect to the front page after
// completion.
Issue #1668866 by ParisLiakos, aspilicious, tim.plunkett, pdrake, g.oechsler, dawehner, Berdir, corvus_ch, damiankloip, disasm, marcingy, neclimdul: Replace drupal_goto() with RedirectResponse.
2013-06-19 16:07:30 +00:00
return batch_process ( '' );
2009-01-08 08:42:13 +00:00
}
else {
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.' ));
2013-07-11 17:29:02 +00:00
return new RedirectResponse ( url ( " user/ " . $account -> id () . " /cancel " , array ( 'absolute' => TRUE )));
2009-01-08 08:42:13 +00:00
}
2007-09-10 13:14:38 +00:00
}
2012-06-04 12:06:09 +00:00
throw new AccessDeniedHttpException ();
2007-09-10 13:14:38 +00:00
}