ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilAuthSession.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 include_once './Services/Authentication/classes/class.ilSession.php';
6 
14 {
15  const SESSION_AUTH_AUTHENTICATED = '_authsession_authenticated';
16  const SESSION_AUTH_USER_ID = '_authsession_user_id';
17  const SESSION_AUTH_EXPIRED = '_authsession_expired';
18 
19  private static $instance = null;
20 
24  private $logger = null;
25 
26  private $id = '';
27  private $user_id = 0;
28  private $expired = false;
29  private $authenticated = false;
30 
35  private function __construct(\ilLogger $logger)
36  {
37  $this->logger = $logger;
38  }
39 
45  public static function getInstance(\ilLogger $logger)
46  {
47  if (self::$instance) {
48  return self::$instance;
49  }
50  return self::$instance = new self($logger);
51  }
52 
56  protected function getLogger()
57  {
58  return $this->logger;
59  }
60 
65  public function init()
66  {
67  session_start();
68 
69  $this->setId(session_id());
70 
71  $user_id = (int) ilSession::get(self::SESSION_AUTH_USER_ID);
72 
73  if ($user_id) {
74  $this->getLogger()->debug('Resuming old session for user: ' . $user_id);
75  $this->setUserId(ilSession::get(self::SESSION_AUTH_USER_ID));
76  $this->expired = (int) ilSession::get(self::SESSION_AUTH_EXPIRED);
77  $this->authenticated = (int) ilSession::get(self::SESSION_AUTH_AUTHENTICATED);
78 
79  $this->validateExpiration();
80  } else {
81  $this->getLogger()->debug('Started new session.');
83  $this->expired = false;
84  $this->authenticated = false;
85  }
86  return true;
87  }
88 
93  public function isValid()
94  {
95  return !$this->isExpired() && $this->isAuthenticated();
96  }
97 
101  public function regenerateId()
102  {
103  $old_session_id = session_id();
104  session_regenerate_id(true);
105  $this->setId(session_id());
106  $this->getLogger()->info('Session regenerate id: [' . substr($old_session_id, 0, 5) . '] -> [' . substr($this->getId(), 0, 5) . ']');
107  }
108 
112  public function logout()
113  {
114  $this->getLogger()->debug('Logout called for: ' . $this->getUserId());
115  session_regenerate_id(true);
116  session_destroy();
117 
118  $this->init();
119  $this->setAuthenticated(true, ANONYMOUS_USER_ID);
120  }
121 
125  public function isAuthenticated()
126  {
127  return $this->authenticated;
128  }
129 
135  public function setAuthenticated($a_status, $a_user_id)
136  {
137  $this->authenticated = $a_status;
138  $this->user_id = $a_user_id;
139  ilSession::set(self::SESSION_AUTH_AUTHENTICATED, $a_status);
140  ilSession::set(self::SESSION_AUTH_USER_ID, (int) $a_user_id);
141  $this->setExpired(false);
142  if ($a_status) {
143  $this->regenerateId();
144  }
145  }
146 
151  public function isExpired()
152  {
153  return (bool) $this->expired;
154  }
155 
160  public function setExpired($a_status)
161  {
162  $this->expired = $a_status;
163  ilSession::set(self::SESSION_AUTH_EXPIRED, (int) $a_status);
164  }
165 
170  public function setUserId($a_id)
171  {
172  $this->user_id = $a_id;
173  }
174 
179  public function getUserId()
180  {
181  return $this->user_id;
182  }
183 
188  protected function validateExpiration()
189  {
190  if ($this->isExpired()) {
191  // keep status
192  return false;
193  }
194 
195  if (time() > ilSession::lookupExpireTime($this->getId())) {
196  $this->setExpired(true);
197  return false;
198  }
199  return true;
200  }
201 
206  protected function setId($a_id)
207  {
208  $this->id = $a_id;
209  }
210 
215  public function getId()
216  {
217  return $this->id;
218  }
219 }
setAuthenticated($a_status, $a_user_id)
Set authenticated.
logout()
Logout user => stop session.
const ANONYMOUS_USER_ID
Definition: constants.php:25
regenerateId()
Regenerate id.
isExpired()
Check if current is or was expired in last request.
static get($a_var)
Get a value.
static set($a_var, $a_val)
Set a value.
isAuthenticated()
Check if session is authenticated.
const SESSION_AUTH_AUTHENTICATED
setExpired($a_status)
Set session expired.
getUserId()
Get authenticated user id.
init()
Start auth session.
getId()
get session id
setUserId($a_id)
Set authenticated user id.
static lookupExpireTime($a_session_id)
Lookup expire time for a specific session ilDB $ilDB.
isValid()
Check if current session is valid (authenticated and not expired)
validateExpiration()
Check expired value of session.
static getInstance(\ilLogger $logger)
Get instance.
Component logger with individual log levels by component id.
__construct(\ilLogger $logger)
Consctructor.
setId($a_id)
Set id.