Skip to content

Commit

Permalink
Add account deletion in admin area. Restore display to standalone in …
Browse files Browse the repository at this point in the history
…PWAs.
  • Loading branch information
Nonononoki committed Jul 1, 2021
1 parent 52c9f65 commit 6c6b4a6
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 91 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Alovoa aims to be the first widespread open-source dating web application on the
- Your most private data is securely encrypted

### How to build:
- Install a JDK (OpenJDK 11 is officially supported)
- Install a JDK >= 9 (OpenJDK 11 is officially supported)
- Install maven: https://maven.apache.org/install.html
- Setup a database (MariaDB is officially supported)
- Setup an email server or use an existing one (any provider with IMAP support should work)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.nonononoki.alovoa.component;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
Expand All @@ -15,10 +14,11 @@
public class RestExceptionHandler extends ResponseEntityExceptionHandler {

private static final Logger LOGGER = LoggerFactory.getLogger(RestExceptionHandler.class);

@ExceptionHandler
protected ResponseEntity<Object> handleConflict(Exception ex, WebRequest request) {
LOGGER.error(ex.getMessage());
return handleExceptionInternal(ex, ExceptionUtils.getStackTrace(ex), new HttpHeaders(), HttpStatus.CONFLICT, request);
return handleExceptionInternal(ex, ex.getMessage() /* ExceptionUtils.getStackTrace(ex) */, new HttpHeaders(),
HttpStatus.CONFLICT, request);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.nonononoki.alovoa.config;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationStartedEvent;
Expand All @@ -10,16 +12,46 @@
import com.nonononoki.alovoa.entity.User;
import com.nonononoki.alovoa.entity.user.Gender;
import com.nonononoki.alovoa.entity.user.UserIntention;
import com.nonononoki.alovoa.model.AlovoaException;
import com.nonononoki.alovoa.model.UserDeleteParams;
import com.nonononoki.alovoa.repo.ConversationRepository;
import com.nonononoki.alovoa.repo.GenderRepository;
import com.nonononoki.alovoa.repo.UserBlockRepository;
import com.nonononoki.alovoa.repo.UserHideRepository;
import com.nonononoki.alovoa.repo.UserIntentionRepository;
import com.nonononoki.alovoa.repo.UserLikeRepository;
import com.nonononoki.alovoa.repo.UserNotificationRepository;
import com.nonononoki.alovoa.repo.UserReportRepository;
import com.nonononoki.alovoa.repo.UserRepository;
import com.nonononoki.alovoa.service.AuthService;
import com.nonononoki.alovoa.service.UserService;

@Component
public class EventListenerConfig {

@Autowired
private AuthService authService;

@Autowired
private UserRepository userRepo;

@Autowired
private UserLikeRepository userLikeRepo;

@Autowired
private UserHideRepository userHideRepo;

@Autowired
private UserBlockRepository userBlockRepo;

@Autowired
private UserReportRepository userReportRepo;

@Autowired
private UserNotificationRepository userNotificationRepo;

@Autowired
private ConversationRepository conversationRepo;
@Autowired
private GenderRepository genderRepo;

Expand All @@ -36,12 +68,13 @@ public class EventListenerConfig {
private String adminKey;

@EventListener
public void handleContextRefresh(ApplicationStartedEvent event) {
public void handleContextRefresh(ApplicationStartedEvent event) throws AlovoaException {
setDefaultAdmin();
setDefaultGenders();
setDefaulIntentions();
removeInvalidUsers();
}

public void setDefaultAdmin() {
long noUsers = userRepo.count();
if (noUsers == 0) {
Expand All @@ -55,10 +88,9 @@ public void setDefaultAdmin() {
}

public void setDefaultGenders() {

long noGenders = genderRepo.count();



if (noGenders == 0) {
Gender male = new Gender();
male.setText("male");
Expand All @@ -72,9 +104,9 @@ public void setDefaultGenders() {
other.setText("other");
genderRepo.saveAndFlush(other);
}

}

public void setDefaulIntentions() {
long noIntentions = userIntentionRepo.count();
if (noIntentions == 0) {
Expand All @@ -92,4 +124,29 @@ public void setDefaulIntentions() {
}
}

public void removeInvalidUsers() throws AlovoaException {
List<User> users = userRepo.findAll();

UserDeleteParams userDeleteParam = UserDeleteParams.builder().conversationRepo(conversationRepo)
.userBlockRepo(userBlockRepo).userHideRepo(userHideRepo).userLikeRepo(userLikeRepo)
.userNotificationRepo(userNotificationRepo).userRepo(userRepo).userReportRepo(userReportRepo).build();

for (User user : users) {

if (!user.getEmail().contains("@")) {
UserService.removeUserDataCascading(user, userDeleteParam);

user = authService.getCurrentUser();
user = userRepo.saveAndFlush(user);
userRepo.delete(user);
userRepo.flush();

user = authService.getCurrentUser();
user = userRepo.saveAndFlush(user);
userRepo.delete(user);
userRepo.flush();
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.nonononoki.alovoa.model;

import lombok.Data;

@Data
public class AdminAccountDeleteDto {
private String email;
}
13 changes: 10 additions & 3 deletions src/main/java/com/nonononoki/alovoa/rest/AdminController.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nonononoki.alovoa.model.AdminAccountDeleteDto;
import com.nonononoki.alovoa.model.AlovoaException;
import com.nonononoki.alovoa.model.MailDto;
import com.nonononoki.alovoa.service.AdminService;
Expand All @@ -34,9 +35,9 @@ public void deleteReport(@PathVariable long id) throws AlovoaException {
}

@PostMapping("/ban-user/{id}")
public void banUser(@PathVariable String id) throws NumberFormatException, InvalidKeyException,
IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException,
InvalidAlgorithmParameterException, AlovoaException {
public void banUser(@PathVariable String id)
throws NumberFormatException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, AlovoaException {
adminService.banUser(id);
}

Expand All @@ -55,4 +56,10 @@ public void sendMailAll(@RequestBody MailDto dto) throws AlovoaException, Messag
adminService.sendMailAll(dto);
}

@PostMapping(value = "/delete-account", consumes = "application/json")
public void deleteAccount(@RequestBody AdminAccountDeleteDto dto)
throws AlovoaException, MessagingException, IOException {
adminService.deleteAccount(dto);
}

}
33 changes: 29 additions & 4 deletions src/main/java/com/nonononoki/alovoa/service/AdminService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.nonononoki.alovoa.entity.Contact;
import com.nonononoki.alovoa.entity.User;
import com.nonononoki.alovoa.entity.user.UserReport;
import com.nonononoki.alovoa.model.AdminAccountDeleteDto;
import com.nonononoki.alovoa.model.AlovoaException;
import com.nonononoki.alovoa.model.MailDto;
import com.nonononoki.alovoa.model.UserDeleteParams;
Expand Down Expand Up @@ -113,9 +114,9 @@ public void deleteReport(long id) throws AlovoaException {
userRepo.saveAndFlush(u);
}

public void banUser(String id) throws AlovoaException, NumberFormatException, InvalidKeyException,
IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException,
InvalidAlgorithmParameterException {
public void banUser(String id)
throws AlovoaException, NumberFormatException, InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException {

checkRights();

Expand All @@ -129,7 +130,7 @@ public void banUser(String id) throws AlovoaException, NumberFormatException, In
.userBlockRepo(userBlockRepo).userHideRepo(userHideRepo).userLikeRepo(userLikeRepo)
.userNotificationRepo(userNotificationRepo).userRepo(userRepo).userReportRepo(userReportRepo).build();

UserService.removeUserLinkedLists(user, userDeleteParam);
UserService.removeUserDataCascading(user, userDeleteParam);

user.setAudio(null);
user.setDates(null);
Expand Down Expand Up @@ -161,6 +162,30 @@ public void banUser(String id) throws AlovoaException, NumberFormatException, In
userRepo.saveAndFlush(user);
}

public void deleteAccount(AdminAccountDeleteDto dto) throws AlovoaException {
checkRights();

User user = userRepo.findByEmail(dto.getEmail());

if(user.isAdmin()) {
throw new AlovoaException("cannot_delete_admin");
}

UserDeleteParams userDeleteParam = UserDeleteParams.builder().conversationRepo(conversationRepo)
.userBlockRepo(userBlockRepo).userHideRepo(userHideRepo).userLikeRepo(userLikeRepo)
.userNotificationRepo(userNotificationRepo).userRepo(userRepo).userReportRepo(userReportRepo).build();

if (user != null) {
UserService.removeUserDataCascading(user, userDeleteParam);
user = authService.getCurrentUser();
user = userRepo.saveAndFlush(user);
userRepo.delete(user);
userRepo.flush();
}

throw new AlovoaException("user_not_found");
}

private void checkRights() throws AlovoaException {
if (!authService.getCurrentUser().isAdmin()) {
throw new AlovoaException("not_admin");
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/nonononoki/alovoa/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ public void deleteAccountConfirm(UserDeleteAccountDto dto)
.userBlockRepo(userBlockRepo).userHideRepo(userHideRepo).userLikeRepo(userLikeRepo)
.userNotificationRepo(userNotificationRepo).userRepo(userRepo).userReportRepo(userReportRepo).build();

removeUserLinkedLists(user, userDeleteParam);
removeUserDataCascading(user, userDeleteParam);

user = authService.getCurrentUser();
user = userRepo.saveAndFlush(user);
userRepo.delete(user);
Expand All @@ -235,7 +236,7 @@ public void deleteAccountConfirm(UserDeleteAccountDto dto)
mailService.sendAccountDeleteConfirm(user);
}

public static User removeUserLinkedLists(User user, UserDeleteParams userDeleteParam) {
public static User removeUserDataCascading(User user, UserDeleteParams userDeleteParam) {

UserRepository userRepo = userDeleteParam.getUserRepo();
UserLikeRepository userLikeRepo = userDeleteParam.getUserLikeRepo();
Expand Down Expand Up @@ -358,7 +359,6 @@ public static User removeUserLinkedLists(User user, UserDeleteParams userDeleteP

userRepo.flush();
conversationRepo.flush();

return user;
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ server.port=${PORT:8443}
#server.ssl.key-store-password=alovoa
#server.ssl.key-alias=alovoa

server.http2.enabled=true

spring.datasource.platform=mysql
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/alovoa?createDatabaseIfNotExist=true&serverTimezone=UTC&useLegacyDatetimeCode=false
Expand Down
Loading

0 comments on commit 6c6b4a6

Please sign in to comment.