diff --git a/config/config-example.php b/config/config-example.php index 0aa3a61..b8a8b07 100644 --- a/config/config-example.php +++ b/config/config-example.php @@ -1,27 +1,27 @@ getUser($_SESSION['uid'], ['signedsir']); +} catch (LdapException $e) { + $error = $e->getMessage(); +} + $template = Template::create(); $template->addData(['currentSection' => 'index'], 'navbar'); echo $template->render('index', [ + 'error' => $error, 'uid' => $_SESSION['uid'], 'id' => $_SESSION['id'], - 'name' => $_SESSION['cn'] + 'name' => $_SESSION['cn'], + 'signedSir' => Ldap::optionalBooleanToBool($attributes, 'signedsir') ]); diff --git a/public/personal.php b/public/personal.php index 82dec1f..8c1ab85 100644 --- a/public/personal.php +++ b/public/personal.php @@ -35,12 +35,12 @@ header('Content-Type: application/json'); header('Content-Transfer-Encoding: Binary'); header('Content-Description: File Transfer'); - header("Content-Disposition: attachment; filename=${_SESSION['uid']}.json"); + header("Content-Disposition: attachment; filename={$_SESSION['uid']}.json"); echo json_encode($attributes, JSON_PRETTY_PRINT); exit; } - if (isset($_POST) && !empty($_POST)) { + if (!empty($_POST)) { Validation::handleUserEditPost($editableAttributes, $ldap, $_SESSION['uid'], $attributes); http_response_code(303); header('Location: personal.php'); diff --git a/public/sir.php b/public/sir.php index f8dae35..063317a 100644 --- a/public/sir.php +++ b/public/sir.php @@ -9,7 +9,7 @@ require '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; Authentication::requireLogin(); -if (!Authentication::isAdmin()) { +if (!Authentication::isAdmin() && !isset($_GET['uid']) && $_GET['uid'] !== $_SESSION['uid']) { $template = Template::create(); echo $template->render('403'); exit; diff --git a/public/sugo.php b/public/sugo.php new file mode 100644 index 0000000..ff70350 --- /dev/null +++ b/public/sugo.php @@ -0,0 +1,49 @@ +getUsers(['givenname', 'sn', 'signedsir', 'nsaccountlock', 'mail']); + if (isset($_GET['uid'])) { + $selectedUser = $_GET['uid']; + } +} else { + $users = [$ldap->getUser($_SESSION['uid'], ['givenname', 'sn', 'signedsir', 'nsaccountlock', 'mail'])]; + $selectedUser = $_SESSION['uid']; +} + +$mappedUsers = []; +foreach ($users as $user) { + $mappedUsers[] = [ + 'uid' => $user['uid'], + 'cn' => $user['cn'], + 'needsToSign' => Ldap::optionalBooleanToBool($user, 'signedsir'), + 'isLocked' => Ldap::optionalBooleanToBool($user, 'nsaccountlock'), + 'email' => $user['mail'] + ]; +} + +usort($mappedUsers, function (array $a, array $b): int { + return strcasecmp($a['uid'], $b['uid']); +}); + +$template = Template::create(); +$template->addData(['currentSection' => 'sugo'], 'navbar'); + +echo $template->render('sugo', [ + 'users' => $mappedUsers, + 'selectedUser' => $selectedUser +]); diff --git a/src/Authentication.php b/src/Authentication.php index 396e6e6..6a38adf 100644 --- a/src/Authentication.php +++ b/src/Authentication.php @@ -76,15 +76,38 @@ public static function authenticate() session_start(); } - if (defined('TEST_MODE') && TEST_MODE) { + if (TEST_MODE) { error_log('TEST_MODE, faking authentication'); - $_SESSION['uid'] = 'test.administrator'; - $_SESSION['id'] = 'fake:example:68048769-c06d-4873-adf6-dbfa6b0afcd3'; - $_SESSION['cn'] = 'Test Administrator'; - $_SESSION['groups'] = ['HR']; - $_SESSION['expires'] = PHP_INT_MAX; - $_SESSION['refresh_token'] = 'refresh_token'; - $_SESSION['id_token'] = 'id_token'; + switch (TEST_MODE_SSO) { + case 1: + default: + $_SESSION['uid'] = 'test.administrator'; + $_SESSION['id'] = 'fake:example:68048769-c06d-4873-adf6-dbfa6b0afcd3'; + $_SESSION['cn'] = 'Test Administrator'; + $_SESSION['groups'] = ['HR']; + $_SESSION['expires'] = PHP_INT_MAX; + $_SESSION['refresh_token'] = 'refresh_token'; + $_SESSION['id_token'] = 'id_token'; + break; + case 2: + $_SESSION['uid'] = 'alice'; + $_SESSION['id'] = 'fake:example:9e071e1e-d0dd-4d58-9ac2-087ea0b41e97'; + $_SESSION['cn'] = 'Alice Test'; + $_SESSION['groups'] = ['Cloud', 'Gente', 'Riparatori']; + $_SESSION['expires'] = PHP_INT_MAX; + $_SESSION['refresh_token'] = 'refresh_token'; + $_SESSION['id_token'] = 'id_token'; + break; + case 3: + $_SESSION['uid'] = 'brodino'; + $_SESSION['id'] = 'fake:example:c476f0de-e554-439e-af4f-35c8bed02b9b'; + $_SESSION['cn'] = 'Bro Dino'; + $_SESSION['groups'] = ['Admin', 'Gente']; + $_SESSION['expires'] = PHP_INT_MAX; + $_SESSION['refresh_token'] = 'refresh_token'; + $_SESSION['id_token'] = 'id_token'; + break; + } } else { $oidc = self::getOidc(); //$oidc->setCertPath('/path/to/my.cert'); @@ -110,7 +133,7 @@ public static function signOut() $token = $_SESSION['id_token']; session_destroy(); - if (defined('TEST_MODE') && TEST_MODE) { + if (TEST_MODE) { error_log('TEST_MODE, no need to log out'); } else { $oidc->signOut($token, CRAUTO_URL . '/logout_done.php'); @@ -307,6 +330,15 @@ private static function setAttributes(OpenIDConnectClient $oidc, $claims = null, $refresh_token = $oidc->getRefreshToken(); $id_token = $idt ?? $oidc->getIdToken(); + $ldap = new Ldap( + CRAUTO_LDAP_URL, + CRAUTO_LDAP_BIND_DN, + CRAUTO_LDAP_PASSWORD, + CRAUTO_LDAP_USERS_DN, + CRAUTO_LDAP_GROUPS_DN, + CRAUTO_LDAP_STARTTLS + ); + $_SESSION['uid'] = $uid; $_SESSION['id'] = $id; $_SESSION['cn'] = $cn; diff --git a/src/Ldap.php b/src/Ldap.php index 74f4abd..e0d96fb 100644 --- a/src/Ldap.php +++ b/src/Ldap.php @@ -5,6 +5,7 @@ use DateTime; use DateTimeZone; use InvalidArgumentException; +use LDAP\Result; class Ldap { @@ -24,6 +25,7 @@ class Ldap 'createtimestamp' => '20191025105022Z', 'modifytimestamp' => '20191025155317Z', 'safetytestdate' => '20160909', + 'degreecourse' => 'Ingegneria dell\'Ingegno', 'signedsir' => 'true', 'haskey' => 'true', 'schacpersonaluniquecode' => 's111111', @@ -33,7 +35,8 @@ class Ldap 'weeelabnickname' => ['io'], 'websitedescription' => "Il capo supremo\nSu due righe", 'description' => '', - 'nsaccountlock' => null + 'nsaccountlock' => null, + 'mail' => 'admin@example.com', ], 'alice' => [ 'uid' => 'alice', @@ -44,6 +47,7 @@ class Ldap 'createtimestamp' => '20191025105022Z', 'modifytimestamp' => '20191025155317Z', 'safetytestdate' => '20991104', + 'degreecourse' => 'Architettura (dei calcolatori perĂ²)', 'signedsir' => null, 'haskey' => null, 'schacpersonaluniquecode' => 's22222', @@ -53,7 +57,8 @@ class Ldap 'weeelabnickname' => [], 'websitedescription' => 'Persona', 'description' => '', - 'nsaccountlock' => 'true' + 'nsaccountlock' => 'true', + 'mail' => 'alice@example.com', ], 'brodino' => [ 'uid' => 'brodino', @@ -64,6 +69,7 @@ class Ldap 'createtimestamp' => '20191025105022Z', 'modifytimestamp' => '20191025155317Z', 'safetytestdate' => '20201104', + 'degreecourse' => 'Ingegneria dell\'Ingegnerizzazione', 'signedsir' => 'true', 'haskey' => null, 'nsaccountlock' => 'true', @@ -72,7 +78,8 @@ class Ldap 'sshpublickey' => [], 'weeelabnickname' => [], 'description' => '', - 'telegramnickname' => 'brodino' + 'telegramnickname' => 'brodino', + 'mail' => 'brodino@example.com', ], 'bob' => [ 'uid' => 'bob', @@ -92,7 +99,8 @@ class Ldap 'sshpublickey' => [], 'weeelabnickname' => [], 'description' => '', - 'nsaccountlock' => null + 'nsaccountlock' => null, + 'mail' => 'bob@example.com', ], 'broski' => [ 'uid' => 'broski', @@ -103,6 +111,7 @@ class Ldap 'createtimestamp' => '20191025105022Z', 'modifytimestamp' => '20191025155317Z', 'safetytestdate' => '20201025', + 'degreecourse' => 'Ingegneria dell\'Ingegnerizzazione', 'signedsir' => null, 'haskey' => null, 'nsaccountlock' => null, @@ -111,7 +120,29 @@ class Ldap 'sshpublickey' => [], 'weeelabnickname' => [], 'description' => '', - 'telegramid' => '123456789' + 'telegramid' => '123456789', + 'mail' => 'bro@example.com', + ], + 'brobruh' => [ + 'uid' => 'brobruh', + 'cn' => 'Bro Bruh', + 'givenname' => 'Bro', + 'sn' => 'Bruh', + 'memberof' => ["cn=Admin,ou=Groups,dc=weeeopen,dc=it", "cn=Gente,ou=Groups,dc=weeeopen,dc=it"], + 'createtimestamp' => '20191025105022Z', + 'modifytimestamp' => '20191025155317Z', + 'safetytestdate' => '20210926', + 'degreecourse' => 'Ingegneria Disinformatica', + 'signedsir' => null, + 'haskey' => null, + 'nsaccountlock' => 'true', + 'schacpersonaluniquecode' => 's333444555666', + 'telegramnickname' => null, + 'sshpublickey' => [], + 'weeelabnickname' => [], + 'description' => '', + 'telegramid' => '12345678912345', + 'mail' => 'bro@bruh.example', ], ]; private const EXAMPLE_GROUPS = ['Admin', 'Persone', 'Cloud']; @@ -264,7 +295,7 @@ public function getUsers(array $attributes): array * @param string $uid UID to search * @param array|null $attributes Attributes to include in search result ("null" for all) * - * @return resource|null $sr from ldap_search or none if no users are found + * @return array|Result|null $sr from ldap_search or none if no users are found * @throws LdapException if cannot search or more than one user is found */ private function searchByUid(string $uid, ?array $attributes = null) @@ -484,6 +515,11 @@ public static function groupDnToName(string $dn): string throw new InvalidArgumentException("$dn is not a group DN"); } + public static function optionalBooleanToBool(array $attributes, string $var): bool + { + return isset($attributes[$var]) && $attributes[$var] === 'true'; + } + public function groupNamesToDn(array $names): array { if (count($names) <= 0) { diff --git a/src/Template.php b/src/Template.php index 13c92d0..063b9f3 100644 --- a/src/Template.php +++ b/src/Template.php @@ -31,6 +31,6 @@ public static function telegramColumn($nickname, $id): string public static function shortListEntry(string $uid, string $cn, ?string $schacpersonaluniquecode): string { $schacpersonaluniquecode = $schacpersonaluniquecode ?? 'no matricola'; - return /** @lang HTML */ "$cn, $schacpersonaluniquecode Get SIR"; + return /** @lang HTML */ "$cn, $schacpersonaluniquecode Sign SIR"; } } diff --git a/templates/index.php b/templates/index.php index 0d4bd64..b153128 100644 --- a/templates/index.php +++ b/templates/index.php @@ -2,11 +2,21 @@ /** @var $uid string */ /** @var $id string */ /** @var $name string */ +/** @var $signedSir bool */ +/** @var $error string|null */ $this->layout('base', ['title' => 'Welcome']) ?>

Crauto

Creatore e Rimuovitore Autogestito di Utenti che Tutto Offre

Hi , your username is and your ID is

+ + + + +

You need to sign your SIR! Sign the SIR

+

Enabled services

What can I access with this account?