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;
|
|
|
|
|
2025-04-25 15:35:12 +02:00
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
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
|
2025-04-25 15:35:12 +02:00
|
|
|
@RequiredArgsConstructor
|
2024-04-21 13:15:18 +02:00
|
|
|
class AppUpdateAuthService implements ShowAdminInterface {
|
|
|
|
|
2024-12-24 09:52:53 +00:00
|
|
|
private final UserRepository userRepository;
|
|
|
|
|
|
|
|
private final ApplicationProperties applicationProperties;
|
|
|
|
|
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;
|
|
|
|
}
|
2025-04-25 15:35:12 +02:00
|
|
|
if ("anonymousUser".equalsIgnoreCase(authentication.getName())) {
|
2024-04-21 13:15:18 +02:00
|
|
|
return !showUpdateOnlyAdmin;
|
|
|
|
}
|
|
|
|
Optional<User> user = userRepository.findByUsername(authentication.getName());
|
|
|
|
if (user.isPresent() && showUpdateOnlyAdmin) {
|
|
|
|
return "ROLE_ADMIN".equals(user.get().getRolesAsString());
|
|
|
|
}
|
|
|
|
return showUpdate;
|
|
|
|
}
|
|
|
|
}
|