Refactor accessors
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
package sh.rhiobet.lalafin.access;
|
||||
|
||||
import sh.rhiobet.lalafin.path.Path;
|
||||
|
||||
public interface AccessService {
|
||||
boolean checkAccess(Path path);
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package sh.rhiobet.lalafin.advent;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.inject.Named;
|
||||
import sh.rhiobet.lalafin.access.AccessService;
|
||||
import sh.rhiobet.lalafin.advent.AdventConfiguration.AdventEvent;
|
||||
import sh.rhiobet.lalafin.path.Path;
|
||||
|
||||
@ApplicationScoped
|
||||
@Named("advent")
|
||||
public class AdventAccessService implements AccessService {
|
||||
|
||||
@Inject
|
||||
AdventConfiguration adventConfiguration;
|
||||
|
||||
@Override
|
||||
public boolean checkAccess(Path path) {
|
||||
String pathUri = path.getURI();
|
||||
|
||||
Optional<AdventEvent> matchingAdvent = adventConfiguration.events().stream()
|
||||
.filter(e -> pathUri.startsWith(e.path()))
|
||||
.findFirst();
|
||||
|
||||
if (matchingAdvent.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
String dayString = pathUri.replaceAll(
|
||||
"^" + Pattern.quote(matchingAdvent.get().path()) + "/?($1)\\.?[^.]*$",
|
||||
"$1");
|
||||
try {
|
||||
int day = Integer.parseInt(dayString);
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
return ((now.getYear() > matchingAdvent.get().year())
|
||||
|| (now.getYear() == matchingAdvent.get().year()
|
||||
&& now.getMonthValue() > matchingAdvent.get().month())
|
||||
|| (now.getYear() == matchingAdvent.get().year()
|
||||
&& now.getMonthValue() == matchingAdvent.get().month()
|
||||
&& now.getDayOfMonth() >= day));
|
||||
} catch (NumberFormatException ignored) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package sh.rhiobet.lalafin.advent;
|
||||
|
||||
import java.util.List;
|
||||
import io.smallrye.config.ConfigMapping;
|
||||
|
||||
@ConfigMapping(prefix = "advent")
|
||||
public interface AdventConfiguration {
|
||||
public List<AdventEvent> events();
|
||||
|
||||
public static interface AdventEvent {
|
||||
public String path();
|
||||
public int year();
|
||||
public int month();
|
||||
}
|
||||
}
|
||||
17
src/main/java/sh/rhiobet/lalafin/file/FileConfiguration.java
Normal file
17
src/main/java/sh/rhiobet/lalafin/file/FileConfiguration.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package sh.rhiobet.lalafin.file;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import io.smallrye.config.ConfigMapping;
|
||||
|
||||
@ConfigMapping(prefix = "file")
|
||||
public interface FileConfiguration {
|
||||
public String directory();
|
||||
public Optional<List<String>> ignored();
|
||||
public List<Route> routes();
|
||||
|
||||
public static interface Route {
|
||||
public String path();
|
||||
public Optional<List<String>> roles();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package sh.rhiobet.lalafin.file;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import io.quarkus.security.identity.SecurityIdentity;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.inject.Named;
|
||||
import sh.rhiobet.lalafin.access.AccessService;
|
||||
import sh.rhiobet.lalafin.api.configuration.FileApiConfiguration;
|
||||
import sh.rhiobet.lalafin.api.configuration.FileApiConfiguration.Route;
|
||||
import sh.rhiobet.lalafin.path.Path;
|
||||
|
||||
@ApplicationScoped
|
||||
@Named("file/role")
|
||||
public class FileRoleAccessService implements AccessService {
|
||||
@Inject
|
||||
SecurityIdentity securityIdentity;
|
||||
|
||||
@Inject
|
||||
FileApiConfiguration fileApiConfiguration;
|
||||
|
||||
public boolean checkAccess(Path path) {
|
||||
String pathUri = path.getURI();
|
||||
List<Route> matchingRoutes = fileApiConfiguration.routes().stream()
|
||||
.filter(r -> pathUri.startsWith(r.path()))
|
||||
.toList();
|
||||
|
||||
|
||||
if (matchingRoutes.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return matchingRoutes.stream()
|
||||
.allMatch(r -> r.roles().isEmpty()
|
||||
|| this.securityIdentity.getRoles().containsAll(r.roles().get()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user