-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsso.module
45 lines (40 loc) · 1.15 KB
/
sso.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
/**
* Implements hook_config_info().
*/
function sso_config_info() {
return [
'sso.settings' => [
'label' => t('Single Sign-On Settings'),
'group' => t('SSO'),
'description' => t('Stores configuration settings for the Single Sign-On (SSO) module.'),
],
];
}
/**
* Implements hook_init().
* Check the sessions table for an existing valid session on page load.
*/
function sso_init() {
if (user_is_logged_in()) {
return; // Skip if the user is already logged in.
}
// Retrieve the session ID from the Backdrop native cookie.
$cookie_name = session_name();
$sid = $_COOKIE[$cookie_name] ?? NULL;
if ($sid) {
// Look up the session in the sessions table.
$session = db_select('sessions', 's')
->fields('s', ['uid', 'timestamp'])
->condition('sid', $sid)
->condition('timestamp', REQUEST_TIME - ini_get('session.gc_maxlifetime'), '>')
->execute()
->fetchAssoc();
if ($session && !empty($session['uid'])) {
$account = user_load($session['uid']); // Load the user account.
if ($account) {
user_login_finalize($account); // Log in the user.
}
}
}
}