Elgg  Version 6.0
login_as.php
Go to the documentation of this file.
1 <?php
9 
10 $user_guid = (int) get_input('user_guid');
11 
13 if (!$user instanceof \ElggUser) {
14  return elgg_error_response(elgg_echo('action:user:login_as:unknown'));
15 }
16 
19 
20 // store the original persistent login state to restore on logout_as.
21 $persistent = false;
22 if (isset($_COOKIE['elggperm'])) {
23  $original_perm_user = elgg_get_user_by_persistent_token($_COOKIE['elggperm']);
24  if ($original_perm_user instanceof \ElggUser && $original_user->guid === $original_perm_user->guid) {
25  $persistent = true;
26  }
27 }
28 
30 $session->set('login_as_original_user_guid', $original_user_guid);
31 $session->set('login_as_original_persistent', $persistent);
32 
33 try {
35 
36  return elgg_ok_response('', elgg_echo('action:user:login_as:success', [$user->getDisplayName()]));
37 } catch (LoginException $exc) {
38  $session->remove('login_as_original_user_guid');
39  $session->remove('login_as_original_persistent');
40 
41  try {
43  } catch (LoginException $ex) {
44  // we can't log back in as ourselves? just leave us logged out then...
45  }
46 
47  return elgg_error_response(elgg_echo('action:user:login_as:error', [$user->getDisplayName()]));
48 }
$original_user_guid
Definition: login_as.php:18
$user_guid
Definition: login_as.php:10
Generic parent class for login exceptions.
elgg_ok_response($content= '', string|array $message= '', string $forward_url=null, int $status_code=ELGG_HTTP_OK)
Prepares a successful response to be returned by a page or an action handler.
elgg_get_user_by_persistent_token(string $token)
Get a user based on a persistent login token.
Definition: users.php:252
elgg_get_session()
Gets Elgg&#39;s session object.
Definition: sessions.php:15
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
if(!$user instanceof\ElggUser) $original_user
Definition: login_as.php:17
get_input(string $variable, $default=null, bool $filter_result=true)
Parameter input functions.
Definition: input.php:20
elgg_error_response(string|array $message= '', string $forward_url=REFERRER, int $status_code=ELGG_HTTP_BAD_REQUEST)
Prepare an error response to be returned by a page or an action handler.
$user
Definition: login_as.php:12
get_user(int $guid)
Elgg users Functions to manage multiple or single users in an Elgg install.
Definition: users.php:16
if(isset($_COOKIE['elggperm'])) $session
Definition: login_as.php:29
elgg_login(\ElggUser $user, bool $persistent=false)
Log in a user.
Definition: sessions.php:81
$persistent
Definition: login_as.php:21
elgg_get_logged_in_user_entity()
Return the current logged in user, or null if no user is logged in.
Definition: sessions.php:24
Login as the specified user.