Skip to content

Commit

Permalink
Lint everything
Browse files Browse the repository at this point in the history
  • Loading branch information
lvps committed Apr 25, 2024
1 parent bcad9ff commit ecd9b02
Show file tree
Hide file tree
Showing 38 changed files with 713 additions and 620 deletions.
19 changes: 12 additions & 7 deletions public/authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,27 @@

$error = null;
try {
$ldap = new Ldap(CRAUTO_LDAP_URL, CRAUTO_LDAP_BIND_DN, CRAUTO_LDAP_PASSWORD, CRAUTO_LDAP_USERS_DN,
CRAUTO_LDAP_GROUPS_DN, CRAUTO_LDAP_STARTTLS);

if(isset($_POST) && !empty($_POST)) {
$ldap = new Ldap(
CRAUTO_LDAP_URL,
CRAUTO_LDAP_BIND_DN,
CRAUTO_LDAP_PASSWORD,
CRAUTO_LDAP_USERS_DN,
CRAUTO_LDAP_GROUPS_DN,
CRAUTO_LDAP_STARTTLS
);

if (isset($_POST) && !empty($_POST)) {
Validation::handlePasswordChangePost($ldap, $_SESSION['uid'], $_POST);
$_SESSION['success'] = 'Password updated successfully';
http_response_code(303);
header("Location: authentication.php");
exit;
}
} catch(LdapException | ValidationException | InvalidArgumentException $e) {
} catch (LdapException | ValidationException | InvalidArgumentException $e) {
$error = $e->getMessage();
}

if(isset($_SESSION['success'])) {
if (isset($_SESSION['success'])) {
$success = $_SESSION['success'];
unset($_SESSION['success']);
} else {
Expand All @@ -36,4 +42,3 @@
'error' => $error,
'success' => $success
]);

6 changes: 2 additions & 4 deletions public/groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

require '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
Authentication::requireLogin();
if(!Authentication::isAdmin()) {
if (!Authentication::isAdmin()) {
$template = Template::create();
echo $template->render('403');
exit;
Expand All @@ -17,7 +17,7 @@
try {
$ldap = new Ldap(CRAUTO_LDAP_URL, CRAUTO_LDAP_BIND_DN, CRAUTO_LDAP_PASSWORD, CRAUTO_LDAP_USERS_DN, CRAUTO_LDAP_GROUPS_DN, CRAUTO_LDAP_STARTTLS);
$users = $ldap->getUsersList(new DateTimeZone('Europe/Rome'));
} catch(LdapException $e) {
} catch (LdapException $e) {
$error = $e->getMessage();
}

Expand All @@ -27,5 +27,3 @@
'users' => $users,
'error' => $error,
]);


46 changes: 25 additions & 21 deletions public/people.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,30 @@

require '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
Authentication::requireLogin();
if(!Authentication::isAdmin()) {
if (!Authentication::isAdmin()) {
$template = Template::create();
echo $template->render('403');
exit;
}

if(isset($_GET['uid'])) {
$allowedAttributes = Validation::allowedAttributesAdmin;
$editableAttributes = array_combine(Validation::editableAttributesAdmin, Validation::editableAttributesAdmin);
if (isset($_GET['uid'])) {
$allowedAttributes = Validation::ALLOWED_ATTRIBUTES_ADMIN;
$editableAttributes = array_combine(Validation::EDITABLE_ATTRIBUTES_ADMIN, Validation::EDITABLE_ATTRIBUTES_ADMIN);

$targetUid = $_GET['uid'];

$attributes = [];
$allGroups = [];
$error = null;
try {
$ldap = new Ldap(CRAUTO_LDAP_URL, CRAUTO_LDAP_BIND_DN, CRAUTO_LDAP_PASSWORD, CRAUTO_LDAP_USERS_DN,
CRAUTO_LDAP_GROUPS_DN, CRAUTO_LDAP_STARTTLS);
$ldap = new Ldap(
CRAUTO_LDAP_URL,
CRAUTO_LDAP_BIND_DN,
CRAUTO_LDAP_PASSWORD,
CRAUTO_LDAP_USERS_DN,
CRAUTO_LDAP_GROUPS_DN,
CRAUTO_LDAP_STARTTLS
);
$attributes = $ldap->getUser($targetUid, array_merge($allowedAttributes, ['createtimestamp', 'modifytimestamp']));
$targetUid = $attributes['uid'] ?? $targetUid; // Canonicalize uid, or use the supplied one
// Do not move elsewhere, otherwise you get empty groups in case of errors above here
Expand All @@ -37,8 +43,8 @@
// useless...
$requireOldPasswordForChange = strtolower($_SESSION['uid']) === strtolower($attributes['uid']);

if(isset($_POST) && !empty($_POST)) {
if(isset($_POST['password1'])) {
if (isset($_POST) && !empty($_POST)) {
if (isset($_POST['password1'])) {
Validation::handlePasswordChangePost($ldap, $targetUid, $_POST, $requireOldPasswordForChange);
} else {
Validation::handleUserEditPost($editableAttributes, $ldap, $targetUid, $attributes);
Expand All @@ -48,12 +54,12 @@
header("Location: ${_SERVER['REQUEST_URI']}");
exit;
}
} catch(LdapException | ValidationException | InvalidArgumentException $e) {
} catch (LdapException | ValidationException | InvalidArgumentException $e) {
$error = $e->getMessage();
}

$groups = [];
foreach($attributes['memberof'] as $dn) {
foreach ($attributes['memberof'] as $dn) {
$groups[] = Ldap::groupDnToName($dn);
}
$attributes['memberof'] = $groups;
Expand All @@ -69,37 +75,37 @@
'editableAttributes' => $editableAttributes,
'allowedAttributes' => $allowedAttributes,
'adminRequireOldPassword' => $requireOldPasswordForChange ?? true,
'allGroups' => $allGroups
]);
'allGroups' => $allGroups
]);
} else {
$error = null;
$users = [];
$website = isset($_GET['for']) && $_GET['for'] == 'website';
try {
$ldap = new Ldap(CRAUTO_LDAP_URL, CRAUTO_LDAP_BIND_DN, CRAUTO_LDAP_PASSWORD, CRAUTO_LDAP_USERS_DN, CRAUTO_LDAP_GROUPS_DN, CRAUTO_LDAP_STARTTLS);
$users = $ldap->getUsersList(new DateTimeZone('Europe/Rome'), $website ? ['degreecourse', 'websitedescription'] : ['websitedescription']);
} catch(LdapException $e) {
} catch (LdapException $e) {
$error = $e->getMessage();
}

$template = Template::create();
$template->addData(['currentSection' => 'people'], 'navbar');

if($website) {
if ($website) {
$excludedGroups = explode(',', CRAUTO_WEBSITE_IGNORE_GROUPS);
$excludedGroups = array_combine($excludedGroups, $excludedGroups);
if(count($excludedGroups) > 0) {
if (count($excludedGroups) > 0) {
$usersFiltered = [];
foreach($users as $user) {
foreach ($users as $user) {
$groups = $user['memberof'] ?? [];
$exclude = false;
foreach($groups as $group) {
if(array_key_exists($group, $excludedGroups)) {
foreach ($groups as $group) {
if (array_key_exists($group, $excludedGroups)) {
$exclude = true;
break;
}
}
if(!$exclude) {
if (!$exclude) {
$usersFiltered[] = $user;
}
}
Expand All @@ -119,5 +125,3 @@
]);
}
}


28 changes: 17 additions & 11 deletions public/personal.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,31 @@
require '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
Authentication::requireLogin();

$allowedAttributes = Validation::allowedAttributesUser;
if(Authentication::isAdmin()) {
$editableAttributes = array_combine(Validation::editableAttributesAdmin, Validation::editableAttributesAdmin);
$allowedAttributes = Validation::ALLOWED_ATTRIBUTES_USER;
if (Authentication::isAdmin()) {
$editableAttributes = array_combine(Validation::EDITABLE_ATTRIBUTES_ADMIN, Validation::EDITABLE_ATTRIBUTES_ADMIN);
// Some attributes are editable, but only on the "people" page, not on the personal page, where they aren't even shown...
$editableAttributes = array_intersect($editableAttributes, $allowedAttributes);
} else {
$editableAttributes = array_combine(Validation::editableAttributesUser, Validation::editableAttributesUser);
$editableAttributes = array_combine(Validation::EDITABLE_ATTRIBUTES_USER, Validation::EDITABLE_ATTRIBUTES_USER);
}

$attributes = [];
$allGroups = [];
$error = null;
try {
$ldap = new Ldap(CRAUTO_LDAP_URL, CRAUTO_LDAP_BIND_DN, CRAUTO_LDAP_PASSWORD, CRAUTO_LDAP_USERS_DN,
CRAUTO_LDAP_GROUPS_DN, CRAUTO_LDAP_STARTTLS);
$ldap = new Ldap(
CRAUTO_LDAP_URL,
CRAUTO_LDAP_BIND_DN,
CRAUTO_LDAP_PASSWORD,
CRAUTO_LDAP_USERS_DN,
CRAUTO_LDAP_GROUPS_DN,
CRAUTO_LDAP_STARTTLS
);
$attributes = $ldap->getUser($_SESSION['uid'], $allowedAttributes);
$allGroups = $ldap->getGroups();

if(isset($_GET['download'])) {
if (isset($_GET['download'])) {
header('Content-Type: application/json');
header('Content-Transfer-Encoding: Binary');
header('Content-Description: File Transfer');
Expand All @@ -34,18 +40,18 @@
exit;
}

if(isset($_POST) && !empty($_POST)) {
if (isset($_POST) && !empty($_POST)) {
Validation::handleUserEditPost($editableAttributes, $ldap, $_SESSION['uid'], $attributes);
http_response_code(303);
header('Location: personal.php');
exit;
}
} catch(LdapException | ValidationException | InvalidArgumentException $e) {
} catch (LdapException | ValidationException | InvalidArgumentException $e) {
$error = $e->getMessage();
}

$groups = [];
foreach($attributes['memberof'] as $dn) {
foreach ($attributes['memberof'] as $dn) {
$groups[] = Ldap::groupDnToName($dn);
}
$attributes['memberof'] = $groups;
Expand All @@ -59,5 +65,5 @@
'attributes' => $attributes,
'allowedAttributes' => $allowedAttributes,
'editableAttributes' => $editableAttributes,
'allGroups' => $allGroups
'allGroups' => $allGroups
]);
28 changes: 17 additions & 11 deletions public/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';

$loggedin = Authentication::isLoggedIn();
if($loggedin) {
if ($loggedin) {
$template = Template::create();
$template->addData(['authenticated' => $loggedin, 'isAdmin' => $loggedin && Authentication::isAdmin()], 'navbar');
echo $template->render('403');
Expand Down Expand Up @@ -458,11 +458,17 @@
$template->addData(['authenticated' => $loggedin, 'isAdmin' => $loggedin && Authentication::isAdmin()], 'navbar');

try {
$ldap = new Ldap(CRAUTO_LDAP_URL, CRAUTO_LDAP_BIND_DN, CRAUTO_LDAP_PASSWORD, CRAUTO_LDAP_USERS_DN,
CRAUTO_LDAP_GROUPS_DN, CRAUTO_LDAP_STARTTLS);
if(isset($_GET['invite'])) {
$ldap = new Ldap(
CRAUTO_LDAP_URL,
CRAUTO_LDAP_BIND_DN,
CRAUTO_LDAP_PASSWORD,
CRAUTO_LDAP_USERS_DN,
CRAUTO_LDAP_GROUPS_DN,
CRAUTO_LDAP_STARTTLS
);
if (isset($_GET['invite'])) {
$defaultAttributes = $ldap->getInvitedUser($_GET['invite'], CRAUTO_LDAP_INVITES_DN);
if($defaultAttributes === null) {
if ($defaultAttributes === null) {
$template = Template::create();
echo $template->render('403', ['error' => 'Invalid invite code']);
exit;
Expand All @@ -472,29 +478,29 @@
echo $template->render('403', ['error' => 'Missing invite code']);
exit;
}
} catch(LdapException | ValidationException $e) {
} catch (LdapException | ValidationException $e) {
$error = $e->getMessage();
echo $template->render('500', ['error' => $error]);
exit;
}

// Invite code is valid and $defaultAttributes is available, if getting here
try {
if(isset($_POST) && !empty($_POST)) {
Validation::handleUserRegisterPost($_POST, Validation::allowedAttributesRegister, $ldap, $degreeCourses, $countries, $province);
if (isset($_POST) && !empty($_POST)) {
Validation::handleUserRegisterPost($_POST, Validation::ALLOWED_ATTRIBUTES_REGISTER, $ldap, $degreeCourses, $countries, $province);
$ldap->deleteInvite(CRAUTO_LDAP_INVITES_DN, $_GET['invite']);
http_response_code(303);
$_SESSION['register_done'] = true;
header('Location: register_done.php');
exit;
}
} catch(LdapException | ValidationException $e) {
} catch (LdapException | ValidationException $e) {
$error = $e->getMessage();
}


if(isset($defaultAttributes['degreecourse'])) {
if(!isset($degreeCourses[$defaultAttributes['degreecourse']])) {
if (isset($defaultAttributes['degreecourse'])) {
if (!isset($degreeCourses[$defaultAttributes['degreecourse']])) {
unset($defaultAttributes['degreecourse']);
}
}
Expand Down
3 changes: 1 addition & 2 deletions public/register_done.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

session_start();

if(isset($_SESSION['register_done'])) {
if (isset($_SESSION['register_done'])) {
unset($_SESSION['register_done']);
$template = Template::create();
echo $template->render('register_done');
Expand All @@ -15,4 +15,3 @@
$template->addData(['authenticated' => Authentication::isLoggedIn(), 'isAdmin' => Authentication::isLoggedIn() && Authentication::isAdmin()], 'navbar');
echo $template->render('403');
}

Loading

0 comments on commit ecd9b02

Please sign in to comment.