Skip to content

Commit

Permalink
Be able to delete your own account
Browse files Browse the repository at this point in the history
  • Loading branch information
Portals committed Jun 8, 2024
1 parent eb55d2c commit 3ede1d1
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package it.chalmers.gamma.adapter.primary.web;

import it.chalmers.gamma.app.user.MeFacade;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class DeleteYourAccountController {

private final MeFacade meFacade;

public DeleteYourAccountController(MeFacade meFacade) {
this.meFacade = meFacade;
}

public record DeleteYourAccountForm(String password) {}

@GetMapping("/delete-your-account")
public ModelAndView getDeleteYourAccount(
@RequestHeader(value = "HX-Request", required = false) boolean htmxRequest) {
ModelAndView mv = new ModelAndView();

if (htmxRequest) {
mv.setViewName("pages/delete-your-account");
} else {
mv.setViewName("index");
mv.addObject("page", "pages/delete-your-account");
}

mv.addObject("form", new DeleteYourAccountForm(""));

return mv;
}

@DeleteMapping("/delete-your-account")
public ModelAndView deleteYourAccount(
DeleteYourAccountForm form, final BindingResult bindingResult) {
try {
this.meFacade.deleteMe(form.password);
} catch (IllegalArgumentException e) {
bindingResult.addError(
new FieldError("form", "password", "Incorrect password"));

ModelAndView mv = new ModelAndView();

mv.setViewName("pages/delete-your-account");
mv.addObject("form", new DeleteYourAccountForm(""));
mv.addObject(BindingResult.MODEL_KEY_PREFIX + "form", bindingResult);

return mv;
}

return new ModelAndView("redirect:/login?deleted");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public ModelAndView getLogin(
@RequestParam(value = "error", required = false) String error,
@RequestParam(value = "logout", required = false) String logout,
@RequestParam(value = "authorizing", required = false) String authorizing,
@RequestParam(value = "deleted", required = false) String deleted,
@RequestHeader(value = "HX-Request", required = false) boolean htmxRequest,
@RequestParam(value = "throttle", required = false) String throttle,
HttpServletResponse response) {
Expand All @@ -36,10 +37,12 @@ public ModelAndView getLogin(

boolean isAuthorizing = authorizing != null;
boolean isThrottled = throttle != null;
boolean isDeleted = deleted != null;

mv.addObject("error", error);
mv.addObject("logout", logout);
mv.addObject("authorizing", isAuthorizing);
mv.addObject("deleted", isDeleted);
mv.addObject("throttle", isThrottled);

response.addHeader("HX-Retarget", "body");
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/it/chalmers/gamma/app/user/MeFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,10 @@ public void deleteMe(String password) {
try {
this.userRepository.delete(me.id());
} catch (UserRepository.UserNotFoundException e) {
e.printStackTrace();
throw new IllegalStateException();
}
} else {
throw new IllegalArgumentException();
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/resources/templates/pages/delete-your-account.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<header th:replace="~{common/header}"></header>
<main>
<article>
<header>
Deleting your account
</header>
<p>
Deleting your account can't be reversed.
</p>
<form th:object="${form}" id="delete-account" th:action="@{/delete-your-account}" th:method="delete">
<div th:replace="~{common/input :: passwordTextInput2(field='password', label='Confirm password')}"></div>
</form>
<footer>
<button form="delete-account">
Delete the account
</button>
</footer>
</article>
</main>
4 changes: 4 additions & 0 deletions app/src/main/resources/templates/pages/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ <h1>
You have been throttled for attempting to sign in too many times...
</p>

<p th:if="${deleted}">
Your account has been deleted.
</p>

<a href="/activate-cid">
Register
</a>
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/resources/templates/pages/me.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,25 @@
<button disabled class="outline" form="update-me-avatar">Upload avatar</button>
</footer>
</article>
<article>
<header>
Do you want to delete your account?
</header>
<p>
Here you can delete your account and prevent further access for clients you have accepted.
You have the right to ensure all of your data is removed.
Please email <a href="mailto:[email protected]">[email protected]</a>, along with:
</p>
<p>
UserId: <span th:text="${me.id()}"></span>
</p>
<p>
Cid: <span th:text="${me.cid()}"></span>
</p>
<footer>
<a th:href="@{/delete-your-account}" >
Proceed to deleting your account
</a>
</footer>
</article>
</main>

0 comments on commit 3ede1d1

Please sign in to comment.