From 024dc2b69118117a910ade19fb535b86a2d7a4a0 Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Tue, 5 Nov 2024 16:58:23 +0100 Subject: [PATCH] commit what I've done --- .../talkpages/page/action/ActionResource.java | 28 +++++++++++-------- .../talkpages/page/action/ActionService.java | 6 ++-- .../talkpages/template/AuthExtension.java | 5 +++- 3 files changed, 23 insertions(+), 16 deletions(-) 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 2ec02e4..2677327 100644 --- a/src/main/java/eu/m724/talkpages/page/action/ActionResource.java +++ b/src/main/java/eu/m724/talkpages/page/action/ActionResource.java @@ -8,10 +8,7 @@ import io.quarkus.security.identity.SecurityIdentity; import io.vertx.core.http.HttpServerRequest; import jakarta.inject.Inject; import jakarta.transaction.Transactional; -import jakarta.ws.rs.Consumes; -import jakarta.ws.rs.POST; -import jakarta.ws.rs.Path; -import jakarta.ws.rs.Produces; +import jakarta.ws.rs.*; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.MultivaluedMap; import jakarta.ws.rs.core.NewCookie; @@ -41,28 +38,35 @@ public class ActionResource { @Inject HttpServerRequest request; - @POST + @GET @Path("/create") - public Response create(MultivaluedMap formData) { - String title = formData.getFirst("title"); - String content = formData.getFirst("content"); + public Response create() { + + } + + @POST + @Path("/create/{title:.+}") + public Response create(@PathParam("title") String title) { Account account = accountService.getAccount(); try { - Page page = actionService.createPage(title, content, account); + Page page = actionService.createPage(title, account); return redirectService.page(page) .cookie(new NewCookie.Builder("prefilledContent").value("").maxAge(0).build()) .status(RestResponse.Status.SEE_OTHER).build(); - } catch (UnacceptableDataException | DuplicateException e) { // TODO I could reduce all this code + } catch (UnacceptableDataException e) { // TODO I could reduce all this code // illegal title String encodedMessage = URLEncoder.encode(e.getMessage(), StandardCharsets.UTF_8); - int errorType = e instanceof UnacceptableDataException ? 1 : 2; + int errorType = 1; return Response .temporaryRedirect(URI.create("/edit/%s?errorType=%d&error=%s".formatted(title, errorType, encodedMessage))) .cookie(new NewCookie.Builder("prefilledContent").path("/edit/" + title).value(content).build()) .status(Response.Status.SEE_OTHER).build(); - // TODO find a better, more concise way. Also maybe create a different type for each error. And consider if I should catch all Exceptions + } catch (DuplicateException e) { + return Response + .temporaryRedirect(URI.create("/create/%s?notice=duplicate".formatted(title))) // TODO handle that in template + .status(Response.Status.SEE_OTHER).build(); } } diff --git a/src/main/java/eu/m724/talkpages/page/action/ActionService.java b/src/main/java/eu/m724/talkpages/page/action/ActionService.java index 06486c9..7b92012 100644 --- a/src/main/java/eu/m724/talkpages/page/action/ActionService.java +++ b/src/main/java/eu/m724/talkpages/page/action/ActionService.java @@ -13,18 +13,18 @@ import jakarta.transaction.Transactional; @ApplicationScoped public class ActionService { @Transactional - Page createPage(String title, String content, Account account) { + Page createPage(String title, Account account) throws UnacceptableDataException, DuplicateException { //account = Account.findById(account.getName()); // title and content are sanitized so only prohibit if necessary if (title.contains("/")) { throw new UnacceptableDataException("Title cannot contain slashes (/). Those are used for sub-pages."); } else if (Page.findByTitle(title) != null) { - throw new DuplicateException("Page already exists, I made you edit it."); + throw new DuplicateException("Page already exists."); } Page page = new Page(title); - PageRevision revision = new PageRevision(page, content); // TODO + PageRevision revision = new PageRevision(page, "This page is empty"); revision.setAuthor(account); page.setLatestRevision(revision); diff --git a/src/main/java/eu/m724/talkpages/template/AuthExtension.java b/src/main/java/eu/m724/talkpages/template/AuthExtension.java index f1dec36..e34a4d6 100644 --- a/src/main/java/eu/m724/talkpages/template/AuthExtension.java +++ b/src/main/java/eu/m724/talkpages/template/AuthExtension.java @@ -5,6 +5,7 @@ import eu.m724.talkpages.orm.entity.auth.Session; import io.quarkus.arc.Arc; import io.quarkus.arc.InstanceHandle; import io.quarkus.qute.TemplateExtension; +import io.quarkus.security.UnauthorizedException; import io.quarkus.security.identity.CurrentIdentityAssociation; import io.quarkus.security.identity.SecurityIdentity; @@ -17,12 +18,14 @@ public class AuthExtension { public static Session session() { SecurityIdentity identity = getIdentity(); + if (identity.isAnonymous()) throw new UnauthorizedException(); return identity.getAttribute("session"); } public static String name() { SecurityIdentity identity = getIdentity(); - return identity.isAnonymous() ? identity.getAttribute("address") : identity.getPrincipal().getName(); + if (identity.isAnonymous()) throw new UnauthorizedException(); + return identity.getPrincipal().getName(); } private static SecurityIdentity getIdentity() {