Added delete user handler in admin #13

This commit is contained in:
Rohan Sircar 2020-07-30 23:22:57 +05:30
parent dce2e3ece3
commit 40cc234b83
6 changed files with 59 additions and 4 deletions

View File

@ -103,6 +103,11 @@
<artifactId>flyway-core</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.spencerwi</groupId>
<artifactId>Either.java</artifactId>
<version>2.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>

View File

@ -2,6 +2,7 @@ import moment from "moment";
import { capitalize } from "../../../common/util/Util";
import { AdminUserDTO } from "../../../common/dto/AdminUserDTO";
import log from "loglevel";
import { createApiHeaders } from "../../../common/ajax/util";
export async function viewUsers(authToken: string) {
// const users = await getOtherUsers(authToken);
@ -46,7 +47,18 @@ export async function viewUsers(authToken: string) {
extend: "selectedSingle",
text: "Delete User",
action: (e, dt, button, config) => {
log.error("Not implemented yet");
const selectedRow = dt.row({ selected: true });
const username = (selectedRow.data() as AdminUserDTO).userName;
fetch(`/api/admin/delete/users/${username}`, {
headers: createApiHeaders(authToken),
method: "DELETE",
}).then((resp) => {
if (resp.status === 200) {
selectedRow.remove();
log.info("User deleted successfully");
dt.draw();
}
});
},
},
{

View File

@ -2,20 +2,21 @@ package org.ros.chatto.controller;
import java.security.Principal;
import java.time.Instant;
import java.util.Date;
import java.util.List;
import javax.validation.Valid;
import com.spencerwi.either.Result;
import org.ros.chatto.dto.AdminUserDTO;
import org.ros.chatto.dto.ChatMessageDTO;
import org.ros.chatto.dto.ReencryptionDTO;
import org.ros.chatto.service.AdminService;
import org.ros.chatto.service.ChatService;
import org.ros.chatto.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
@ -70,4 +71,17 @@ public class AdminRESTController {
public List<AdminUserDTO> getAllOtherUsers(Principal principal) {
return adminService.getOtherUsers(principal.getName());
}
@DeleteMapping("/delete/users/{userName}")
public ResponseEntity<?> deleteUser(@PathVariable String userName) {
Result<Void> res = adminService.deleteUser(userName);
if (res.isOk()) {
return ResponseEntity.ok()
.body("Deleted User with name - " + userName);
} else {
return ResponseEntity.badRequest().body(
"An error occured while trying to delete user with name - "
+ userName);
}
}
}

View File

@ -1,20 +1,33 @@
package org.ros.chatto.service;
import java.security.Principal;
import java.util.List;
import java.util.Optional;
import com.spencerwi.either.Result;
import org.ros.chatto.dto.AdminUserDTO;
import org.ros.chatto.model.ChatUser;
import org.ros.chatto.repository.UserRepository;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Service
@RequiredArgsConstructor
@Slf4j
public class AdminService {
private final UserRepository userRepository;
public List<AdminUserDTO> getOtherUsers(String principal) {
return userRepository.getOtherUsers(principal);
}
public Result<Void> deleteUser(String userName) {
Optional<ChatUser> user = userRepository.findByUserName(userName);
return Result.attempt(() -> user.get()).map((u) -> {
userRepository.delete(u);
return (Void) null;
});
}
}

View File

@ -28,4 +28,5 @@ public interface UserService {
public void incrementUserSession(String userName);
public UserSession decrementUserSession(String userName);
}

View File

@ -194,4 +194,14 @@ public class UserServiceImpl implements UserService {
return user.getUserRoles().stream().map(ur -> ur.getRole())
.collect(Collectors.toSet());
}
public void deleteUser(String userName) {
//
Optional<ChatUser> user = userRepository.findByUserName(userName);
user.ifPresentOrElse(u -> {
userRepository.delete(u);
}, () -> {
log.error("User with name {} not found for deletion", userName);
});
}
}