ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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.');
82  $this->setUserId(0);
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  $this->setAuthenticated(false, 0);
116  session_regenerate_id(true);
117  session_destroy();
118  }
119 
123  public function isAuthenticated()
124  {
125  return $this->authenticated;
126  }
127 
133  public function setAuthenticated($a_status, $a_user_id)
134  {
135  $this->authenticated = $a_status;
136  $this->user_id = $a_user_id;
137  ilSession::set(self::SESSION_AUTH_AUTHENTICATED, $a_status);
138  ilSession::set(self::SESSION_AUTH_USER_ID, (int) $a_user_id);
139  $this->setExpired(false);
140  if ($a_status) {
141  $this->regenerateId();
142  }
143  }
144 
149  public function isExpired()
150  {
151  return (bool) $this->expired;
152  }
153 
158  public function setExpired($a_status)
159  {
160  $this->expired = $a_status;
161  ilSession::set(self::SESSION_AUTH_EXPIRED, (int) $a_status);
162  }
163 
168  public function setUserId($a_id)
169  {
170  $this->user_id = $a_id;
171  }
172 
177  public function getUserId()
178  {
179  return $this->user_id;
180  }
181 
186  protected function validateExpiration()
187  {
188  if ($this->isExpired()) {
189  // keep status
190  return false;
191  }
192 
193  if (time() > ilSession::lookupExpireTime($this->getId())) {
194  $this->setExpired(true);
195  return false;
196  }
197  return true;
198  }
199 
204  protected function setId($a_id)
205  {
206  $this->id = $a_id;
207  }
208 
213  public function getId()
214  {
215  return $this->id;
216  }
217 }
setAuthenticated($a_status, $a_user_id)
Set authenticated.
logout()
Logout user => stop session.
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.
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
static getInstance(\ilLogger $logger)
Get instance.
Component logger with individual log levels by component id.
__construct(\ilLogger $logger)
Consctructor.
setId($a_id)
Set id.