Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,13 @@ public String getLoginResponse(HttpServletRequest request) {
throw new IEMRException("Authentication failed. Please log in again.");
}

// Validate the token first
Claims claims = jwtUtil.validateToken(jwtToken);
if (claims == null) {
logger.warn("Authentication failed: invalid or expired token.");
throw new IEMRException("Authentication failed. Please log in again.");
}

// Extract user ID from the JWT token
String userId = jwtUtil.getUserIdFromToken(jwtToken);

Expand Down Expand Up @@ -1230,4 +1237,85 @@ public ResponseEntity<?> getUserDetails(@PathVariable("userName") String userNam
}

}

@Operation(summary = "Unlock user account locked due to failed login attempts")
@RequestMapping(value = "/unlockUserAccount", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON, headers = "Authorization")
public String unlockUserAccount(@RequestBody String request, HttpServletRequest httpRequest) {
OutputResponse response = new OutputResponse();
try {
Long authenticatedUserId = getAuthenticatedUserId(httpRequest);
validateAdminPrivileges(authenticatedUserId);
Long userId = parseUserIdFromRequest(request);
boolean unlocked = iemrAdminUserServiceImpl.unlockUserAccount(userId);
response.setResponse(unlocked ? "User account successfully unlocked" : "User account was not locked");
} catch (Exception e) {
logger.error("Error unlocking user account: " + e.getMessage(), e);
response.setError(e);
}
return response.toString();
}

@Operation(summary = "Get user account lock status")
@RequestMapping(value = "/getUserLockStatus", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON, headers = "Authorization")
public String getUserLockStatus(@RequestBody String request, HttpServletRequest httpRequest) {
OutputResponse response = new OutputResponse();
try {
Long authenticatedUserId = getAuthenticatedUserId(httpRequest);
validateAdminPrivileges(authenticatedUserId);
Long userId = parseUserIdFromRequest(request);
String lockStatusJson = iemrAdminUserServiceImpl.getUserLockStatusJson(userId);
response.setResponse(lockStatusJson);
} catch (Exception e) {
logger.error("Error getting user lock status: " + e.getMessage(), e);
response.setError(e);
}
return response.toString();
}

private Long parseUserIdFromRequest(String request) throws IEMRException {
try {
JsonObject requestObj = JsonParser.parseString(request).getAsJsonObject();
if (!requestObj.has("userId") || requestObj.get("userId").isJsonNull()) {
throw new IEMRException("userId is required");
}
JsonElement userIdElement = requestObj.get("userId");
if (!userIdElement.isJsonPrimitive() || !userIdElement.getAsJsonPrimitive().isNumber()) {
throw new IEMRException("userId must be a number");
}
return userIdElement.getAsLong();
} catch (IEMRException e) {
throw e;
} catch (Exception e) {
throw new IEMRException("Invalid request body", e);
}
}

private Long getAuthenticatedUserId(HttpServletRequest httpRequest) throws IEMRException {
String authorization = httpRequest.getHeader("Authorization");
if (authorization != null && authorization.contains("Bearer ")) {
authorization = authorization.replace("Bearer ", "");
}
if (authorization == null || authorization.isEmpty()) {
throw new IEMRException("Authentication required");
}
try {
String sessionJson = sessionObject.getSessionObject(authorization);
if (sessionJson == null || sessionJson.isEmpty()) {
throw new IEMRException("Session expired. Please log in again.");
}
JSONObject session = new JSONObject(sessionJson);
return session.getLong("userID");
} catch (IEMRException e) {
throw e;
} catch (Exception e) {
throw new IEMRException("Authentication failed", e);
}
}

private void validateAdminPrivileges(Long userId) throws IEMRException {
if (!iemrAdminUserServiceImpl.hasAdminPrivileges(userId)) {
logger.warn("Unauthorized access attempt by userId: {}", userId);
throw new IEMRException("Access denied. Admin privileges required.");
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/iemr/common/data/users/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ public class User implements Serializable {
@Column(name = "dhistoken")
private String dhistoken;

@Expose
@Column(name = "lock_timestamp")
private Timestamp lockTimestamp;

/*
* protected User() { }
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ UserSecurityQMapping verifySecurityQuestionAnswers(@Param("UserID") Long UserID,

@Query("SELECT u FROM User u WHERE u.userID=5718")
User getAllExistingUsers();

User findByUserID(Long userID);

}
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ public List<ServiceRoleScreenMapping> getUserServiceRoleMappingForProvider(Integ

List<User> getUserIdbyUserName(String userName) throws IEMRException;

boolean unlockUserAccount(Long userId) throws IEMRException;

String getUserLockStatusJson(Long userId) throws IEMRException;

boolean hasAdminPrivileges(Long userId) throws IEMRException;


}
Loading