Added delete user handler in admin #13
This commit is contained in:
parent
dce2e3ece3
commit
40cc234b83
5
pom.xml
5
pom.xml
@ -103,6 +103,11 @@
|
|||||||
<artifactId>flyway-core</artifactId>
|
<artifactId>flyway-core</artifactId>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.spencerwi</groupId>
|
||||||
|
<artifactId>Either.java</artifactId>
|
||||||
|
<version>2.2.0</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
@ -2,6 +2,7 @@ import moment from "moment";
|
|||||||
import { capitalize } from "../../../common/util/Util";
|
import { capitalize } from "../../../common/util/Util";
|
||||||
import { AdminUserDTO } from "../../../common/dto/AdminUserDTO";
|
import { AdminUserDTO } from "../../../common/dto/AdminUserDTO";
|
||||||
import log from "loglevel";
|
import log from "loglevel";
|
||||||
|
import { createApiHeaders } from "../../../common/ajax/util";
|
||||||
|
|
||||||
export async function viewUsers(authToken: string) {
|
export async function viewUsers(authToken: string) {
|
||||||
// const users = await getOtherUsers(authToken);
|
// const users = await getOtherUsers(authToken);
|
||||||
@ -46,7 +47,18 @@ export async function viewUsers(authToken: string) {
|
|||||||
extend: "selectedSingle",
|
extend: "selectedSingle",
|
||||||
text: "Delete User",
|
text: "Delete User",
|
||||||
action: (e, dt, button, config) => {
|
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();
|
||||||
|
}
|
||||||
|
});
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -2,20 +2,21 @@ package org.ros.chatto.controller;
|
|||||||
|
|
||||||
import java.security.Principal;
|
import java.security.Principal;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.Date;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
|
||||||
|
import com.spencerwi.either.Result;
|
||||||
|
|
||||||
import org.ros.chatto.dto.AdminUserDTO;
|
import org.ros.chatto.dto.AdminUserDTO;
|
||||||
import org.ros.chatto.dto.ChatMessageDTO;
|
import org.ros.chatto.dto.ChatMessageDTO;
|
||||||
import org.ros.chatto.dto.ReencryptionDTO;
|
import org.ros.chatto.dto.ReencryptionDTO;
|
||||||
import org.ros.chatto.service.AdminService;
|
import org.ros.chatto.service.AdminService;
|
||||||
import org.ros.chatto.service.ChatService;
|
import org.ros.chatto.service.ChatService;
|
||||||
import org.ros.chatto.service.UserService;
|
import org.ros.chatto.service.UserService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
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.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
@ -70,4 +71,17 @@ public class AdminRESTController {
|
|||||||
public List<AdminUserDTO> getAllOtherUsers(Principal principal) {
|
public List<AdminUserDTO> getAllOtherUsers(Principal principal) {
|
||||||
return adminService.getOtherUsers(principal.getName());
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,33 @@
|
|||||||
package org.ros.chatto.service;
|
package org.ros.chatto.service;
|
||||||
|
|
||||||
import java.security.Principal;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import com.spencerwi.either.Result;
|
||||||
|
|
||||||
import org.ros.chatto.dto.AdminUserDTO;
|
import org.ros.chatto.dto.AdminUserDTO;
|
||||||
|
import org.ros.chatto.model.ChatUser;
|
||||||
import org.ros.chatto.repository.UserRepository;
|
import org.ros.chatto.repository.UserRepository;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
|
@Slf4j
|
||||||
public class AdminService {
|
public class AdminService {
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
|
|
||||||
public List<AdminUserDTO> getOtherUsers(String principal) {
|
public List<AdminUserDTO> getOtherUsers(String principal) {
|
||||||
return userRepository.getOtherUsers(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;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
@ -28,4 +28,5 @@ public interface UserService {
|
|||||||
public void incrementUserSession(String userName);
|
public void incrementUserSession(String userName);
|
||||||
|
|
||||||
public UserSession decrementUserSession(String userName);
|
public UserSession decrementUserSession(String userName);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -194,4 +194,14 @@ public class UserServiceImpl implements UserService {
|
|||||||
return user.getUserRoles().stream().map(ur -> ur.getRole())
|
return user.getUserRoles().stream().map(ur -> ur.getRole())
|
||||||
.collect(Collectors.toSet());
|
.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);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user