ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
ilAuthSession Class Reference
+ Collaboration diagram for ilAuthSession:

Public Member Functions

 init ()
 Start auth session. More...
 
 isValid ()
 Check if current session is valid (authenticated and not expired) More...
 
 regenerateId ()
 Regenerate id. More...
 
 logout ()
 Logout user => stop session. More...
 
 isAuthenticated ()
 Check if session is authenticated. More...
 
 setAuthenticated (bool $a_status, int $a_user_id)
 Set authenticated. More...
 
 isExpired ()
 Check if current is or was expired in last request. More...
 
 setExpired (bool $a_status)
 Set session expired. More...
 
 setUserId (int $a_id)
 Set authenticated user id. More...
 
 getUserId ()
 Get authenticated user id. More...
 
 getId ()
 get session id More...
 

Static Public Member Functions

static getInstance (ilLogger $logger)
 

Protected Member Functions

 getLogger ()
 
 validateExpiration ()
 Check expired value of session. More...
 
 setId (string $a_id)
 Set id. More...
 

Private Member Functions

 __construct (ilLogger $logger)
 

Private Attributes

const string SESSION_AUTH_AUTHENTICATED = '_authsession_authenticated'
 
const string SESSION_AUTH_USER_ID = '_authsession_user_id'
 
const string SESSION_AUTH_EXPIRED = '_authsession_expired'
 
ilLogger $logger
 
string $id = ''
 
int $user_id = 0
 
bool $expired = false
 
bool $authenticated = false
 

Static Private Attributes

static ilAuthSession $instance = null
 

Detailed Description

Definition at line 21 of file class.ilAuthSession.php.

Constructor & Destructor Documentation

◆ __construct()

ilAuthSession::__construct ( ilLogger  $logger)
private

Definition at line 36 of file class.ilAuthSession.php.

References $logger, and ILIAS\Repository\logger().

37  {
38  $this->logger = $logger;
39  }
+ Here is the call graph for this function:

Member Function Documentation

◆ getId()

ilAuthSession::getId ( )

get session id

Definition at line 203 of file class.ilAuthSession.php.

References $id.

Referenced by regenerateId(), and validateExpiration().

203  : string
204  {
205  return $this->id;
206  }
+ Here is the caller graph for this function:

◆ getInstance()

static ilAuthSession::getInstance ( ilLogger  $logger)
static

Definition at line 41 of file class.ilAuthSession.php.

References $logger.

Referenced by ilInitialisation\initSession().

42  {
43  if (self::$instance) {
44  return self::$instance;
45  }
46  return self::$instance = new self($logger);
47  }
+ Here is the caller graph for this function:

◆ getLogger()

ilAuthSession::getLogger ( )
protected

Definition at line 49 of file class.ilAuthSession.php.

References $logger.

Referenced by init(), logout(), and regenerateId().

49  : ilLogger
50  {
51  return $this->logger;
52  }
+ Here is the caller graph for this function:

◆ getUserId()

ilAuthSession::getUserId ( )

Get authenticated user id.

Definition at line 170 of file class.ilAuthSession.php.

References $user_id.

Referenced by logout(), and ilAuthFrontend\migrateAccount().

170  : int
171  {
172  return $this->user_id;
173  }
+ Here is the caller graph for this function:

◆ init()

ilAuthSession::init ( )

Start auth session.

Definition at line 57 of file class.ilAuthSession.php.

References ANONYMOUS_USER_ID, ilLogLevel\ERROR, ilSession\get(), getLogger(), ILIAS\Repository\int(), setId(), setUserId(), and validateExpiration().

Referenced by logout().

57  : 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  }
static get(string $a_var)
setUserId(int $a_id)
Set authenticated user id.
const ANONYMOUS_USER_ID
Definition: constants.php:27
validateExpiration()
Check expired value of session.
setId(string $a_id)
Set id.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isAuthenticated()

ilAuthSession::isAuthenticated ( )

Check if session is authenticated.

Definition at line 122 of file class.ilAuthSession.php.

References ANONYMOUS_USER_ID.

Referenced by isValid(), and ilAuthFrontend\migrateAccount().

122  : bool
123  {
124  return $this->authenticated || $this->user_id === ANONYMOUS_USER_ID;
125  }
const ANONYMOUS_USER_ID
Definition: constants.php:27
+ Here is the caller graph for this function:

◆ isExpired()

ilAuthSession::isExpired ( )

Check if current is or was expired in last request.

Definition at line 145 of file class.ilAuthSession.php.

References ANONYMOUS_USER_ID.

Referenced by isValid(), and validateExpiration().

145  : bool
146  {
147  return $this->expired && $this->user_id !== ANONYMOUS_USER_ID;
148  }
const ANONYMOUS_USER_ID
Definition: constants.php:27
+ Here is the caller graph for this function:

◆ isValid()

ilAuthSession::isValid ( )

Check if current session is valid (authenticated and not expired)

Definition at line 90 of file class.ilAuthSession.php.

References isAuthenticated(), and isExpired().

90  : bool
91  {
92  return !$this->isExpired() && $this->isAuthenticated();
93  }
isExpired()
Check if current is or was expired in last request.
isAuthenticated()
Check if session is authenticated.
+ Here is the call graph for this function:

◆ logout()

ilAuthSession::logout ( )

Logout user => stop session.

Definition at line 109 of file class.ilAuthSession.php.

References ANONYMOUS_USER_ID, getLogger(), getUserId(), init(), and setAuthenticated().

109  : 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  }
const ANONYMOUS_USER_ID
Definition: constants.php:27
getUserId()
Get authenticated user id.
init()
Start auth session.
setAuthenticated(bool $a_status, int $a_user_id)
Set authenticated.
+ Here is the call graph for this function:

◆ regenerateId()

ilAuthSession::regenerateId ( )

Regenerate id.

Definition at line 98 of file class.ilAuthSession.php.

References getId(), getLogger(), and setId().

Referenced by setAuthenticated().

98  : 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  }
getId()
get session id
setId(string $a_id)
Set id.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setAuthenticated()

ilAuthSession::setAuthenticated ( bool  $a_status,
int  $a_user_id 
)

Set authenticated.

Definition at line 130 of file class.ilAuthSession.php.

References regenerateId(), ilSession\set(), and setExpired().

Referenced by logout().

130  : 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  }
regenerateId()
Regenerate id.
setExpired(bool $a_status)
Set session expired.
static set(string $a_var, $a_val)
Set a value.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setExpired()

ilAuthSession::setExpired ( bool  $a_status)

Set session expired.

Definition at line 153 of file class.ilAuthSession.php.

References ilSession\set().

Referenced by setAuthenticated(), and validateExpiration().

153  : void
154  {
155  $this->expired = $a_status;
156  ilSession::set(self::SESSION_AUTH_EXPIRED, (int) $a_status);
157  }
static set(string $a_var, $a_val)
Set a value.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setId()

ilAuthSession::setId ( string  $a_id)
protected

Set id.

Definition at line 195 of file class.ilAuthSession.php.

Referenced by init(), and regenerateId().

195  : void
196  {
197  $this->id = $a_id;
198  }
+ Here is the caller graph for this function:

◆ setUserId()

ilAuthSession::setUserId ( int  $a_id)

Set authenticated user id.

Definition at line 162 of file class.ilAuthSession.php.

Referenced by init().

162  : void
163  {
164  $this->user_id = $a_id;
165  }
+ Here is the caller graph for this function:

◆ validateExpiration()

ilAuthSession::validateExpiration ( )
protected

Check expired value of session.

Definition at line 178 of file class.ilAuthSession.php.

References getId(), isExpired(), ilSession\lookupExpireTime(), and setExpired().

Referenced by init().

178  : 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  }
isExpired()
Check if current is or was expired in last request.
static lookupExpireTime(string $a_session_id)
Lookup expire time for a specific session.
setExpired(bool $a_status)
Set session expired.
getId()
get session id
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $authenticated

bool ilAuthSession::$authenticated = false
private

Definition at line 34 of file class.ilAuthSession.php.

◆ $expired

bool ilAuthSession::$expired = false
private

Definition at line 33 of file class.ilAuthSession.php.

◆ $id

string ilAuthSession::$id = ''
private

Definition at line 31 of file class.ilAuthSession.php.

Referenced by getId().

◆ $instance

ilAuthSession ilAuthSession::$instance = null
staticprivate

Definition at line 27 of file class.ilAuthSession.php.

◆ $logger

ilLogger ilAuthSession::$logger
private

Definition at line 29 of file class.ilAuthSession.php.

Referenced by __construct(), getInstance(), and getLogger().

◆ $user_id

int ilAuthSession::$user_id = 0
private

Definition at line 32 of file class.ilAuthSession.php.

Referenced by getUserId().

◆ SESSION_AUTH_AUTHENTICATED

const string ilAuthSession::SESSION_AUTH_AUTHENTICATED = '_authsession_authenticated'
private

Definition at line 23 of file class.ilAuthSession.php.

◆ SESSION_AUTH_EXPIRED

const string ilAuthSession::SESSION_AUTH_EXPIRED = '_authsession_expired'
private

Definition at line 25 of file class.ilAuthSession.php.

◆ SESSION_AUTH_USER_ID

const string ilAuthSession::SESSION_AUTH_USER_ID = '_authsession_user_id'
private

Definition at line 24 of file class.ilAuthSession.php.


The documentation for this class was generated from the following file: