Multiple user support added

This commit is contained in:
Sidharth Prabhu
2026-04-21 12:59:07 +05:30
parent c4ca865181
commit 0c65542fb9
9 changed files with 518 additions and 67 deletions

View File

@@ -62,4 +62,31 @@ public class SystemAuthController {
userService.deleteManager(id); // Using existing delete logic
return ResponseEntity.noContent().build();
}
@GetMapping("/admins")
public ResponseEntity<List<SystemUser>> getAdmins() {
return ResponseEntity.ok(userService.getMasters());
}
@PostMapping("/admins")
public ResponseEntity<SystemUser> addAdmin(@RequestBody SystemUser admin) {
return ResponseEntity.ok(userService.createMaster(admin));
}
@PostMapping("/update-master")
public ResponseEntity<?> updateMaster(@RequestBody Map<String, Object> data) {
try {
Object idObj = data.get("id");
Long id = (idObj != null) ? Long.valueOf(idObj.toString()) : 0L;
String email = (String) data.get("email");
String password = (String) data.get("password");
String name = (String) data.get("name");
userService.updateMasterAccount(id, email, password, name);
return ResponseEntity.ok(Map.of("success", true, "message", "Credentials updated successfully"));
} catch (Exception e) {
return ResponseEntity.status(500).body(Map.of("success", false, "message", e.getMessage()));
}
}
}

View File

@@ -28,29 +28,10 @@ public class SystemUserService {
@PostConstruct
public void init() {
// Handle migration from old credentials if they exist
repository.findByEmail("admin@ritcanteen.com").ifPresent(oldUser -> {
boolean hasNewAdmin = repository.findByEmail("admin").isPresent();
if (hasNewAdmin) {
repository.delete(oldUser);
System.out.println(">>> REMOVED OBSOLETE MASTER USER: admin@ritcanteen.com");
} else {
oldUser.setEmail(masterUsername);
oldUser.setPassword(passwordEncoder.encode(masterPassword));
repository.save(oldUser);
System.out.println(">>> MIGRATED MASTER USER: admin / admin");
}
});
// Ensure 'admin' user exists and has the correct password
Optional<SystemUser> adminUser = repository.findByEmail(masterUsername);
if (adminUser.isPresent()) {
SystemUser admin = adminUser.get();
admin.setPassword(passwordEncoder.encode(masterPassword));
admin.setRole("MASTER");
repository.save(admin);
System.out.println(">>> UPDATED MASTER USER PASSWORD: " + masterUsername + " / " + masterPassword);
} else {
// Ensure at least one 'MASTER' user exists in the database
List<SystemUser> masters = repository.findByRole("MASTER");
if (masters.isEmpty()) {
SystemUser master = new SystemUser();
master.setName("Admin Master");
master.setEmail(masterUsername);
@@ -59,7 +40,9 @@ public class SystemUserService {
master.setPermissions(List.of("dashboard", "sale", "customers", "purchases", "inventory", "expense", "reports", "stores", "table", "wallet", "promotions", "feedback"));
master.setViewOnly(false);
repository.save(master);
System.out.println(">>> SEEDED MASTER USER: " + masterUsername + " / " + masterPassword);
System.out.println(">>> SEEDED DEFAULT MASTER USER (Failsafe Source): " + masterUsername + " / " + masterPassword);
} else {
System.out.println(">>> MASTER USER(S) FOUND IN DATABASE. Skipping default seeding.");
}
}
@@ -83,24 +66,81 @@ public class SystemUserService {
return repository.save(staff);
}
public List<SystemUser> getMasters() {
return repository.findByRole("MASTER");
}
public SystemUser createMaster(SystemUser admin) {
admin.setRole("MASTER");
admin.setPassword(passwordEncoder.encode(admin.getPassword()));
// Grant full permissions by default for new admins
admin.setPermissions(List.of("dashboard", "sale", "customers", "purchases", "inventory", "expense", "reports", "stores", "table", "wallet", "promotions", "feedback"));
return repository.save(admin);
}
public void deleteManager(Long id) {
if (id != null) {
repository.deleteById(id);
}
}
public void updateMasterAccount(Long id, String email, String password, String name) {
SystemUser user;
if (id == null || id == 0) {
// If failsafe user (ID 0) or null, try to find the first master in the database
user = repository.findByRole("MASTER").stream().findFirst()
.orElseGet(() -> {
SystemUser newMaster = new SystemUser();
newMaster.setRole("MASTER");
newMaster.setPermissions(List.of("dashboard", "sale", "customers", "purchases", "inventory", "expense", "reports", "stores", "table", "wallet", "promotions", "feedback"));
return newMaster;
});
} else {
user = repository.findById(id)
.orElseThrow(() -> new RuntimeException("Master account not found with ID: " + id));
}
if (email != null) user.setEmail(email);
if (password != null && !password.isEmpty()) {
user.setPassword(passwordEncoder.encode(password));
}
if (name != null) user.setName(name);
repository.save(user);
}
public Optional<SystemUser> authenticate(String email, String password) {
System.out.println(">>> Attempting authentication for: " + email);
// 1. Try Database First
Optional<SystemUser> user = repository.findByEmail(email);
if (user.isPresent()) {
boolean matches = passwordEncoder.matches(password, user.get().getPassword());
System.out.println(">>> User found. Password match: " + matches);
System.out.println(">>> User found in DB. Password match: " + matches);
if (matches) {
return user;
}
} else {
System.out.println(">>> User NOT found: " + email);
System.out.println(">>> User NOT found in DB. Checking Failsafe eligibility...");
// 2. Try Failsafe (Properties) - ONLY if no Master users exist in DB
List<SystemUser> masters = repository.findByRole("MASTER");
if (masters.isEmpty()) {
if (email.equals(masterUsername) && password.equals(masterPassword)) {
System.out.println(">>> FAILSAFE AUTHENTICATION SUCCESSFUL (No DB Master Found)");
SystemUser failsafeUser = new SystemUser();
failsafeUser.setId(0L);
failsafeUser.setName("Failsafe Admin");
failsafeUser.setEmail(masterUsername);
failsafeUser.setRole("MASTER");
failsafeUser.setPermissions(List.of("dashboard", "sale", "customers", "purchases", "inventory", "expense", "reports", "stores", "table", "wallet", "promotions", "feedback"));
return Optional.of(failsafeUser);
}
} else {
System.out.println(">>> Failsafe disabled because custom master account exists in database.");
}
}
return Optional.empty();
}
}