commit what I've done

This commit is contained in:
Minecon724 2024-11-05 16:58:23 +01:00
parent 28f3522538
commit 024dc2b691
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
3 changed files with 23 additions and 16 deletions

View file

@ -8,10 +8,7 @@ import io.quarkus.security.identity.SecurityIdentity;
import io.vertx.core.http.HttpServerRequest; import io.vertx.core.http.HttpServerRequest;
import jakarta.inject.Inject; import jakarta.inject.Inject;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
import jakarta.ws.rs.Consumes; import jakarta.ws.rs.*;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap; import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.NewCookie; import jakarta.ws.rs.core.NewCookie;
@ -41,28 +38,35 @@ public class ActionResource {
@Inject @Inject
HttpServerRequest request; HttpServerRequest request;
@POST @GET
@Path("/create") @Path("/create")
public Response create(MultivaluedMap<String, String> formData) { public Response create() {
String title = formData.getFirst("title");
String content = formData.getFirst("content"); }
@POST
@Path("/create/{title:.+}")
public Response create(@PathParam("title") String title) {
Account account = accountService.getAccount(); Account account = accountService.getAccount();
try { try {
Page page = actionService.createPage(title, content, account); Page page = actionService.createPage(title, account);
return redirectService.page(page) return redirectService.page(page)
.cookie(new NewCookie.Builder("prefilledContent").value("").maxAge(0).build()) .cookie(new NewCookie.Builder("prefilledContent").value("").maxAge(0).build())
.status(RestResponse.Status.SEE_OTHER).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 // illegal title
String encodedMessage = URLEncoder.encode(e.getMessage(), StandardCharsets.UTF_8); String encodedMessage = URLEncoder.encode(e.getMessage(), StandardCharsets.UTF_8);
int errorType = e instanceof UnacceptableDataException ? 1 : 2; int errorType = 1;
return Response return Response
.temporaryRedirect(URI.create("/edit/%s?errorType=%d&error=%s".formatted(title, errorType, encodedMessage))) .temporaryRedirect(URI.create("/edit/%s?errorType=%d&error=%s".formatted(title, errorType, encodedMessage)))
.cookie(new NewCookie.Builder("prefilledContent").path("/edit/" + title).value(content).build()) .cookie(new NewCookie.Builder("prefilledContent").path("/edit/" + title).value(content).build())
.status(Response.Status.SEE_OTHER).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();
} }
} }

View file

@ -13,18 +13,18 @@ import jakarta.transaction.Transactional;
@ApplicationScoped @ApplicationScoped
public class ActionService { public class ActionService {
@Transactional @Transactional
Page createPage(String title, String content, Account account) { Page createPage(String title, Account account) throws UnacceptableDataException, DuplicateException {
//account = Account.findById(account.getName()); //account = Account.findById(account.getName());
// title and content are sanitized so only prohibit if necessary // title and content are sanitized so only prohibit if necessary
if (title.contains("/")) { if (title.contains("/")) {
throw new UnacceptableDataException("Title cannot contain slashes (/). Those are used for sub-pages."); throw new UnacceptableDataException("Title cannot contain slashes (/). Those are used for sub-pages.");
} else if (Page.findByTitle(title) != null) { } 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); Page page = new Page(title);
PageRevision revision = new PageRevision(page, content); // TODO PageRevision revision = new PageRevision(page, "This page is empty");
revision.setAuthor(account); revision.setAuthor(account);
page.setLatestRevision(revision); page.setLatestRevision(revision);

View file

@ -5,6 +5,7 @@ import eu.m724.talkpages.orm.entity.auth.Session;
import io.quarkus.arc.Arc; import io.quarkus.arc.Arc;
import io.quarkus.arc.InstanceHandle; import io.quarkus.arc.InstanceHandle;
import io.quarkus.qute.TemplateExtension; import io.quarkus.qute.TemplateExtension;
import io.quarkus.security.UnauthorizedException;
import io.quarkus.security.identity.CurrentIdentityAssociation; import io.quarkus.security.identity.CurrentIdentityAssociation;
import io.quarkus.security.identity.SecurityIdentity; import io.quarkus.security.identity.SecurityIdentity;
@ -17,12 +18,14 @@ public class AuthExtension {
public static Session session() { public static Session session() {
SecurityIdentity identity = getIdentity(); SecurityIdentity identity = getIdentity();
if (identity.isAnonymous()) throw new UnauthorizedException();
return identity.getAttribute("session"); return identity.getAttribute("session");
} }
public static String name() { public static String name() {
SecurityIdentity identity = getIdentity(); 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() { private static SecurityIdentity getIdentity() {