19 declare(strict_types=1);
45 'session_max_idle_after_first_request',
46 'session_allow_client_maintenance',
47 'session_handling_type' 68 self::SESSION_TYPE_USER,
69 self::SESSION_TYPE_ANONYM
79 self::SESSION_TYPE_UNKNOWN,
80 self::SESSION_TYPE_SYSTEM,
81 self::SESSION_TYPE_ADMIN
100 case isset($_ENV[
'SHELL']):
101 $type = self::SESSION_TYPE_SYSTEM;
105 $type = self::SESSION_TYPE_ANONYM;
108 case self::checkAdministrationPermission($user_id):
109 $type = self::SESSION_TYPE_ADMIN;
113 $type = self::SESSION_TYPE_USER;
118 self::debug(__METHOD__ .
" --> update sessions type to (" .
$type .
")");
125 if (in_array($type, self::$session_types_controlled,
true)) {
127 self::checkCurrentSessionIsAllowed($auth_session, $user_id);
143 if ((
int)
$ilSetting->get(
'session_handling_type',
'0') !== 1) {
148 self::debug(__METHOD__ .
" --> reset sessions type to (" .
ilSession::get(
'SessionType') .
")");
152 self::removeSessionCookie();
167 $max_sessions = (
int)
$ilSetting->get(
'session_max_count', (
string) self::DEFAULT_MAX_COUNT);
169 if ($max_sessions > 0) {
171 $num_sessions = self::getExistingSessionCount(self::$session_types_controlled);
173 self::debug(__METHOD__ .
"--> total existing sessions (" . $num_sessions .
")");
175 if (($num_sessions + 1) > $max_sessions) {
176 self::debug(__METHOD__ .
' --> limit for session pool reached, but try kicking some first request abidencer');
178 self::kickFirstRequestAbidencer(self::$session_types_controlled);
181 $num_sessions = self::getExistingSessionCount(self::$session_types_controlled);
183 if (($num_sessions + 1) > $max_sessions) {
184 self::debug(__METHOD__ .
' --> limit for session pool still reached so try kick one min idle session');
186 self::kickOneMinIdleSession(self::$session_types_controlled);
189 $num_sessions = self::getExistingSessionCount(self::$session_types_controlled);
191 if (($num_sessions + 1) > $max_sessions) {
192 self::debug(__METHOD__ .
' --> limit for session pool still reached so logout session (' . session_id() .
') and trigger event');
208 $ilAppEventHandler = $DIC[
'ilAppEventHandler'];
209 $ilAppEventHandler->raise(
210 'Services/Authentication',
211 'reachedSessionPoolLimit',
221 self::debug(__METHOD__ .
' --> limit of session pool not reached anymore after kicking one min idle session');
224 self::debug(__METHOD__ .
' --> limit of session pool not reached anymore after kicking some first request abidencer');
227 self::debug(__METHOD__ .
' --> limit for session pool not reached yet');
230 self::debug(__METHOD__ .
' --> limit for session pool not set so check is bypassed');
241 $ilDB = $DIC[
'ilDB'];
245 $query =
"SELECT count(session_id) AS num_sessions FROM usr_session " .
246 "WHERE expires > %s " .
247 "AND " .
$ilDB->in(
'type', $a_types,
false,
'integer');
262 $ilDB = $DIC[
'ilDB'];
266 $min_idle = (
int)
$ilSetting->get(
'session_min_idle', (
string) self::DEFAULT_MIN_IDLE) * 60;
267 $max_idle = (
int)
$ilSetting->get(
'session_max_idle', (
string) self::DEFAULT_MAX_IDLE) * 60;
269 $query =
"SELECT session_id,expires FROM usr_session WHERE expires >= %s " .
270 "AND (expires - %s) < (%s - %s) " .
271 "AND " .
$ilDB->in(
'type', $a_types,
false,
'integer') .
" ORDER BY expires";
275 array(
'integer',
'integer',
'integer',
'integer'),
276 array($ts, $ts, $max_idle, $min_idle)
282 self::debug(__METHOD__ .
' --> successfully deleted one min idle session');
286 self::debug(__METHOD__ .
' --> no min idle session available for deletion');
297 $ilDB = $DIC[
'ilDB'];
300 $max_idle_after_first_request = (
int)
$ilSetting->get(
'session_max_idle_after_first_request') * 60;
302 if ((
int) $max_idle_after_first_request === 0) {
306 $query =
"SELECT session_id,expires FROM usr_session WHERE " .
307 "(ctime - createtime) < %s " .
308 "AND (%s - createtime) > %s " .
309 "AND " .
$ilDB->in(
'type', $a_types,
false,
'integer');
313 array(
'integer',
'integer',
'integer'),
314 array($max_idle_after_first_request, time(), $max_idle_after_first_request)
317 $session_ids = array();
319 $session_ids[$row->session_id] = $row->expires;
323 self::debug(__METHOD__ .
' --> Finished kicking first request abidencer');
336 $ilDB = $DIC[
'ilDB'];
338 $query =
"SELECT session_id, expires FROM usr_session " .
339 "WHERE session_id = %s";
348 if ($row[
'expires'] > $ts) {
349 self::debug(__METHOD__ .
' --> Found a valid session with id (' . $a_sid .
')');
352 self::debug(__METHOD__ .
' --> Found an expired session with id (' . $a_sid .
')');
356 if (count($sessions) === 1) {
357 self::debug(__METHOD__ .
' --> Exact one valid session found for session id (' . $a_sid .
')');
362 if (count($sessions) > 1) {
363 self::debug(__METHOD__ .
' --> Strange!!! More than one sessions found for given session id! (' . $a_sid .
')');
365 self::debug(__METHOD__ .
' --> No valid session found for session id (' . $a_sid .
')');
377 self::debug(
'Session cookie has been removed');
395 $rbacsystem = $DIC[
'rbacsystem'];
397 $access = $rbacsystem->checkAccessOfUser(
411 private static function debug(
string $a_debug_log_message): void
415 $logger = $DIC->logger()->auth();
417 $logger->debug($a_debug_log_message);
427 return self::$setting_fields;
const DEFAULT_MAX_COUNT
default value for settings that have not been defined in setup or administration yet ...
static array $setting_fields
all fieldnames that are saved in settings table
static get(string $a_var)
logout()
Logout user => stop session.
static createRawEntry(string $a_session_id, int $a_session_type, int $a_timestamp, int $a_user_id)
Create raw data entry.
static removeSessionCookie()
removes a session cookie, so it is not sent by browser anymore
static checkAdministrationPermission(int $a_user_id)
checks wether a given user login relates to an user with administrative permissions ...
static _lookupId($a_user_str)
static getSettingFields()
returns the array of setting fields
const SESSION_HANDLING_FIXED
const SESSION_TYPE_ANONYM
static setCookie(string $a_cookie_name, string $a_cookie_value='', bool $a_also_set_super_global=true, bool $a_set_cookie_invalid=false)
static isValidSession(string $a_sid)
checks if session exists for given id and if it is still valid
const SESSION_CLOSE_LIMIT
static redirect(string $a_script)
static getExistingSessionCount(array $a_types)
returns number of valid sessions relating to given session types
static handleLoginEvent(string $a_login, ilAuthSession $auth_session)
when current session is allowed to be created it marks it with type regarding to the sessions user co...
static _destroy($a_session_id, ?int $a_closing_context=null, $a_expired_at=null)
Destroy session.
static array $session_types_controlled
const SESSION_TYPE_SYSTEM
static array $session_types_not_controlled
all session types that will be involved when count of sessions will be determined or when idleing ses...
static setClosingContext(int $a_context)
set closing context (for statistics)
const SESSION_HANDLING_LOAD_DEPENDENT
const DEFAULT_ALLOW_CLIENT_MAINTENANCE
static handleLogoutEvent()
reset sessions type to unknown
static kickFirstRequestAbidencer(array $a_types)
kicks sessions of users that abidence after login so people could not login and go for coffe break ;-...
const SESSION_CLOSE_FIRST
static kickOneMinIdleSession(array $a_types)
if sessions exist that relates to given session types and idled longer than min idle parameter...
static set(string $a_var, $a_val)
Set a value.
static debug(string $a_debug_log_message)
logs the given debug message in
const DEFAULT_MAX_IDLE_AFTER_FIRST_REQUEST
const SESSION_TYPE_UNKNOWN
session types from which one is assigned to each session
static checkCurrentSessionIsAllowed(ilAuthSession $auth, int $a_user_id)
checks wether the current session exhaust the limit of sessions when limit is reached it deletes "fir...