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 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<String, String> 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();
}
}

View file

@ -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);

View file

@ -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() {