Elgg  Version master
login.php
Go to the documentation of this file.
1 <?php
8 
9 $username = get_input('username');
10 $password = get_input('password', null, false);
11 $persistent = (bool) get_input('persistent');
12 $result = false;
13 
14 if (empty($username) || empty($password)) {
16 }
17 
18 // fetch the user (even disabled)
21 });
22 
23 if ($user instanceof \ElggUser) {
24  // could be fetched based on email address
25  $username = $user->username;
26 }
27 
28 try {
29  // try to authenticate
30  $result = elgg_pam_authenticate('user', [
31  'username' => $username,
32  'password' => $password,
33  ]);
34  if ($result !== true) {
35  // was due to missing hash?
36  if ($user && !$user->password_hash) {
37  // if we did this in user password PAM handler, visitors could sniff account usernames from
38  // email addresses. Instead, this lets us give the visitor only the information
39  // they provided.
40  elgg_get_session()->set('forgotpassword:hash_missing', get_input('username'));
41  $output = [
42  'forward' => elgg_generate_url('account:password:reset'),
43  ];
44  return elgg_ok_response($output, '', elgg_generate_url('account:password:reset'));
45  }
46 
47  throw new LoginException(elgg_echo('LoginException:Unknown'));
48  }
49 
50  if (!$user) {
51  throw new LoginException(elgg_echo('login:baduser'));
52  }
53 
56  $prev = $e->getPrevious();
57 
58  $forward = null;
59  if ($prev instanceof LoginException) {
60  $forward = $prev->getRedirectUrl();
61  } elseif ($e instanceof LoginException) {
62  $forward = $e->getRedirectUrl();
63  }
64 
65  // if a forward url is set we need to use a ok response.
66  // The login action is mostly used as an AJAX action and AJAX actions do not support redirects.
67  if (!empty($forward)) {
68  // Registering an error as we use an OK response
69  // It makes no sense for AJAX actions as a OK response with a forward will instantly redirect without time to read the message
70  $error = $e->getMessage();
71  if (!empty($error) && !elgg_is_xhr()) {
73  }
74 
75  return elgg_ok_response('', '', $forward);
76  }
77 
78  return elgg_error_response($e->getMessage(), REFERRER, ELGG_HTTP_UNAUTHORIZED);
79 }
80 
81 if (elgg_is_xhr()) {
82  // Hold the system messages until the client refreshes the page.
83  set_input('elgg_fetch_messages', 0);
84 }
85 
87  'user' => $user,
88 ];
89 $message = elgg_echo('loginok', [], $user->getLanguage(elgg_get_current_language()));
90 
elgg_get_current_language()
Get the current system/user language or &#39;en&#39;.
Definition: languages.php:27
elgg_call(int $flags, Closure $closure)
Calls a callable autowiring the arguments using public DI services and applying logic based on flags...
Definition: elgglib.php:304
Elgg login action.
if(!$deleted) $forward
Definition: delete.php:33
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_username(string $username, bool $try_email=false)
Get a user by username.
Definition: users.php:31
catch(AuthenticationException|LoginException $e) if(elgg_is_xhr()) $message
Definition: login.php:89
Thrown by the AuthenticationService.
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
const ELGG_HTTP_UNAUTHORIZED
Definition: constants.php:65
set_input(string $variable, $value)
Sets an input value that may later be retrieved by get_input.
Definition: input.php:34
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
get_input(string $variable, $default=null, bool $filter_result=true)
Parameter input functions.
Definition: input.php:20
$persistent
Definition: login.php:11
$error
Bad request error.
Definition: 400.php:6
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.
const REFERRER
Used in calls to forward() to specify the browser should be redirected to the referring page...
Definition: constants.php:37
$username
Definition: login.php:9
const ELGG_SHOW_DISABLED_ENTITIES
Definition: constants.php:123
$result
Definition: login.php:12
elgg_get_login_forward_url(\ElggUser $user)
Determine which URL the user should be forwarded to upon successful login.
Definition: sessions.php:141
$password
Definition: login.php:10
elgg_pam_authenticate(string $policy, array $authentication_params=[])
Start an authentication process.
Definition: pam.php:58
elgg_register_error_message(string|array $options)
Registers a error system message.
Definition: elgglib.php:62
const ELGG_HTTP_BAD_REQUEST
Definition: constants.php:64
elgg_login(\ElggUser $user, bool $persistent=false)
Log in a user.
Definition: sessions.php:81
if(empty($username)||empty($password)) $user
Definition: login.php:19
elgg_generate_url(string $name, array $parameters=[])
Generate a URL for named route.
elgg_is_xhr()
Checks whether the request was requested via ajax.
Definition: actions.php:78
Login as the specified user.
getRedirectUrl()
Get preferred redirect URL.
catch(AuthenticationException|LoginException $e) if(elgg_is_xhr()) $output
Definition: login.php:86