diff --git a/src/main/java/com/guams/review/controller/AuthorController.java b/src/main/java/com/guams/review/controller/AuthorController.java index 61a3419..b848e6f 100644 --- a/src/main/java/com/guams/review/controller/AuthorController.java +++ b/src/main/java/com/guams/review/controller/AuthorController.java @@ -110,6 +110,20 @@ public class AuthorController { } } + @PostMapping("/register/admin") + public ResponseEntity authorRegisterAdmin(@RequestBody Author authorToCreate, Authentication authentication) { + Author sender = authorService.findByName(authentication.getName()).orElseThrow(() -> new NotFoundException("Author not found")); + Assert.isNull(authorToCreate.getId(), "Author id must be null"); + if (authorService.findByName(authorToCreate.getName()).isPresent()) { + throw new AlreadyExistsException("Author already exists"); + } + if (!sender.getRole().equals("ADMIN")) { + throw new UnauthorizedExecption("Specified Author is not authorized to do that"); + } + authorToCreate.setPassword(passwordEncoder.encode(authorToCreate.getPassword())); + return new ResponseEntity<>(authorRepository.save(authorToCreate).setPassword(""), HttpStatus.CREATED); + } + @PostMapping("/register") public ResponseEntity authorRegister(@RequestBody Author author) { Assert.isNull(author.getId(), "Author id must be null"); @@ -118,7 +132,6 @@ public class AuthorController { } author.setPassword(passwordEncoder.encode(author.getPassword())); return new ResponseEntity<>(authorRepository.save(author.setRole("READER")).setPassword(""), HttpStatus.CREATED); - } @GetMapping(value = "/me", produces = "application/json")