From e7beafc65b417be96fe230571da4b62e64ac23a6 Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Tue, 29 Oct 2024 14:39:03 +0100 Subject: [PATCH] yes --- .../eu/m724/talkpages/auth/AuthResource.java | 20 ++++++++++++----- .../eu/m724/talkpages/auth/AuthService.java | 12 +++++----- .../talkpages/orm/entity/auth/Account.java | 6 ++--- .../talkpages/orm/entity/auth/Session.java | 6 ++--- .../talkpages/page/action/AccountService.java | 22 +++++++++++++++++++ .../talkpages/page/action/ActionResource.java | 17 ++------------ .../templates/AuthResource/delete.html | 2 +- 7 files changed, 51 insertions(+), 34 deletions(-) diff --git a/src/main/java/eu/m724/talkpages/auth/AuthResource.java b/src/main/java/eu/m724/talkpages/auth/AuthResource.java index 9fa50cf..a370fd9 100644 --- a/src/main/java/eu/m724/talkpages/auth/AuthResource.java +++ b/src/main/java/eu/m724/talkpages/auth/AuthResource.java @@ -2,12 +2,11 @@ package eu.m724.talkpages.auth; import eu.m724.talkpages.orm.entity.auth.Account; import eu.m724.talkpages.orm.entity.auth.Session; -import eu.m724.talkpages.page.action.AccountService; import io.quarkus.qute.CheckedTemplate; import io.quarkus.qute.TemplateInstance; import io.quarkus.security.Authenticated; import io.quarkus.security.identity.SecurityIdentity; -import io.vertx.core.http.HttpClientRequest; +import io.smallrye.common.annotation.Blocking; import io.vertx.core.http.HttpServerRequest; import jakarta.inject.Inject; import jakarta.ws.rs.*; @@ -31,7 +30,7 @@ public class AuthResource { public static native TemplateInstance auth(String message); public static native TemplateInstance authenticated(String username); public static native TemplateInstance register(String username, String message); - public static native TemplateInstance delete(); + public static native TemplateInstance delete(long revisionCount); } @GET @@ -49,8 +48,19 @@ public class AuthResource { @GET @Path("/delete") - public TemplateInstance delete() { - return Templates.delete(); + @Blocking + public Response delete(@Context HttpServerRequest request) { + Account account; + if (identity.isAnonymous()) { + account = Account.findByName(request.remoteAddress().hostAddress()); + } else { + Session session = identity.getAttribute("session"); + account = session.getAccount(); + } + + if (account == null) return Response.status(Response.Status.NOT_FOUND).build(); + long revisionCount = account.getRevisionsCount(); + return Response.ok(Templates.delete(revisionCount)).build(); } @GET diff --git a/src/main/java/eu/m724/talkpages/auth/AuthService.java b/src/main/java/eu/m724/talkpages/auth/AuthService.java index d95ac77..cfdbf0a 100644 --- a/src/main/java/eu/m724/talkpages/auth/AuthService.java +++ b/src/main/java/eu/m724/talkpages/auth/AuthService.java @@ -33,10 +33,8 @@ public class AuthService { String hashedPassword = argon2.hash(10, 65536, 1, password); account = new Account(username, hashedPassword); - account.persistAndFlush(); - Session session = new Session(account); - session.persist(); + account.persistAndFlush(); return session; } @@ -48,12 +46,12 @@ public class AuthService { @Transactional void delete(Account account) { - account.getRevisions().forEach(pageRevision -> { - // TODO - }); + /* account.getRevisions().forEach(pageRevision -> { + pageRevision.setAuthor(null); + pageRevision.persist(); + });*/ // TODO delete user pages after they appear account.delete(); - account.persistAndFlush(); } @Transactional diff --git a/src/main/java/eu/m724/talkpages/orm/entity/auth/Account.java b/src/main/java/eu/m724/talkpages/orm/entity/auth/Account.java index 7c0a6fa..d405b31 100644 --- a/src/main/java/eu/m724/talkpages/orm/entity/auth/Account.java +++ b/src/main/java/eu/m724/talkpages/orm/entity/auth/Account.java @@ -2,13 +2,11 @@ package eu.m724.talkpages.orm.entity.auth; import eu.m724.talkpages.orm.entity.content.PageRevision; import io.quarkus.hibernate.orm.panache.PanacheEntity; -import io.quarkus.hibernate.orm.panache.PanacheEntityBase; import jakarta.persistence.*; import jakarta.transaction.Transactional; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -73,17 +71,19 @@ public class Account extends PanacheEntity { public boolean isSystemAccount() { return password == null; } - + @Transactional public List getRevisions() { return PageRevision.find("author", this).list(); } + @Transactional public long getRevisionsCount() { return PageRevision.count("author", this); } // Operations + @Transactional public static Account findByName(String name) { return Account.find("name", name).firstResult(); } diff --git a/src/main/java/eu/m724/talkpages/orm/entity/auth/Session.java b/src/main/java/eu/m724/talkpages/orm/entity/auth/Session.java index 9303158..20a644f 100644 --- a/src/main/java/eu/m724/talkpages/orm/entity/auth/Session.java +++ b/src/main/java/eu/m724/talkpages/orm/entity/auth/Session.java @@ -1,10 +1,8 @@ package eu.m724.talkpages.orm.entity.auth; import io.quarkus.hibernate.orm.panache.PanacheEntity; -import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; import jakarta.persistence.ManyToOne; -import jakarta.transaction.Transactional; import java.security.SecureRandom; import java.time.LocalDateTime; @@ -41,12 +39,14 @@ public class Session extends PanacheEntity { this.account = account; this.token = token; this.expires = expires; + + account.getSessions().add(this); } // Columns - @ManyToOne(cascade = CascadeType.ALL) + @ManyToOne private Account account; // TODO make a generator for this if possible diff --git a/src/main/java/eu/m724/talkpages/page/action/AccountService.java b/src/main/java/eu/m724/talkpages/page/action/AccountService.java index ae57dfb..a4b586d 100644 --- a/src/main/java/eu/m724/talkpages/page/action/AccountService.java +++ b/src/main/java/eu/m724/talkpages/page/action/AccountService.java @@ -1,11 +1,20 @@ package eu.m724.talkpages.page.action; import eu.m724.talkpages.orm.entity.auth.Account; +import eu.m724.talkpages.orm.entity.auth.Session; +import io.quarkus.security.identity.SecurityIdentity; +import io.vertx.core.http.HttpServerRequest; import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; import jakarta.transaction.Transactional; @ApplicationScoped public class AccountService { + @Inject + SecurityIdentity identity; + + @Inject + HttpServerRequest request; // TODO I think it would be better to accept InetAddress @Transactional @@ -21,4 +30,17 @@ public class AccountService { return account; } + @Transactional + public Account getAccount() { + Account account; + + if (identity.isAnonymous()) { + account = addressAccount(request.remoteAddress().hostAddress()); + } else { + Session session = identity.getAttribute("session"); + account = session.getAccount(); + } + + return account; + } } diff --git a/src/main/java/eu/m724/talkpages/page/action/ActionResource.java b/src/main/java/eu/m724/talkpages/page/action/ActionResource.java index d16dccc..2ec02e4 100644 --- a/src/main/java/eu/m724/talkpages/page/action/ActionResource.java +++ b/src/main/java/eu/m724/talkpages/page/action/ActionResource.java @@ -46,14 +46,7 @@ public class ActionResource { public Response create(MultivaluedMap formData) { String title = formData.getFirst("title"); String content = formData.getFirst("content"); - Account account; - - if (identity.isAnonymous()) { - account = accountService.addressAccount(request.remoteAddress().hostAddress()); - } else { - Session session = identity.getAttribute("session"); - account = session.getAccount(); - } + Account account = accountService.getAccount(); try { Page page = actionService.createPage(title, content, account); @@ -79,14 +72,8 @@ public class ActionResource { public Response edit(MultivaluedMap formData) { String title = formData.getFirst("title"); String content = formData.getFirst("content"); - Account account; - if (identity.isAnonymous()) { - account = accountService.addressAccount(request.remoteAddress().hostAddress()); - } else { - Session session = identity.getAttribute("session"); - account = session.getAccount(); - } + Account account = accountService.getAccount(); Page page = Page.findByTitle(title); diff --git a/src/main/resources/templates/AuthResource/delete.html b/src/main/resources/templates/AuthResource/delete.html index 2299173..23a4dd1 100644 --- a/src/main/resources/templates/AuthResource/delete.html +++ b/src/main/resources/templates/AuthResource/delete.html @@ -8,7 +8,7 @@
  • Your account {user:name} will be deleted.
  • Your user page and talk page, along with their subpages, and all revisions from all users, will be deleted.
  • -
  • Your {user:session.account.getRevisionsCount} authored revisions will remain, but your name will be replaced with a shared "Deleted User"
  • +
  • Your {revisionCount} authored revisions will remain, but your name will be replaced with a shared "Deleted User"
  • Your username will become available.
{#else}