diff --git a/src/main/java/com/example/clientproject/service/LoggingService.java b/src/main/java/com/example/clientproject/service/LoggingService.java new file mode 100644 index 0000000000000000000000000000000000000000..aa453854b260c560b6cfa466c2f51a590796ebf5 --- /dev/null +++ b/src/main/java/com/example/clientproject/service/LoggingService.java @@ -0,0 +1,65 @@ +package com.example.clientproject.service; + +import com.example.clientproject.data.events.Events; +import com.example.clientproject.data.logs.Logs; +import com.example.clientproject.data.logs.LogsRepo; +import com.example.clientproject.service.Utils.JWTUtils; +import org.springframework.stereotype.Service; + +import javax.servlet.http.HttpSession; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +/** + * Service for all logging based methods + */ +@Service +public class LoggingService { + LogsRepo logsRepo; + JWTUtils jwtUtils; + + /** + * Constructor + * @param aLogsRepo - object of type LogsRepo + * @param aJWTUtils - object of type JWTUtils + */ + public LoggingService(LogsRepo aLogsRepo, JWTUtils aJWTUtils) { + jwtUtils = aJWTUtils; + logsRepo = aLogsRepo; + } + + /** + * Method for logging an event + * @param event - the event + * @param session - the session + * @param details - details of the event + */ + public void logEvent(Events event, HttpSession session, String details) { + // Instantiate a flagging variable + boolean superAdminStatus; + // If the session attribute "superAdmin" doesn't exist (super admin not logged in) + if (session.getAttribute("superAdmin") == null) { + // Set the flag to false + superAdminStatus = false; + // Else + } else { + // Set the flag to the state of the session attribute + superAdminStatus = (boolean) session.getAttribute("superAdmin"); + } + + // Instantiate a DateTimeFormatter with the correct format + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + // Create a new Log object + Logs newLog = new Logs( + details, + LocalDateTime.now().format(formatter), + superAdminStatus, + jwtUtils.getLoggedInUserRow(session).get(), + event + ); + + // Save the new log + logsRepo.save(newLog); + } +} diff --git a/src/main/java/com/example/clientproject/service/Utils/JWTUtils.java b/src/main/java/com/example/clientproject/service/Utils/JWTUtils.java index d306113a8480c918eac69b7ef4174fd44fea8149..fbfe4c7b5129cad9e9d647de706badee3c9ade66 100644 --- a/src/main/java/com/example/clientproject/service/Utils/JWTUtils.java +++ b/src/main/java/com/example/clientproject/service/Utils/JWTUtils.java @@ -1,5 +1,7 @@ package com.example.clientproject.service.Utils; +import com.example.clientproject.data.userPermissions.UserPermissions; +import com.example.clientproject.data.userPermissions.UserPermissionsRepo; import com.example.clientproject.data.users.Users; import com.example.clientproject.data.users.UsersRepo; import io.jsonwebtoken.Claims; @@ -23,9 +25,11 @@ import java.util.Optional; public class JWTUtils { private UsersRepo usersRepo; + private UserPermissionsRepo userPermRepo; - public JWTUtils(UsersRepo ausersRepo){ - usersRepo = ausersRepo; + public JWTUtils(UsersRepo aUsersRepo, UserPermissionsRepo aUserPermsRepo){ + usersRepo = aUsersRepo; + userPermRepo = aUserPermsRepo; } private String SECRET_KEY; @@ -91,6 +95,14 @@ public class JWTUtils { jwtTimeToLive // used to calculate expiration (claim = exp) ); + List<UserPermissions> userPermList = userPermRepo.findByUserId(userId); + for (UserPermissions u: userPermList) { + if (u.getAdminType().getAdminTypeId() == 3) { + setSuperAdmin(session, true); + break; + } + } + session.setAttribute("loginCredJWT", jwt); return jwt.toString(); } @@ -131,6 +143,14 @@ public class JWTUtils { } public void logOutUser(HttpSession session){ + if ((boolean) session.getAttribute("superAdmin")) { + setSuperAdmin(session, false); + } + session.removeAttribute("loginCredJWT"); } + + public void setSuperAdmin(HttpSession session, boolean status) { + session.setAttribute("superAdmin", status); + } }