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.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

View file

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

View file

@ -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<PageRevision> 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();
}

View file

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

View file

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

View file

@ -46,14 +46,7 @@ public class ActionResource {
public Response create(MultivaluedMap<String, String> 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<String, String> 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);

View file

@ -8,7 +8,7 @@
<ul>
<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 <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>
</ul>
{#else}