fix(admin): refactor AdminController with explicit LoginDTO/RecipientDTO to prevent Jackson 500 error
Some checks failed
Deploy RIT Freshers Hub to VPS / Deploy to Live VPS (push) Has been cancelled
Some checks failed
Deploy RIT Freshers Hub to VPS / Deploy to Live VPS (push) Has been cancelled
This commit is contained in:
@@ -17,10 +17,27 @@ import java.util.*;
|
|||||||
public class AdminController {
|
public class AdminController {
|
||||||
|
|
||||||
@Value("${admin.username:ritadmin}")
|
@Value("${admin.username:ritadmin}")
|
||||||
private String adminUsername = "ritadmin";
|
private String adminUsername;
|
||||||
|
|
||||||
@Value("${admin.password:ritadmin2026!}")
|
@Value("${admin.password:ritadmin2026!}")
|
||||||
private String adminPassword = "ritadmin2026!";
|
private String adminPassword;
|
||||||
|
|
||||||
|
public static class LoginDTO {
|
||||||
|
private String username;
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
public String getUsername() { return username; }
|
||||||
|
public void setUsername(String username) { this.username = username; }
|
||||||
|
public String getPassword() { return password; }
|
||||||
|
public void setPassword(String password) { this.password = password; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class RecipientDTO {
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
public String getEmail() { return email; }
|
||||||
|
public void setEmail(String email) { this.email = email; }
|
||||||
|
}
|
||||||
|
|
||||||
private Path getRecipientsFilePath() {
|
private Path getRecipientsFilePath() {
|
||||||
Path vpsPath = Paths.get("/var/www/freshers-hub/scripts/recipients.txt");
|
Path vpsPath = Paths.get("/var/www/freshers-hub/scripts/recipients.txt");
|
||||||
@@ -36,44 +53,49 @@ public class AdminController {
|
|||||||
return localPath;
|
return localPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/login")
|
@RequestMapping(value = "/login", method = {RequestMethod.GET, RequestMethod.POST})
|
||||||
public ResponseEntity<Map<String, Object>> login(
|
public ResponseEntity<Map<String, Object>> login(
|
||||||
@RequestBody(required = false) Map<String, String> payload,
|
@RequestBody(required = false) LoginDTO dto,
|
||||||
@RequestParam(required = false) String user,
|
@RequestParam(required = false) String username,
|
||||||
@RequestParam(required = false) String pass) {
|
@RequestParam(required = false) String password) {
|
||||||
|
|
||||||
Map<String, Object> response = new HashMap<>();
|
Map<String, Object> response = new HashMap<>();
|
||||||
try {
|
try {
|
||||||
String username = "";
|
String inputUser = "";
|
||||||
String password = "";
|
String inputPass = "";
|
||||||
|
|
||||||
if (payload != null) {
|
if (dto != null) {
|
||||||
if (payload.get("username") != null) username = payload.get("username").trim();
|
if (dto.getUsername() != null) inputUser = dto.getUsername().trim();
|
||||||
if (payload.get("password") != null) password = payload.get("password").trim();
|
if (dto.getPassword() != null) inputPass = dto.getPassword().trim();
|
||||||
}
|
}
|
||||||
if (username.isEmpty() && user != null) username = user.trim();
|
if (inputUser.isEmpty() && username != null) inputUser = username.trim();
|
||||||
if (password.isEmpty() && pass != null) password = pass.trim();
|
if (inputPass.isEmpty() && password != null) inputPass = password.trim();
|
||||||
|
|
||||||
String expectedUser = (adminUsername != null && !adminUsername.isEmpty()) ? adminUsername : "ritadmin";
|
String expectedUser = "ritadmin";
|
||||||
String expectedPass = (adminPassword != null && !adminPassword.isEmpty()) ? adminPassword : "ritadmin2026!";
|
if (adminUsername != null && !adminUsername.trim().isEmpty()) {
|
||||||
|
expectedUser = adminUsername.trim();
|
||||||
|
}
|
||||||
|
|
||||||
boolean isUsernameValid = "ritadmin".equalsIgnoreCase(username) || expectedUser.equalsIgnoreCase(username);
|
String expectedPass = "ritadmin2026!";
|
||||||
boolean isPasswordValid = "ritadmin2026!".equals(password) || expectedPass.equals(password);
|
if (adminPassword != null && !adminPassword.trim().isEmpty()) {
|
||||||
|
expectedPass = adminPassword.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean isUsernameValid = "ritadmin".equalsIgnoreCase(inputUser) || expectedUser.equalsIgnoreCase(inputUser);
|
||||||
|
boolean isPasswordValid = "ritadmin2026!".equals(inputPass) || expectedPass.equals(inputPass);
|
||||||
|
|
||||||
if (isUsernameValid && isPasswordValid) {
|
if (isUsernameValid && isPasswordValid) {
|
||||||
response.put("success", true);
|
response.put("success", true);
|
||||||
response.put("message", "Admin login successful");
|
response.put("message", "Admin login successful");
|
||||||
response.put("token", "ADMIN_SESSION_TOKEN_RIT_2026");
|
response.put("token", "ADMIN_SESSION_TOKEN_RIT_2026");
|
||||||
return ResponseEntity.ok(response);
|
|
||||||
} else {
|
} else {
|
||||||
response.put("success", false);
|
response.put("success", false);
|
||||||
response.put("message", "Invalid admin username or password");
|
response.put("message", "Invalid admin username or password");
|
||||||
return ResponseEntity.ok(response);
|
|
||||||
}
|
}
|
||||||
|
return ResponseEntity.ok(response);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
|
||||||
response.put("success", false);
|
response.put("success", false);
|
||||||
response.put("message", "Internal server error: " + e.getMessage());
|
response.put("message", "Error during login: " + e.getMessage());
|
||||||
return ResponseEntity.ok(response);
|
return ResponseEntity.ok(response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -81,49 +103,39 @@ public class AdminController {
|
|||||||
@GetMapping("/recipients")
|
@GetMapping("/recipients")
|
||||||
public ResponseEntity<List<String>> getRecipients() {
|
public ResponseEntity<List<String>> getRecipients() {
|
||||||
Path path = getRecipientsFilePath();
|
Path path = getRecipientsFilePath();
|
||||||
List<String> recipients = new ArrayList<>();
|
List<String> recipients = getRecipientsList(path);
|
||||||
if (Files.exists(path)) {
|
|
||||||
try {
|
|
||||||
List<String> lines = Files.readAllLines(path);
|
|
||||||
for (String line : lines) {
|
|
||||||
String trimmed = line.trim();
|
|
||||||
if (!trimmed.isEmpty() && !trimmed.startsWith("#")) {
|
|
||||||
recipients.add(trimmed);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ResponseEntity.ok(recipients);
|
return ResponseEntity.ok(recipients);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/recipients")
|
@PostMapping("/recipients")
|
||||||
public ResponseEntity<Map<String, Object>> addRecipient(@RequestBody Map<String, String> payload) {
|
public ResponseEntity<Map<String, Object>> addRecipient(
|
||||||
String email = payload != null ? payload.get("email") : null;
|
@RequestBody(required = false) RecipientDTO dto,
|
||||||
Map<String, Object> response = new HashMap<>();
|
@RequestParam(required = false) String email) {
|
||||||
|
|
||||||
if (email == null || !email.contains("@")) {
|
Map<String, Object> response = new HashMap<>();
|
||||||
|
String targetEmail = (dto != null && dto.getEmail() != null) ? dto.getEmail() : email;
|
||||||
|
|
||||||
|
if (targetEmail == null || !targetEmail.contains("@")) {
|
||||||
response.put("success", false);
|
response.put("success", false);
|
||||||
response.put("message", "Please enter a valid email address");
|
response.put("message", "Please enter a valid email address");
|
||||||
return ResponseEntity.badRequest().body(response);
|
return ResponseEntity.badRequest().body(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
email = email.trim().toLowerCase();
|
targetEmail = targetEmail.trim().toLowerCase();
|
||||||
Path path = getRecipientsFilePath();
|
Path path = getRecipientsFilePath();
|
||||||
List<String> existing = getRecipientsList(path);
|
List<String> existing = getRecipientsList(path);
|
||||||
|
|
||||||
if (existing.contains(email)) {
|
if (existing.contains(targetEmail)) {
|
||||||
response.put("success", false);
|
response.put("success", false);
|
||||||
response.put("message", "Email is already subscribed to notifications");
|
response.put("message", "Email is already subscribed to notifications");
|
||||||
return ResponseEntity.badRequest().body(response);
|
return ResponseEntity.badRequest().body(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
existing.add(email);
|
existing.add(targetEmail);
|
||||||
saveRecipientsList(path, existing);
|
saveRecipientsList(path, existing);
|
||||||
|
|
||||||
response.put("success", true);
|
response.put("success", true);
|
||||||
response.put("message", "Added recipient: " + email);
|
response.put("message", "Added recipient: " + targetEmail);
|
||||||
response.put("recipients", existing);
|
response.put("recipients", existing);
|
||||||
return ResponseEntity.ok(response);
|
return ResponseEntity.ok(response);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user