Skip to content

Commit

Permalink
Add default page to redirect on login in user config
Browse files Browse the repository at this point in the history
  • Loading branch information
HoverEpic committed Feb 15, 2024
1 parent cb7a4b3 commit b4ef8be
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 12 deletions.
6 changes: 5 additions & 1 deletion core/requetes.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ function utilisateurs_id(PDO $bdd, int $id): array {
prenom,
pass,
mail,
page_defaut,
nom,
niveau
FROM utilisateurs
Expand All @@ -108,6 +109,7 @@ function utilisateurs(PDO $bdd): array {
id,
prenom,
mail,
page_defaut,
nom,
niveau
FROM utilisateurs';
Expand Down Expand Up @@ -151,6 +153,7 @@ function utilisateur_update(PDO $bdd, array $utilisateur, int $id) {
nom = :nom,
prenom = :prenom,
mail = :mail,
page_defaut = :page_defaut,
niveau = :niveau,
id_last_hero = :id_editeur
WHERE
Expand All @@ -160,6 +163,7 @@ function utilisateur_update(PDO $bdd, array $utilisateur, int $id) {
$stmt->bindParam(':nom', $utilisateur['nom'], PDO::PARAM_STR);
$stmt->bindParam(':prenom', $utilisateur['prenom'], PDO::PARAM_STR);
$stmt->bindParam(':mail', $utilisateur['mail'], PDO::PARAM_STR);
$stmt->bindParam(':page_defaut', $utilisateur['page_defaut'], PDO::PARAM_STR);
$stmt->bindParam(':niveau', $utilisateur['niveau'], PDO::PARAM_STR);
$stmt->bindParam(':id_editeur', $_SESSION['id'], PDO::PARAM_INT);
$stmt->execute();
Expand Down Expand Up @@ -448,7 +452,7 @@ function structure_update(PDO $bdd, array $structure) {
// Return a valid user or an exception.
function login_user(PDO $bdd, string $email, string $password): array {
$passmd5 = md5($password);
$req = $bdd->prepare('SELECT id, niveau, nom, prenom, mail
$req = $bdd->prepare('SELECT id, niveau, nom, prenom, mail, page_defaut
FROM utilisateurs
WHERE mail = :mail
AND pass = :pass
Expand Down
4 changes: 3 additions & 1 deletion core/session.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function set_session(array $user, array $structure) {
$_SESSION['nom'] = $user['nom'];
$_SESSION['prenom'] = $user['prenom'];
$_SESSION['mail'] = $user['mail'];
$_SESSION['page_defaut'] = $user['page_defaut'];
$_SESSION['tva_active'] = $structure['tva_active'];
$_SESSION['taux_tva'] = $structure['taux_tva'];
$_SESSION['structure'] = $structure['nom'];
Expand Down Expand Up @@ -200,11 +201,12 @@ function new_droits(PDO $bdd, array $droits): string {
. droits($sorties, 's', $droits));
}

function new_utilisateur(string $nom, string $prenom, string $mail, string $droits, string $pass = ''): array {
function new_utilisateur(string $nom, string $prenom, string $mail, string $page_defaut, string $droits, string $pass = ''): array {
return [
'nom' => $nom,
'prenom' => $prenom,
'mail' => $mail,
'page_defaut' => $page_defaut,
'pass' => md5($pass),
'niveau' => $droits
];
Expand Down
17 changes: 10 additions & 7 deletions ifaces/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,16 @@ <h2 class="form-signin-heading">Veuillez vous connecter</h2>
'Content-Type': 'application/json; charset=utf-8'
},
body: JSON.stringify({ username, password })
}).then(status)
.then((json) => {
// redirection vers l'index en attendant de pouvoir faire mieux.
window.location.href = '../ifaces/index.php';
}).catch((ex) => {
div.setAttribute('style', 'width:80%; margin:auto; visibility: visible; display: block');
});
}).then(status).then((json) => {
// redirection vers l'index en attendant de pouvoir faire mieux.
if (json.user.page_defaut != '')
window.location.href = json.user.page_defaut;
else
window.location.href = '../ifaces/index.php';
console.log(status)
}).catch((ex) => {
div.setAttribute('style', 'width:80%; margin:auto; visibility: visible; display: block');
});
}, false);
}, false);
</script>
Expand Down
1 change: 1 addition & 0 deletions ifaces/utilisateurs.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ function _page_droit(array $utilisateur = []): array {

<div class="col-md-4">
<?= configCheckboxArea($droits) ?>
<?= textInput(['name' => 'page_defaut', 'text' => "Page par defaut:"], !is_null($utilisateur['page_defaut']) ? $utilisateur['page_defaut'] : 'index.php') ?>
</div>
<div class="col-md-4">
<?= configCheckboxArea($collectes) ?>
Expand Down
4 changes: 4 additions & 0 deletions moteur/dbconfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,9 @@ function updateDB(PDO $bdd) {
if (!count($bdd->query("SHOW COLUMNS FROM `type_dechets` LIKE 'position'")->fetchAll())) {
$bdd -> exec("ALTER TABLE `type_dechets` ADD COLUMN position int(11) NOT NULL DEFAULT '0'");
}
// Check for utilisateurs.page_defaut
if (!count($bdd->query("SHOW COLUMNS FROM `utilisateurs` LIKE 'page_defaut'")->fetchAll())) {
$bdd -> exec("ALTER TABLE `utilisateurs` ADD COLUMN page_defaut text");
}
// Check for other sql updates....
}
2 changes: 1 addition & 1 deletion moteur/inscription_post.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

if ($pass1 === $pass2) {
$droits = new_droits($bdd, $_POST);
$user = new_utilisateur($nom, $prenom, $mail, $droits, $pass1);
$user = new_utilisateur($nom, $prenom, $mail, '', $droits, $pass1);
utilisateur_insert($bdd, $user);
header('Location: ../ifaces/utilisateurs.php?msg=Utilisateur ajouté avec succes!');
} else {
Expand Down
2 changes: 1 addition & 1 deletion moteur/login_post.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
$structure = structure($bdd);
set_session($user, $structure);
http_response_code(200); // OK
echo json_encode(['status' => 'OK'], JSON_FORCE_OBJECT);
echo json_encode(['status' => 'OK', 'user' => $user], JSON_FORCE_OBJECT);
// A terme on revera un document json decrivant l'utilisateur connecter.
// echo(json_encode($user, JSON_NUMERIC_CHECK | JSON_FORCE_OBJECT));
} catch (Exception $e) {
Expand Down
2 changes: 1 addition & 1 deletion moteur/modification_utilisateur_post.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
if (is_valid_session() && is_allowed_users()) {
require_once '../moteur/dbconfig.php';
try {
utilisateur_update($bdd, new_utilisateur($_POST['nom'], $_POST['prenom'], $_POST['mail'], new_droits($bdd, $_POST)), $_POST['id']);
utilisateur_update($bdd, new_utilisateur($_POST['nom'], $_POST['prenom'], $_POST['mail'], $_POST['page_defaut'], new_droits($bdd, $_POST)), $_POST['id']);
header('Location: ../ifaces/edition_utilisateurs.php?msg=Utilisateur modifié avec succes!');
} catch (PDOException $e) {
if ($e->getCode() == '23000') {
Expand Down

0 comments on commit b4ef8be

Please sign in to comment.