Compare commits
2 Commits
3cc928ebc8
...
8c0bb2ce4a
| Author | SHA1 | Date | |
|---|---|---|---|
| 8c0bb2ce4a | |||
| 5924ca5647 |
@@ -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()));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -72,12 +72,24 @@ public final class ZipEntryPath implements Path {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InputStream getInputStream() throws IOException {
|
public InputStream getInputStream() throws IOException {
|
||||||
ZipFile zipFile = new ZipFile(this.zipFilePath.absolutePath.toFile());
|
if (this.zipFilePath.exists()) {
|
||||||
return new ZipEntryInputStream(zipFile,
|
ZipFile zipFile = new ZipFile(this.zipFilePath.absolutePath.toFile());
|
||||||
zipFile.getInputStream(zipFile.getEntry(this.entryName)));
|
ZipEntry zipEntry = zipFile.getEntry(this.entryName);
|
||||||
|
if (zipEntry != null) {
|
||||||
|
return new ZipEntryInputStream(zipFile, zipFile.getInputStream(zipEntry));
|
||||||
|
} else {
|
||||||
|
throw new IOException("Zip entry does not exist.");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new IOException("Zip file does not exist.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void fetchEntryData() {
|
private void fetchEntryData() {
|
||||||
|
if (!this.zipFilePath.exists()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try (ZipFile zipFile = new ZipFile(this.zipFilePath.absolutePath.toFile())) {
|
try (ZipFile zipFile = new ZipFile(this.zipFilePath.absolutePath.toFile())) {
|
||||||
ZipEntry zipEntry = zipFile.getEntry(this.entryName);
|
ZipEntry zipEntry = zipFile.getEntry(this.entryName);
|
||||||
this.exists = zipEntry != null;
|
this.exists = zipEntry != null;
|
||||||
|
|||||||
Reference in New Issue
Block a user