2024-04-21 13:15:18 +02:00
|
|
|
package stirling.software.SPDF.config.security;
|
|
|
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
2024-10-14 22:34:41 +01:00
|
|
|
import stirling.software.SPDF.config.interfaces.ShowAdminInterface;
|
2024-04-21 13:15:18 +02:00
|
|
|
import stirling.software.SPDF.model.ApplicationProperties;
|
|
|
|
import stirling.software.SPDF.model.User;
|
|
|
|
import stirling.software.SPDF.repository.UserRepository;
|
|
|
|
|
|
|
|
@Service
|
|
|
|
class AppUpdateAuthService implements ShowAdminInterface {
|
|
|
|
|
2024-12-24 09:52:53 +00:00
|
|
|
private final UserRepository userRepository;
|
|
|
|
|
|
|
|
private final ApplicationProperties applicationProperties;
|
|
|
|
|
|
|
|
public AppUpdateAuthService(
|
|
|
|
UserRepository userRepository, ApplicationProperties applicationProperties) {
|
|
|
|
this.userRepository = userRepository;
|
|
|
|
this.applicationProperties = applicationProperties;
|
|
|
|
}
|
2024-04-21 13:15:18 +02:00
|
|
|
|
2024-06-07 04:38:10 +00:00
|
|
|
@Override
|
2024-04-21 13:15:18 +02:00
|
|
|
public boolean getShowUpdateOnlyAdmins() {
|
2024-09-13 16:42:38 +01:00
|
|
|
boolean showUpdate = applicationProperties.getSystem().isShowUpdate();
|
2024-04-21 13:15:18 +02:00
|
|
|
if (!showUpdate) {
|
|
|
|
return showUpdate;
|
|
|
|
}
|
|
|
|
boolean showUpdateOnlyAdmin = applicationProperties.getSystem().getShowUpdateOnlyAdmin();
|
|
|
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
|
|
if (authentication == null || !authentication.isAuthenticated()) {
|
|
|
|
return !showUpdateOnlyAdmin;
|
|
|
|
}
|
|
|
|
if (authentication.getName().equalsIgnoreCase("anonymousUser")) {
|
|
|
|
return !showUpdateOnlyAdmin;
|
|
|
|
}
|
|
|
|
Optional<User> user = userRepository.findByUsername(authentication.getName());
|
|
|
|
if (user.isPresent() && showUpdateOnlyAdmin) {
|
|
|
|
return "ROLE_ADMIN".equals(user.get().getRolesAsString());
|
|
|
|
}
|
|
|
|
return showUpdate;
|
|
|
|
}
|
|
|
|
}
|