Skip to content

Commit

Permalink
add Lock_Screen config
Browse files Browse the repository at this point in the history
  • Loading branch information
codeurzebs committed Jun 9, 2024
1 parent 45ddf3a commit 353ba78
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.spiderdiplome.controllers.accountservlets.candidatpotentiel;

public class DashboardServlet {
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
Utilisateur existingUser = utilisateurDAO.findByMatricule(matricule);
if (existingUser == null) {
setErrorAndRedirect(req, resp, "Aucun utilisateur avec ce matricule.");
System.out.println("Aucun utilisateur avec ce matricule.");
return;
}

Expand All @@ -82,7 +83,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
try {
existingUser = updateUser(existingUser, phoneEmail, statusbd, role, firstName, lastName, password);
utilisateurDAO.update(existingUser);

System.out.println("Le compte a été mis à jour avec succès !");
setSuccessAndRedirect(req, resp, "Le compte a été mis à jour avec succès !");
} catch (Exception e) {
setErrorAndRedirect(req, resp, "Erreur lors de la mise à jour de l'utilisateur : " + e.getMessage());
Expand All @@ -102,7 +103,7 @@ private Utilisateur updateUser(Utilisateur user, String phoneEmail, int status,
user.setRole(role);
user.setNom(firstName);
user.setPrenom(lastName);
if (password != null && !password.isEmpty()) {
if (password != null) {
String hashedPassword = this.passwordHashing.hash(password, salt);
if (hashedPassword == null) {
throw new Exception("Erreur lors du hachage du mot de passe");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.spiderdiplome.controllers.accountservlets.superadmin;

import com.spiderdiplome.models.Utilisateur;
import com.spiderdiplome.repository.implement.UtilisateurDAOImpl;
import com.spiderdiplome.security.authentification.AuthenticationService;
import com.spiderdiplome.security.tokenjjwt.TokenProvider;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import javax.ws.rs.core.HttpHeaders;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(description = "Lock Screen Servlet", urlPatterns = {"/verrouiller"})
public class LockScreenServlet extends HttpServlet {

private UtilisateurDAOImpl utilisateurDAO;
private AuthenticationService authService;

@Override
public void init() throws ServletException {
super.init();
// Initialisation du servlet
utilisateurDAO = new UtilisateurDAOImpl();
authService = new AuthenticationService();
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession(false);
if (session != null && session.getAttribute("user") != null) {
Utilisateur utilisateur = (Utilisateur) session.getAttribute("user");
req.setAttribute("usermatricule", utilisateur.getMatricule());
session.invalidate();
this.getServletContext().getRequestDispatcher("/WEB-INF/views/v1/data/secure/superadmin-area/lock_screen.jsp").forward(req, resp);
} else {
this.getServletContext().getRequestDispatcher("/WEB-INF/views/login.jsp").forward(req, resp);
}
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String matricule = req.getParameter("matricule");
String password = req.getParameter("password");
try {
if (authService.authenticate(matricule, password)) {
handleSuccessfulAuthentication(req, resp, matricule);
} else {
forwardWithError(req, resp, "Le nom d'utilisateur ou le mot de passe que vous avez entré est incorrect. Veuillez réessayer.");
}
} catch (Exception e) {
forwardWithError(req, resp, "Certaines données que vous avez entrées ne sont pas valides. Veuillez vérifier et réessayer.");
}
}

private boolean isInvalid(String matricule, String password) {
return matricule == null || matricule.isEmpty() || password == null || password.isEmpty();
}

private boolean isUserAlreadyLoggedIn(HttpServletRequest req) {
HttpSession session = req.getSession(false);
return session != null && session.getAttribute("user") != null;
}

private void handleSuccessfulAuthentication(HttpServletRequest req, HttpServletResponse resp, String matricule) throws IOException {
Utilisateur utilisateur = utilisateurDAO.findByMatricule(matricule);
HttpSession session = req.getSession(true);
session.setAttribute("user", utilisateur);

TokenProvider tokenProvider = (TokenProvider) getServletContext().getAttribute("tokenProvider");
String token = tokenProvider.createToken(matricule);
session.setAttribute("token", token);

resp.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token);

Cookie userCookie = new Cookie("user_spiderdiplome", matricule);
userCookie.setMaxAge(24 * 60 * 60);
resp.addCookie(userCookie);

String redirectUrl = getRedirectUrl(utilisateur, token);
sendSuccessResponse(resp, redirectUrl);
}

private String getRedirectUrl(Utilisateur utilisateur, String token) {
switch (utilisateur.getRole()) {
case "superadmin":
return "tableau-de-bord?st=" + token;
case "user":
return "userPage.jsp";
default:
return "otherPage.jsp";
}
}

private void sendSuccessResponse(HttpServletResponse resp, String redirectUrl) throws IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println("<div class=\"alert alert-success\">\n" +
"\t\t\t\t\t\t\t<h3><strong>Authentification Reussie!</strong> Connexion en cours.......</h3>\n" +
"\t\t\t\t\t\t</div>");
out.println("<script>");
out.println("setTimeout(function(){");
out.println(" window.location.href = '" + redirectUrl + "';");
out.println("}, 3000);"); // Redirection après 3 secondes
out.println("</script>");
}

private void forwardWithError(HttpServletRequest req, HttpServletResponse resp, String errorMessage) throws ServletException, IOException {
req.setAttribute("errorMessage", "<div class=\"alert alert-danger\">\n" +
" <strong>Erreur d'authentification!</strong> " + errorMessage + "\n" +
"</div>");
this.getServletContext().getRequestDispatcher("/WEB-INF/views/login.jsp").forward(req, resp);
}

@Override
public void destroy() {
super.destroy();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="btn btn-sm pull-right" href="lock_screen.html">
<a class="btn btn-sm pull-right" href="verrouiller">
<i class="fa fa-lock"></i>
</a>
<a class="btn btn-sm pull-right logoutConfirm_open" href="#logoutConfirm">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Verrouillage</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">

<!-- Bootstrap core CSS -->
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">

<!-- Font Awesome -->
<link href="css/font-awesome.min.css" rel="stylesheet">

<!-- Perfect -->
<link href="css/app.min.css" rel="stylesheet">

</head>

<body style="background-color:#3a3a3a;">

<!--Modal-->
<div class="modal fade lock-screen-wrapper" id="lockScreen">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<div class="lock-screen-img">
<img src="img/user.jpg" alt="">
</div>

<div class="text-center m-top-sm">
<div class="h4 text-white">${usermatricule}</div>

<div class="input-group m-top-sm">
<form class="form-login" method="post" action="verrouiller?matricule=${usermatricule}">
<input type="password" required name="password" class="form-control text-sm" placeholder="Entrer votre mot de passe">
<span class="input-group-btn">
<button><i class="fa fa-arrow-right"></i></button>
</span>
</form>
</div>
</div>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<!-- Le javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->

<!-- Jquery -->
<script src="js/jquery-1.10.2.min.js"></script>

<!-- Bootstrap -->
<script src="bootstrap/js/bootstrap.min.js"></script>

<!-- Modernizr -->
<script src='js/modernizr.min.js'></script>

<!-- Pace -->
<script src='js/pace.min.js'></script>

<!-- Popup Overlay -->
<script src='js/jquery.popupoverlay.min.js'></script>

<!-- Slimscroll -->
<script src='js/jquery.slimscroll.min.js'></script>

<!-- Cookie -->
<script src='js/jquery.cookie.min.js'></script>

<!-- Perfect -->
<script src="js/app/app.js"></script>

<script>
$(function () {
$('#lockScreen').modal({
show: true,
backdrop: 'static'
})
});
</script>
</body>
</html>

0 comments on commit 353ba78

Please sign in to comment.