ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 
21  private $logger = null;
22 
23  private $id = '';
24  private $user_id = 0;
25  private $expired = false;
26  private $authenticated = false;
27 
31  private function __construct()
32  {
33  $this->logger = ilLoggerFactory::getLogger('auth');
34  }
35 
40  public static function getInstance()
41  {
42  if(self::$instance)
43  {
44  return self::$instance;
45  }
46  return self::$instance = new self();
47  }
48 
52  public function getLogger()
53  {
54  return $this->logger;
55  }
56 
61  public function init()
62  {
63  session_start();
64 
65  $this->setId(session_id());
66 
67  $user_id = (int) ilSession::get(self::SESSION_AUTH_USER_ID);
68 
69  if($user_id)
70  {
71  $this->getLogger()->debug('Resuming old session for user: ' . $user_id);
72  $this->setUserId(ilSession::get(self::SESSION_AUTH_USER_ID));
73  $this->expired = (int) ilSession::get(self::SESSION_AUTH_EXPIRED);
74  $this->authenticated = (int) ilSession::get(self::SESSION_AUTH_AUTHENTICATED);
75 
76  $this->validateExpiration();
77  }
78  else
79  {
80  $this->getLogger()->debug('Started new session.');
81  $this->setUserId(0);
82  $this->expired = false;
83  $this->authenticated = false;
84  }
85  return true;
86  }
87 
92  public function isValid()
93  {
94  return !$this->isExpired() && $this->isAuthenticated();
95  }
96 
100  public function regenerateId()
101  {
102  $old_session_id = session_id();
103  session_regenerate_id(true);
104  $this->setId(session_id());
105  $this->getLogger()->info('Session regenerate id: ['.substr($old_session_id,0,5).'] -> ['. substr($this->getId(),0,5).']');
106  }
107 
111  public function logout()
112  {
113  $this->getLogger()->debug('Logout called for: '. $this->getUserId());
114  $this->setAuthenticated(false, 0);
115  session_regenerate_id(true);
116  session_destroy();
117  }
118 
122  public function isAuthenticated()
123  {
124  return $this->authenticated;
125  }
126 
132  public function setAuthenticated($a_status, $a_user_id)
133  {
134  $this->authenticated = $a_status;
135  $this->user_id = $a_user_id;
136  ilSession::set(self::SESSION_AUTH_AUTHENTICATED, $a_status);
137  ilSession::set(self::SESSION_AUTH_USER_ID, (int) $a_user_id);
138  $this->setExpired(false);
139  if($a_status)
140  {
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  {
190  // keep status
191  return false;
192  }
193 
194  if(time() > ilSession::lookupExpireTime($this->getId()))
195  {
196  $this->setExpired(true);
197  return false;
198  }
199  return true;
200  }
201 
202  public function setId($a_id)
203  {
204  $this->id = $a_id;
205  }
206 
207  public function getId()
208  {
209  return $this->id;
210  }
211 
212 }
213 ?>
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.
setUserId($a_id)
Set authenticated user id.
__construct()
Consctructor.
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 getLogger($a_component_id)
Get component logger.
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
static getInstance()
Get instance.