ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilAuthSession.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 {
23  private const string SESSION_AUTH_AUTHENTICATED = '_authsession_authenticated';
24  private const string SESSION_AUTH_USER_ID = '_authsession_user_id';
25  private const string SESSION_AUTH_EXPIRED = '_authsession_expired';
26 
27  private static ?ilAuthSession $instance = null;
28 
29  private ilLogger $logger;
30 
31  private string $id = '';
32  private int $user_id = 0;
33  private bool $expired = false;
34  private bool $authenticated = false;
35 
36  private function __construct(ilLogger $logger)
37  {
38  $this->logger = $logger;
39  }
40 
41  public static function getInstance(ilLogger $logger): ilAuthSession
42  {
43  if (self::$instance) {
44  return self::$instance;
45  }
46  return self::$instance = new self($logger);
47  }
48 
49  protected function getLogger(): ilLogger
50  {
51  return $this->logger;
52  }
53 
57  public function init(): bool
58  {
59  if (session_status() === PHP_SESSION_ACTIVE) {
60  $this->getLogger()->error(__METHOD__ . ' called with active session.');
61  $this->getLogger()->logStack(ilLogLevel::ERROR);
62  return false;
63  }
64 
65  session_start();
66 
67  $this->setId(session_id());
68 
69  $user_id = (int) (ilSession::get(self::SESSION_AUTH_USER_ID) ?? ANONYMOUS_USER_ID);
70 
71  if ($user_id) {
72  $this->getLogger()->debug('Resuming old session for user: ' . $user_id);
73  $this->setUserId($user_id);
74  $this->expired = (bool) ilSession::get(self::SESSION_AUTH_EXPIRED);
75  $this->authenticated = (bool) ilSession::get(self::SESSION_AUTH_AUTHENTICATED);
76 
77  $this->validateExpiration();
78  } else {
79  $this->getLogger()->debug('Started new session.');
81  $this->expired = false;
82  $this->authenticated = false;
83  }
84  return true;
85  }
86 
90  public function isValid(): bool
91  {
92  return !$this->isExpired() && $this->isAuthenticated();
93  }
94 
98  public function regenerateId(): void
99  {
100  $old_session_id = session_id();
101  session_regenerate_id(true);
102  $this->setId(session_id());
103  $this->getLogger()->info('Session regenerate id: [' . substr($old_session_id, 0, 5) . '] -> [' . substr($this->getId(), 0, 5) . ']');
104  }
105 
109  public function logout(): void
110  {
111  $this->getLogger()->debug('Logout called for: ' . $this->getUserId());
112  session_regenerate_id(true);
113  session_destroy();
114 
115  $this->init();
116  $this->setAuthenticated(true, ANONYMOUS_USER_ID);
117  }
118 
122  public function isAuthenticated(): bool
123  {
124  return $this->authenticated || $this->user_id === ANONYMOUS_USER_ID;
125  }
126 
130  public function setAuthenticated(bool $a_status, int $a_user_id): void
131  {
132  $this->authenticated = $a_status;
133  $this->user_id = $a_user_id;
134  ilSession::set(self::SESSION_AUTH_AUTHENTICATED, $a_status);
135  ilSession::set(self::SESSION_AUTH_USER_ID, $a_user_id);
136  $this->setExpired(false);
137  if ($a_status) {
138  $this->regenerateId();
139  }
140  }
141 
145  public function isExpired(): bool
146  {
147  return $this->expired && $this->user_id !== ANONYMOUS_USER_ID;
148  }
149 
153  public function setExpired(bool $a_status): void
154  {
155  $this->expired = $a_status;
156  ilSession::set(self::SESSION_AUTH_EXPIRED, (int) $a_status);
157  }
158 
162  public function setUserId(int $a_id): void
163  {
164  $this->user_id = $a_id;
165  }
166 
170  public function getUserId(): int
171  {
172  return $this->user_id;
173  }
174 
178  protected function validateExpiration(): bool
179  {
180  if ($this->isExpired()) {
181  // keep status
182  return false;
183  }
184 
185  if (time() > ilSession::lookupExpireTime($this->getId())) {
186  $this->setExpired(true);
187  return false;
188  }
189  return true;
190  }
191 
195  protected function setId(string $a_id): void
196  {
197  $this->id = $a_id;
198  }
199 
203  public function getId(): string
204  {
205  return $this->id;
206  }
207 }
static get(string $a_var)
logout()
Logout user => stop session.
setUserId(int $a_id)
Set authenticated user id.
const ANONYMOUS_USER_ID
Definition: constants.php:27
regenerateId()
Regenerate id.
isExpired()
Check if current is or was expired in last request.
isAuthenticated()
Check if session is authenticated.
static lookupExpireTime(string $a_session_id)
Lookup expire time for a specific session.
getUserId()
Get authenticated user id.
init()
Start auth session.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
const string SESSION_AUTH_EXPIRED
setExpired(bool $a_status)
Set session expired.
__construct(ilLogger $logger)
getId()
get session id
isValid()
Check if current session is valid (authenticated and not expired)
setAuthenticated(bool $a_status, int $a_user_id)
Set authenticated.
static getInstance(ilLogger $logger)
validateExpiration()
Check expired value of session.
static ilAuthSession $instance
const string SESSION_AUTH_USER_ID
const string SESSION_AUTH_AUTHENTICATED
static set(string $a_var, $a_val)
Set a value.
setId(string $a_id)
Set id.