From 61985fb80673b318a9a9b6bfa345b670ac0e1da9 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 12 Nov 2023 22:44:30 +0100 Subject: [PATCH] Updates --- build.gradle | 4 +-- src/de/steamwar/sql/NodeDownload.java | 43 ++++++++++++-------------- src/de/steamwar/sql/SchematicNode.java | 42 +++++++++++++++++++++---- 3 files changed, 58 insertions(+), 31 deletions(-) diff --git a/build.gradle b/build.gradle index d5f000a..88a7743 100644 --- a/build.gradle +++ b/build.gradle @@ -44,8 +44,8 @@ ext { compileJava.options.encoding = 'UTF-8' -sourceCompatibility = 1.8 -targetCompatibility = 1.8 +sourceCompatibility = 17 +targetCompatibility = 17 sourceSets { main { diff --git a/src/de/steamwar/sql/NodeDownload.java b/src/de/steamwar/sql/NodeDownload.java index 666cf68..54d81b5 100644 --- a/src/de/steamwar/sql/NodeDownload.java +++ b/src/de/steamwar/sql/NodeDownload.java @@ -20,9 +20,11 @@ package de.steamwar.sql; import de.steamwar.sql.internal.Field; +import de.steamwar.sql.internal.SelectStatement; import de.steamwar.sql.internal.Statement; import de.steamwar.sql.internal.Table; import lombok.AllArgsConstructor; +import lombok.Getter; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -30,13 +32,13 @@ import java.sql.Timestamp; import java.time.Instant; @AllArgsConstructor +@Getter public class NodeDownload { - - private static final char[] HEX = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; - private static final String LINK_BASE = "https://steamwar.de/download.php?schem="; - private static final Table table = new Table<>(NodeDownload.class); private static final Statement insert = table.insertFields("NodeId", "Link"); + private static final SelectStatement select = table.selectFields("Link"); + private static final SelectStatement getId = table.select(Table.PRIMARY); + private static final Statement delete = table.delete(Table.PRIMARY); @Field(keys = {Table.PRIMARY}) private final int nodeId; @@ -45,25 +47,20 @@ public class NodeDownload { @Field(def = "CURRENT_TIMESTAMP") private final Timestamp timestamp; - public static String getLink(SchematicNode schem){ - if(schem.isDir()) - throw new SecurityException("Can not Download Directorys"); - MessageDigest digest; - try { - digest = MessageDigest.getInstance("SHA-1"); - } catch (NoSuchAlgorithmException e) { - throw new SecurityException(e); - } - digest.reset(); - digest.update((Instant.now().toString() + schem.getOwner() + schem.getId()).getBytes()); - String hash = base16encode(digest.digest()); - insert.update(schem.getId(), hash); - return LINK_BASE + hash; + public static NodeDownload addCode(SchematicNode node, String link) { + insert.update(node.getId(), link); + return get(node.getId()); } - public static String base16encode(byte[] byteArray) { - StringBuilder hexBuffer = new StringBuilder(byteArray.length * 2); - for (byte b : byteArray) - hexBuffer.append(HEX[(b >>> 4) & 0xF]).append(HEX[b & 0xF]); - return hexBuffer.toString(); + + public static NodeDownload get(int nodeId) { + return getId.select(nodeId); + } + + public static NodeDownload get(String link) { + return select.select(link); + } + + public void delete() { + delete.update(nodeId); } } diff --git a/src/de/steamwar/sql/SchematicNode.java b/src/de/steamwar/sql/SchematicNode.java index 6aa83e1..b9f86d8 100644 --- a/src/de/steamwar/sql/SchematicNode.java +++ b/src/de/steamwar/sql/SchematicNode.java @@ -383,8 +383,8 @@ public class SchematicNode { } public int getRank() { - if(isDir()) - throw new SecurityException("Node is Directory"); + if (isDir()) + return 0; return nodeRank; } @@ -400,8 +400,8 @@ public class SchematicNode { } public SchematicType getSchemtype() { - if(isDir()) - throw new SecurityException("Is Directory"); + if (isDir()) + return null; return nodeType; } @@ -443,8 +443,25 @@ public class SchematicNode { return SchemElo.getElo(this, season); } - public boolean accessibleByUser(int user) { - return NodeMember.getNodeMember(nodeId, user) != null; + public boolean accessibleByUser(SteamwarUser user) { + if (user.getId() == nodeOwner) { + return true; + } + + if (parentNode == null) { + return false; + } + + SchematicNode parent = getParentNode(); + while (parent != null) { + NodeMember member = NodeMember.getNodeMember(nodeId, user.getId()); + if (member != null || (parent.getOwner() == user.getId() && parent.parentNode == null)) { + return true; + } + parent = parent.getParentNode(); + } + + return false; } public Set getMembers() { @@ -506,6 +523,19 @@ public class SchematicNode { return builder.toString(); } + public List> generateBreadcrumbsMap(SteamwarUser user) { + List> map = new ArrayList<>(); + Optional currentNode = Optional.of(this); + if(currentNode.map(SchematicNode::isDir).orElse(false)) { + map.add(Map.entry(getName(), getId())); + } + while (currentNode.isPresent()) { + currentNode = currentNode.flatMap(schematicNode -> Optional.ofNullable(NodeMember.getNodeMember(schematicNode.getId(), effectiveOwner)).map(NodeMember::getParent).orElse(schematicNode.getOptionalParent())).map(SchematicNode::getSchematicNode); + currentNode.ifPresent(node -> map.add(0, Map.entry(node.getName(), node.getId()))); + } + return map; + } + private static final List FORBIDDEN_NAMES = Collections.unmodifiableList(Arrays.asList("public")); public static boolean invalidSchemName(String[] layers) { for (String layer : layers) {