Skip to content
Snippets Groups Projects
Commit f2137334 authored by Joshua Gill's avatar Joshua Gill
Browse files

LoggingService service class, new method to add a log to the "Logs" table when...

LoggingService service class, new method to add a log to the "Logs" table when given required data, new and altered methods in "JWTUtils" to allow for new "Super Admin" functionality required
parent bfe0f438
No related branches found
No related tags found
2 merge requests!114LoggingService service class, new method to add a log to the "Logs" table when...,!101merge
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);
}
}
package com.example.clientproject.service.Utils; 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.Users;
import com.example.clientproject.data.users.UsersRepo; import com.example.clientproject.data.users.UsersRepo;
import io.jsonwebtoken.Claims; import io.jsonwebtoken.Claims;
...@@ -23,9 +25,11 @@ import java.util.Optional; ...@@ -23,9 +25,11 @@ import java.util.Optional;
public class JWTUtils { public class JWTUtils {
private UsersRepo usersRepo; private UsersRepo usersRepo;
private UserPermissionsRepo userPermRepo;
public JWTUtils(UsersRepo ausersRepo){ public JWTUtils(UsersRepo aUsersRepo, UserPermissionsRepo aUserPermsRepo){
usersRepo = ausersRepo; usersRepo = aUsersRepo;
userPermRepo = aUserPermsRepo;
} }
private String SECRET_KEY; private String SECRET_KEY;
...@@ -91,6 +95,14 @@ public class JWTUtils { ...@@ -91,6 +95,14 @@ public class JWTUtils {
jwtTimeToLive // used to calculate expiration (claim = exp) 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); session.setAttribute("loginCredJWT", jwt);
return jwt.toString(); return jwt.toString();
} }
...@@ -131,6 +143,14 @@ public class JWTUtils { ...@@ -131,6 +143,14 @@ public class JWTUtils {
} }
public void logOutUser(HttpSession session){ public void logOutUser(HttpSession session){
if ((boolean) session.getAttribute("superAdmin")) {
setSuperAdmin(session, false);
}
session.removeAttribute("loginCredJWT"); session.removeAttribute("loginCredJWT");
} }
public void setSuperAdmin(HttpSession session, boolean status) {
session.setAttribute("superAdmin", status);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment