4
0

User.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * The Minz_User class handles the user information.
  5. */
  6. final class Minz_User {
  7. public const INTERNAL_USER = '_';
  8. public const CURRENT_USER = 'currentUser';
  9. /**
  10. * Name of the user of the current request as last set by `change()` (empty string if cleared), or null if not set yet.
  11. * Kept apart from the session, which is shared with concurrent requests and reloaded at each access:
  12. * another request changing the current user of the session (e.g. login, logout) must not change the user of a running request.
  13. */
  14. private static ?string $name = null;
  15. /**
  16. * @return string the name of the current user, or null if there is none
  17. */
  18. public static function name(): ?string {
  19. $currentUser = self::$name ?? trim(Minz_Session::paramString(Minz_User::CURRENT_USER));
  20. return $currentUser === '' ? null : $currentUser;
  21. }
  22. /**
  23. * @param string $name the name of the new user. Set to empty string to clear the user.
  24. */
  25. public static function change(string $name = ''): void {
  26. $name = trim($name);
  27. self::$name = $name;
  28. Minz_Session::_param(Minz_User::CURRENT_USER, $name === '' ? false : $name);
  29. }
  30. }