Elgg  Version 5.1
SessionManagerService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
10 
17 
21  protected $entity_cache;
22 
26  protected $events;
27 
31  protected $ignore_access = false;
32 
36  protected $logged_in_user;
37 
41  protected $persistent_login;
42 
46  protected $show_disabled_entities = false;
47 
51  protected $session;
52 
56  protected $session_cache;
57 
61  protected $translator;
62 
73  public function __construct(
75  EventsService $events,
76  Translator $translator,
77  PersistentLoginService $persistent_login,
78  SessionCache $session_cache,
79  EntityCache $entity_cache
80  ) {
81  $this->session = $session;
82  $this->events = $events;
83  $this->translator = $translator;
84  $this->persistent_login = $persistent_login;
85  $this->session_cache = $session_cache;
86  $this->entity_cache = $entity_cache;
87  }
88 
94  public function getIgnoreAccess(): bool {
95  return $this->ignore_access;
96  }
97 
105  public function setIgnoreAccess(bool $ignore = true): bool {
106  $prev = $this->ignore_access;
107  $this->ignore_access = $ignore;
108 
109  return $prev;
110  }
111 
117  public function getDisabledEntityVisibility(): bool {
118  return $this->show_disabled_entities;
119  }
120 
128  public function setDisabledEntityVisibility(bool $show = true): bool {
129  $prev = $this->show_disabled_entities;
130  $this->show_disabled_entities = $show;
131 
132  return $prev;
133  }
134 
145  public function setUserToken(\ElggUser $user = null): void {
146  if (!$user instanceof \ElggUser) {
147  $user = $this->getLoggedInUser();
148  }
149 
150  if (!$user instanceof \ElggUser) {
151  return;
152  }
153 
154  $this->session->set('__user_token', $this->generateUserToken($user));
155  }
156 
166  public function validateUserToken(\ElggUser $user): void {
167  $session_token = $this->session->get('__user_token');
168  $user_token = $this->generateUserToken($user);
169 
170  if ($session_token !== $user_token) {
171  throw new SecurityException($this->translator->translate('session_expired'));
172  }
173  }
174 
183  protected function generateUserToken(\ElggUser $user): string {
184  $hmac = _elgg_services()->hmac->getHmac([
185  $user->time_created,
186  $user->guid,
187  ], 'sha256', $user->password_hash);
188 
189  return $hmac->getToken();
190  }
191 
202  public function login(\ElggUser $user, bool $persistent = false): void {
203  if ($user->isBanned()) {
204  throw new LoginException($this->translator->translate('LoginException:BannedUser'));
205  }
206 
207  // give plugins a chance to reject the login of this user (no user in session!)
208  if (!$this->events->triggerBefore('login', 'user', $user)) {
209  throw new LoginException($this->translator->translate('LoginException:Unknown'));
210  }
211 
212  if (!$user->isEnabled()) {
213  // fallback if no plugin provided a reason
214  throw new LoginException($this->translator->translate('LoginException:DisabledUser'));
215  }
216 
217  // #5933: set logged in user early so code in login event will be able to
218  // use elgg_get_logged_in_user_entity().
219  $this->setLoggedInUser($user, true);
220  $this->setUserToken($user);
221 
222  // re-register at least the core language file for users with language other than site default
223  $this->translator->registerTranslations(\Elgg\Project\Paths::elgg() . 'languages/');
224 
225  // if remember me checked, set cookie with token and store hash(token) for user
226  if ($persistent) {
227  $this->persistent_login->makeLoginPersistent($user);
228  }
229 
230  // User's privilege has been elevated, so change the session id (prevents session fixation)
231  $this->session->migrate();
232 
233  // check before updating last login to determine first login
234  $first_login = empty($user->last_login);
235 
236  $user->setLastLogin();
237  _elgg_services()->accounts->resetAuthenticationFailures($user); // can't inject DI service because of circular reference
238 
239  $this->events->triggerAfter('login', 'user', $user);
240 
241  if ($first_login) {
242  $this->events->trigger('login:first', 'user', $user);
243  $user->first_login = time();
244  }
245  }
246 
253  public function logout(): bool {
254  $user = $this->getLoggedInUser();
255  if (!$user instanceof \ElggUser) {
256  return false;
257  }
258 
259  if (!$this->events->triggerBefore('logout', 'user', $user)) {
260  return false;
261  }
262 
263  $this->persistent_login->removePersistentLogin();
264 
265  // pass along any messages into new session
266  $old_msg = $this->session->get(SystemMessagesService::SESSION_KEY, []);
267  $this->session->invalidate();
268 
269  $this->logged_in_user = null;
270 
271  $this->session->set(SystemMessagesService::SESSION_KEY, $old_msg);
272 
273  $this->events->triggerAfter('logout', 'user', $user);
274 
275  return true;
276  }
277 
287  public function setLoggedInUser(\ElggUser $user, bool $migrate = null): void {
288  $current_user = $this->getLoggedInUser();
289  if ($current_user != $user) {
290  if (!isset($migrate)) {
291  $migrate = !\Elgg\Application::isCli();
292  }
293 
294  if ($migrate) {
295  $this->session->migrate(true);
296  }
297 
298  $this->session->set('guid', $user->guid);
299  $this->logged_in_user = $user;
300  $this->session_cache->clear();
301  $this->entity_cache->save($user);
302  $this->translator->setCurrentLanguage($user->language);
303  }
304  }
305 
313  public function getLoggedInUser(): ?\ElggUser {
314  return $this->logged_in_user;
315  }
316 
322  public function getLoggedInUserGuid(): int {
323  $user = $this->getLoggedInUser();
324  return $user ? $user->guid : 0;
325  }
326 
332  public function isAdminLoggedIn(): bool {
333  $user = $this->getLoggedInUser();
334 
335  return $user && $user->isAdmin();
336  }
337 
343  public function isLoggedIn(): bool {
344  return (bool) $this->getLoggedInUser();
345  }
346 
353  public function removeLoggedInUser(): void {
354  $this->logged_in_user = null;
355  $this->session->remove('guid');
356  $this->session_cache->clear();
357  }
358 }
getLoggedInUser()
Gets the logged in user.
Generic parent class for login exceptions.
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
login(\ElggUser $user, bool $persistent=false)
Log in a user.
Events service.
isAdminLoggedIn()
Returns whether or not the viewer is currently logged in and an admin user.
logout()
Log the current user out.
setLastLogin()
Sets the last logon time of the user to right now.
Definition: ElggUser.php:240
getDisabledEntityVisibility()
Are disabled entities shown?
removeLoggedInUser()
Remove the logged in user.
setUserToken(\ElggUser $user=null)
Set a user specific token in the session for the currently logged in user.
isLoggedIn()
Returns whether or not the user is currently logged in.
setDisabledEntityVisibility(bool $show=true)
Include disabled entities in queries.
Elgg Session Management.
Definition: ElggSession.php:19
Throw when a Security Exception occurs.
if(empty($entity_guid)||empty($recipient)||empty($muted_settings)||empty($hmac_token)) $hmac
Definition: mute.php:18
static isCli()
Is application running in CLI.
Volatile cache for entities.
Definition: EntityCache.php:10
validateUserToken(\ElggUser $user)
Validate the user token stored in the session.
$user
Definition: ban.php:7
getLoggedInUserGuid()
Return the current logged in user by guid.
isEnabled()
Is this entity enabled?
if(isset($_COOKIE['elggperm'])) $session
Definition: login_as.php:29
setLoggedInUser(\ElggUser $user, bool $migrate=null)
Sets the logged in user.
__construct(\ElggSession $session, EventsService $events, Translator $translator, PersistentLoginService $persistent_login, SessionCache $session_cache, EntityCache $entity_cache)
Constructor.
setIgnoreAccess(bool $ignore=true)
Set ignore access.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:346
$persistent
Definition: login_as.php:21
getIgnoreAccess()
Get current ignore access setting.
isBanned()
Is this user banned or not?
Definition: ElggUser.php:178
Login as the specified user.
var elgg
Definition: elgglib.js:4
generateUserToken(\ElggUser $user)
Generate a token for a specific user.