mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-08-06 13:48:58 +02:00
team reformat
This commit is contained in:
parent
01d038199d
commit
704b8bb0c6
@ -31,6 +31,7 @@ import stirling.software.SPDF.model.exception.UnsupportedProviderException;
|
|||||||
import stirling.software.SPDF.repository.AuthorityRepository;
|
import stirling.software.SPDF.repository.AuthorityRepository;
|
||||||
import stirling.software.SPDF.repository.TeamRepository;
|
import stirling.software.SPDF.repository.TeamRepository;
|
||||||
import stirling.software.SPDF.repository.UserRepository;
|
import stirling.software.SPDF.repository.UserRepository;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@ -153,38 +154,54 @@ public class UserService implements UserServiceInterface {
|
|||||||
return userOpt.isPresent() && apiKey.equals(userOpt.get().getApiKey());
|
return userOpt.isPresent() && apiKey.equals(userOpt.get().getApiKey());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveUser(String username, AuthenticationType authenticationType,Long teamId)
|
public void saveUser(String username, AuthenticationType authenticationType, Long teamId)
|
||||||
throws IllegalArgumentException, SQLException, UnsupportedProviderException {
|
throws IllegalArgumentException, SQLException, UnsupportedProviderException {
|
||||||
saveUser(username, authenticationType, teamId, Role.USER.getRoleId());
|
saveUser(username, authenticationType, teamId, Role.USER.getRoleId());
|
||||||
}
|
}
|
||||||
|
|
||||||
public User saveUser(String username, AuthenticationType type)
|
/**
|
||||||
throws IllegalArgumentException, SQLException, UnsupportedProviderException {
|
* Resolves a team based on the provided information, with consistent error handling.
|
||||||
|
*
|
||||||
if (!isUsernameValid(username)) {
|
* @param teamId The ID of the team to find, may be null
|
||||||
throw new IllegalArgumentException(getInvalidUsernameMessage());
|
* @param defaultTeamSupplier A supplier that provides a default team when teamId is null
|
||||||
|
* @return The resolved Team object
|
||||||
|
* @throws IllegalArgumentException If the teamId is invalid
|
||||||
|
*/
|
||||||
|
private Team resolveTeam(Long teamId, Supplier<Team> defaultTeamSupplier) {
|
||||||
|
if (teamId == null) {
|
||||||
|
return defaultTeamSupplier.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
User user = new User();
|
return teamRepository.findById(teamId)
|
||||||
user.setUsername(username);
|
.orElseThrow(() -> new IllegalArgumentException("Invalid team ID: " + teamId));
|
||||||
user.setAuthenticationType(type);
|
}
|
||||||
user.setEnabled(true);
|
|
||||||
user.setFirstLogin(true);
|
/**
|
||||||
|
* Gets the default team, creating it if it doesn't exist.
|
||||||
String defaultRole = Role.USER.getRoleId();
|
*
|
||||||
user.addAuthority(new Authority(defaultRole, user));
|
* @return The default team
|
||||||
|
*/
|
||||||
Team defaultTeam = teamRepository.findByName("Default")
|
private Team getDefaultTeam() {
|
||||||
|
return teamRepository.findByName("Default")
|
||||||
.orElseGet(() -> {
|
.orElseGet(() -> {
|
||||||
Team team = new Team();
|
Team team = new Team();
|
||||||
team.setName("Default");
|
team.setName("Default");
|
||||||
return teamRepository.save(team);
|
return teamRepository.save(team);
|
||||||
});
|
});
|
||||||
user.setTeam(defaultTeam);
|
}
|
||||||
userRepository.save(user);
|
|
||||||
databaseService.exportDatabase();
|
public User saveUser(String username, AuthenticationType type)
|
||||||
|
throws IllegalArgumentException, SQLException, UnsupportedProviderException {
|
||||||
return user;
|
return saveUserCore(
|
||||||
|
username, // username
|
||||||
|
null, // password
|
||||||
|
type, // authenticationType
|
||||||
|
null, // teamId
|
||||||
|
null, // team
|
||||||
|
Role.USER.getRoleId(), // role
|
||||||
|
true, // firstLogin
|
||||||
|
true // enabled
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -201,108 +218,75 @@ public class UserService implements UserServiceInterface {
|
|||||||
return userRepository.save(user);
|
return userRepository.save(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public User saveUser(String username, AuthenticationType authenticationType,Team team, String role)
|
public User saveUser(String username, AuthenticationType authenticationType, Team team, String role)
|
||||||
throws IllegalArgumentException, SQLException, UnsupportedProviderException {
|
throws IllegalArgumentException, SQLException, UnsupportedProviderException {
|
||||||
if (!isUsernameValid(username)) {
|
return saveUserCore(
|
||||||
throw new IllegalArgumentException(getInvalidUsernameMessage());
|
username, // username
|
||||||
}
|
null, // password
|
||||||
User user = new User();
|
authenticationType, // authenticationType
|
||||||
user.setUsername(username);
|
null, // teamId
|
||||||
user.setEnabled(true);
|
team, // team
|
||||||
user.setFirstLogin(false);
|
role, // role
|
||||||
user.addAuthority(new Authority(role, user));
|
false, // firstLogin
|
||||||
user.setTeam(team);
|
true // enabled
|
||||||
user.setAuthenticationType(authenticationType);
|
);
|
||||||
userRepository.save(user);
|
|
||||||
databaseService.exportDatabase();
|
|
||||||
return user;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public User saveUser(String username, AuthenticationType authenticationType,Long teamId, String role)
|
public User saveUser(String username, AuthenticationType authenticationType, Long teamId, String role)
|
||||||
throws IllegalArgumentException, SQLException, UnsupportedProviderException {
|
throws IllegalArgumentException, SQLException, UnsupportedProviderException {
|
||||||
if (!isUsernameValid(username)) {
|
return saveUserCore(
|
||||||
throw new IllegalArgumentException(getInvalidUsernameMessage());
|
username, // username
|
||||||
}
|
null, // password
|
||||||
User user = new User();
|
authenticationType, // authenticationType
|
||||||
user.setUsername(username);
|
teamId, // teamId
|
||||||
user.setEnabled(true);
|
null, // team
|
||||||
user.setFirstLogin(false);
|
role, // role
|
||||||
user.addAuthority(new Authority(role, user));
|
false, // firstLogin
|
||||||
Optional<Team> optTeam = teamRepository.findById(teamId);
|
true // enabled
|
||||||
if(!optTeam.isPresent()) {
|
);
|
||||||
throw new IllegalArgumentException("Team ID was invalid, team not present");
|
|
||||||
}
|
|
||||||
user.setTeam(optTeam.get());
|
|
||||||
user.setAuthenticationType(authenticationType);
|
|
||||||
userRepository.save(user);
|
|
||||||
databaseService.exportDatabase();
|
|
||||||
return user;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public User saveUser(String username, String password, Long teamId)
|
public User saveUser(String username, String password, Long teamId)
|
||||||
throws IllegalArgumentException, SQLException, UnsupportedProviderException {
|
throws IllegalArgumentException, SQLException, UnsupportedProviderException {
|
||||||
|
return saveUserCore(
|
||||||
if (!isUsernameValid(username)) {
|
username, // username
|
||||||
throw new IllegalArgumentException(getInvalidUsernameMessage());
|
password, // password
|
||||||
}
|
AuthenticationType.WEB, // authenticationType
|
||||||
|
teamId, // teamId
|
||||||
// Fetch team or throw
|
null, // team
|
||||||
Team team = teamRepository.findById(teamId)
|
Role.USER.getRoleId(), // role
|
||||||
.orElseThrow(() -> new IllegalArgumentException("Invalid team ID: " + teamId));
|
false, // firstLogin
|
||||||
|
true // enabled
|
||||||
// Create user
|
);
|
||||||
User user = new User();
|
|
||||||
user.setUsername(username);
|
|
||||||
user.setPassword(passwordEncoder.encode(password));
|
|
||||||
user.setEnabled(true);
|
|
||||||
user.setAuthenticationType(AuthenticationType.WEB);
|
|
||||||
user.setTeam(team);
|
|
||||||
user.setFirstLogin(false); // or true depending on your policy
|
|
||||||
|
|
||||||
// Assign default USER role
|
|
||||||
user.addAuthority(new Authority(Role.USER.getRoleId(), user));
|
|
||||||
|
|
||||||
// Save user
|
|
||||||
userRepository.save(user);
|
|
||||||
databaseService.exportDatabase();
|
|
||||||
|
|
||||||
return user;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public User saveUser(String username, String password, Team team, String role, boolean firstLogin)
|
public User saveUser(String username, String password, Team team, String role, boolean firstLogin)
|
||||||
throws IllegalArgumentException, SQLException, UnsupportedProviderException {
|
throws IllegalArgumentException, SQLException, UnsupportedProviderException {
|
||||||
if (!isUsernameValid(username)) {
|
return saveUserCore(
|
||||||
throw new IllegalArgumentException(getInvalidUsernameMessage());
|
username, // username
|
||||||
}
|
password, // password
|
||||||
User user = new User();
|
AuthenticationType.WEB, // authenticationType
|
||||||
user.setUsername(username);
|
null, // teamId
|
||||||
user.setPassword(passwordEncoder.encode(password));
|
team, // team
|
||||||
user.addAuthority(new Authority(role, user));
|
role, // role
|
||||||
user.setEnabled(true);
|
firstLogin, // firstLogin
|
||||||
user.setTeam(team);
|
true // enabled
|
||||||
user.setAuthenticationType(AuthenticationType.WEB);
|
);
|
||||||
user.setFirstLogin(firstLogin);
|
|
||||||
userRepository.save(user);
|
|
||||||
databaseService.exportDatabase();
|
|
||||||
return user;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public User saveUser(String username, String password, Long teamId, String role, boolean firstLogin)
|
public User saveUser(String username, String password, Long teamId, String role, boolean firstLogin)
|
||||||
throws IllegalArgumentException, SQLException, UnsupportedProviderException {
|
throws IllegalArgumentException, SQLException, UnsupportedProviderException {
|
||||||
if (!isUsernameValid(username)) {
|
return saveUserCore(
|
||||||
throw new IllegalArgumentException(getInvalidUsernameMessage());
|
username, // username
|
||||||
}
|
password, // password
|
||||||
User user = new User();
|
AuthenticationType.WEB, // authenticationType
|
||||||
user.setUsername(username);
|
teamId, // teamId
|
||||||
user.setPassword(passwordEncoder.encode(password));
|
null, // team
|
||||||
user.addAuthority(new Authority(role, user));
|
role, // role
|
||||||
user.setEnabled(true);
|
firstLogin, // firstLogin
|
||||||
user.setAuthenticationType(AuthenticationType.WEB);
|
true // enabled
|
||||||
user.setFirstLogin(firstLogin);
|
);
|
||||||
userRepository.save(user);
|
|
||||||
databaseService.exportDatabase();
|
|
||||||
return user;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveUser(String username, String password, Long teamId, String role)
|
public void saveUser(String username, String password, Long teamId, String role)
|
||||||
@ -310,20 +294,18 @@ public class UserService implements UserServiceInterface {
|
|||||||
saveUser(username, password, teamId , role, false);
|
saveUser(username, password, teamId , role, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveUser(String username, String password,Long teamId, boolean firstLogin, boolean enabled)
|
public void saveUser(String username, String password, Long teamId, boolean firstLogin, boolean enabled)
|
||||||
throws IllegalArgumentException, SQLException, UnsupportedProviderException {
|
throws IllegalArgumentException, SQLException, UnsupportedProviderException {
|
||||||
if (!isUsernameValid(username)) {
|
saveUserCore(
|
||||||
throw new IllegalArgumentException(getInvalidUsernameMessage());
|
username, // username
|
||||||
}
|
password, // password
|
||||||
User user = new User();
|
AuthenticationType.WEB, // authenticationType
|
||||||
user.setUsername(username);
|
teamId, // teamId
|
||||||
user.setPassword(passwordEncoder.encode(password));
|
null, // team
|
||||||
user.addAuthority(new Authority(Role.USER.getRoleId(), user));
|
Role.USER.getRoleId(), // role
|
||||||
user.setEnabled(enabled);
|
firstLogin, // firstLogin
|
||||||
user.setAuthenticationType(AuthenticationType.WEB);
|
enabled // enabled
|
||||||
user.setFirstLogin(firstLogin);
|
);
|
||||||
userRepository.save(user);
|
|
||||||
databaseService.exportDatabase();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteUser(String username) {
|
public void deleteUser(String username) {
|
||||||
@ -457,6 +439,77 @@ public class UserService implements UserServiceInterface {
|
|||||||
return (isValidSimpleUsername || isValidEmail) && !notAllowedUser;
|
return (isValidSimpleUsername || isValidEmail) && !notAllowedUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Core implementation for saving a user with all possible parameters.
|
||||||
|
* This method centralizes the common logic for all saveUser variants.
|
||||||
|
*
|
||||||
|
* @param username Username for the new user
|
||||||
|
* @param password Password for the user (may be null for SSO/OAuth users)
|
||||||
|
* @param authenticationType Type of authentication (WEB, SSO, etc.)
|
||||||
|
* @param teamId ID of the team to assign (may be null to use default)
|
||||||
|
* @param team Team object to assign (takes precedence over teamId if both provided)
|
||||||
|
* @param role Role to assign to the user
|
||||||
|
* @param firstLogin Whether this is the user's first login
|
||||||
|
* @param enabled Whether the user account is enabled
|
||||||
|
* @return The saved User object
|
||||||
|
* @throws IllegalArgumentException If username is invalid or team is invalid
|
||||||
|
* @throws SQLException If database operation fails
|
||||||
|
* @throws UnsupportedProviderException If provider is not supported
|
||||||
|
*/
|
||||||
|
private User saveUserCore(
|
||||||
|
String username,
|
||||||
|
String password,
|
||||||
|
AuthenticationType authenticationType,
|
||||||
|
Long teamId,
|
||||||
|
Team team,
|
||||||
|
String role,
|
||||||
|
boolean firstLogin,
|
||||||
|
boolean enabled)
|
||||||
|
throws IllegalArgumentException, SQLException, UnsupportedProviderException {
|
||||||
|
|
||||||
|
if (!isUsernameValid(username)) {
|
||||||
|
throw new IllegalArgumentException(getInvalidUsernameMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
User user = new User();
|
||||||
|
user.setUsername(username);
|
||||||
|
|
||||||
|
// Set password if provided
|
||||||
|
if (password != null && !password.isEmpty()) {
|
||||||
|
user.setPassword(passwordEncoder.encode(password));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set authentication type
|
||||||
|
user.setAuthenticationType(authenticationType);
|
||||||
|
|
||||||
|
// Set enabled status
|
||||||
|
user.setEnabled(enabled);
|
||||||
|
|
||||||
|
// Set first login flag
|
||||||
|
user.setFirstLogin(firstLogin);
|
||||||
|
|
||||||
|
// Set role (authority)
|
||||||
|
if (role == null) {
|
||||||
|
role = Role.USER.getRoleId();
|
||||||
|
}
|
||||||
|
user.addAuthority(new Authority(role, user));
|
||||||
|
|
||||||
|
// Resolve and set team
|
||||||
|
if (team != null) {
|
||||||
|
user.setTeam(team);
|
||||||
|
} else {
|
||||||
|
user.setTeam(resolveTeam(teamId, this::getDefaultTeam));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save user
|
||||||
|
userRepository.save(user);
|
||||||
|
|
||||||
|
// Export database
|
||||||
|
databaseService.exportDatabase();
|
||||||
|
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
private String getInvalidUsernameMessage() {
|
private String getInvalidUsernameMessage() {
|
||||||
return messageSource.getMessage(
|
return messageSource.getMessage(
|
||||||
"invalidUsernameMessage", null, LocaleContextHolder.getLocale());
|
"invalidUsernameMessage", null, LocaleContextHolder.getLocale());
|
||||||
|
@ -210,20 +210,18 @@ public class UserController {
|
|||||||
// If the role ID is not valid, redirect with an error message
|
// If the role ID is not valid, redirect with an error message
|
||||||
return new RedirectView("/adminSettings?messageType=invalidRole", true);
|
return new RedirectView("/adminSettings?messageType=invalidRole", true);
|
||||||
}
|
}
|
||||||
Optional<Team> team = teamId != null ? teamRepository.findById(teamId) : Optional.empty();
|
|
||||||
User newUser;
|
User newUser;
|
||||||
|
|
||||||
if (authType.equalsIgnoreCase(AuthenticationType.SSO.toString())) {
|
if (authType.equalsIgnoreCase(AuthenticationType.SSO.toString())) {
|
||||||
newUser = userService.saveUser(username, AuthenticationType.SSO, teamId,role);
|
newUser = userService.saveUser(username, AuthenticationType.SSO, teamId, role);
|
||||||
} else {
|
} else {
|
||||||
if (password.isBlank()) {
|
if (password.isBlank()) {
|
||||||
return new RedirectView("/adminSettings?messageType=invalidPassword", true);
|
return new RedirectView("/adminSettings?messageType=invalidPassword", true);
|
||||||
}
|
}
|
||||||
newUser = userService.saveUser(username, password, teamId, role, forceChange);
|
newUser = userService.saveUser(username, password, teamId, role, forceChange);
|
||||||
}
|
}
|
||||||
|
|
||||||
team.ifPresent(newUser::setTeam);
|
// The team is already set and saved by the saveUser methods
|
||||||
userService.saveUser(newUser); // Persist with team
|
|
||||||
|
|
||||||
return new RedirectView(
|
return new RedirectView(
|
||||||
"/adminSettings", // Redirect to account page after adding the user
|
"/adminSettings", // Redirect to account page after adding the user
|
||||||
|
Loading…
Reference in New Issue
Block a user