This commit is contained in:
Minecon724 2024-10-29 14:39:03 +01:00
parent 6d56e7b2a0
commit e7beafc65b
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
7 changed files with 51 additions and 34 deletions

View file

@ -2,12 +2,11 @@ package eu.m724.talkpages.auth;
import eu.m724.talkpages.orm.entity.auth.Account; import eu.m724.talkpages.orm.entity.auth.Account;
import eu.m724.talkpages.orm.entity.auth.Session; import eu.m724.talkpages.orm.entity.auth.Session;
import eu.m724.talkpages.page.action.AccountService;
import io.quarkus.qute.CheckedTemplate; import io.quarkus.qute.CheckedTemplate;
import io.quarkus.qute.TemplateInstance; import io.quarkus.qute.TemplateInstance;
import io.quarkus.security.Authenticated; import io.quarkus.security.Authenticated;
import io.quarkus.security.identity.SecurityIdentity; 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 io.vertx.core.http.HttpServerRequest;
import jakarta.inject.Inject; import jakarta.inject.Inject;
import jakarta.ws.rs.*; import jakarta.ws.rs.*;
@ -31,7 +30,7 @@ public class AuthResource {
public static native TemplateInstance auth(String message); public static native TemplateInstance auth(String message);
public static native TemplateInstance authenticated(String username); public static native TemplateInstance authenticated(String username);
public static native TemplateInstance register(String username, String message); public static native TemplateInstance register(String username, String message);
public static native TemplateInstance delete(); public static native TemplateInstance delete(long revisionCount);
} }
@GET @GET
@ -49,8 +48,19 @@ public class AuthResource {
@GET @GET
@Path("/delete") @Path("/delete")
public TemplateInstance delete() { @Blocking
return Templates.delete(); 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 @GET

View file

@ -33,10 +33,8 @@ public class AuthService {
String hashedPassword = argon2.hash(10, 65536, 1, password); String hashedPassword = argon2.hash(10, 65536, 1, password);
account = new Account(username, hashedPassword); account = new Account(username, hashedPassword);
account.persistAndFlush();
Session session = new Session(account); Session session = new Session(account);
session.persist(); account.persistAndFlush();
return session; return session;
} }
@ -48,12 +46,12 @@ public class AuthService {
@Transactional @Transactional
void delete(Account account) { void delete(Account account) {
account.getRevisions().forEach(pageRevision -> { /* account.getRevisions().forEach(pageRevision -> {
// TODO pageRevision.setAuthor(null);
}); pageRevision.persist();
});*/
// TODO delete user pages after they appear // TODO delete user pages after they appear
account.delete(); account.delete();
account.persistAndFlush();
} }
@Transactional @Transactional

View file

@ -2,13 +2,11 @@ package eu.m724.talkpages.orm.entity.auth;
import eu.m724.talkpages.orm.entity.content.PageRevision; import eu.m724.talkpages.orm.entity.content.PageRevision;
import io.quarkus.hibernate.orm.panache.PanacheEntity; import io.quarkus.hibernate.orm.panache.PanacheEntity;
import io.quarkus.hibernate.orm.panache.PanacheEntityBase;
import jakarta.persistence.*; import jakarta.persistence.*;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -73,17 +71,19 @@ public class Account extends PanacheEntity {
public boolean isSystemAccount() { return password == null; } public boolean isSystemAccount() { return password == null; }
@Transactional
public List<PageRevision> getRevisions() { public List<PageRevision> getRevisions() {
return PageRevision.find("author", this).list(); return PageRevision.find("author", this).list();
} }
@Transactional
public long getRevisionsCount() { public long getRevisionsCount() {
return PageRevision.count("author", this); return PageRevision.count("author", this);
} }
// Operations // Operations
@Transactional
public static Account findByName(String name) { public static Account findByName(String name) {
return Account.find("name", name).firstResult(); return Account.find("name", name).firstResult();
} }

View file

@ -1,10 +1,8 @@
package eu.m724.talkpages.orm.entity.auth; package eu.m724.talkpages.orm.entity.auth;
import io.quarkus.hibernate.orm.panache.PanacheEntity; import io.quarkus.hibernate.orm.panache.PanacheEntity;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.ManyToOne; import jakarta.persistence.ManyToOne;
import jakarta.transaction.Transactional;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@ -41,12 +39,14 @@ public class Session extends PanacheEntity {
this.account = account; this.account = account;
this.token = token; this.token = token;
this.expires = expires; this.expires = expires;
account.getSessions().add(this);
} }
// Columns // Columns
@ManyToOne(cascade = CascadeType.ALL) @ManyToOne
private Account account; private Account account;
// TODO make a generator for this if possible // TODO make a generator for this if possible

View file

@ -1,11 +1,20 @@
package eu.m724.talkpages.page.action; package eu.m724.talkpages.page.action;
import eu.m724.talkpages.orm.entity.auth.Account; 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.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
@ApplicationScoped @ApplicationScoped
public class AccountService { public class AccountService {
@Inject
SecurityIdentity identity;
@Inject
HttpServerRequest request;
// TODO I think it would be better to accept InetAddress // TODO I think it would be better to accept InetAddress
@Transactional @Transactional
@ -21,4 +30,17 @@ public class AccountService {
return account; 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;
}
} }

View file

@ -46,14 +46,7 @@ public class ActionResource {
public Response create(MultivaluedMap<String, String> formData) { public Response create(MultivaluedMap<String, String> formData) {
String title = formData.getFirst("title"); String title = formData.getFirst("title");
String content = formData.getFirst("content"); String content = formData.getFirst("content");
Account account; Account account = accountService.getAccount();
if (identity.isAnonymous()) {
account = accountService.addressAccount(request.remoteAddress().hostAddress());
} else {
Session session = identity.getAttribute("session");
account = session.getAccount();
}
try { try {
Page page = actionService.createPage(title, content, account); Page page = actionService.createPage(title, content, account);
@ -79,14 +72,8 @@ public class ActionResource {
public Response edit(MultivaluedMap<String, String> formData) { public Response edit(MultivaluedMap<String, String> formData) {
String title = formData.getFirst("title"); String title = formData.getFirst("title");
String content = formData.getFirst("content"); String content = formData.getFirst("content");
Account account;
if (identity.isAnonymous()) { Account account = accountService.getAccount();
account = accountService.addressAccount(request.remoteAddress().hostAddress());
} else {
Session session = identity.getAttribute("session");
account = session.getAccount();
}
Page page = Page.findByTitle(title); Page page = Page.findByTitle(title);

View file

@ -8,7 +8,7 @@
<ul> <ul>
<li>Your account <strong>{user:name}</strong> will be deleted.</li> <li>Your account <strong>{user:name}</strong> will be deleted.</li>
<li>Your user page and talk page, along with their subpages, and all revisions from all users, will be deleted.</li> <li>Your user page and talk page, along with their subpages, and all revisions from all users, will be deleted.</li>
<li>Your <strong>{user:session.account.getRevisionsCount}</strong> authored revisions will remain, but your name will be replaced with a shared "Deleted User"</li> <!-- TODO optimize the size operation --> <li>Your <strong>{revisionCount}</strong> authored revisions will remain, but your name will be replaced with a shared "Deleted User"</li> <!-- TODO optimize the size operation -->
<li>Your username will become available.</li> <li>Your username will become available.</li>
</ul> </ul>
{#else} {#else}