Route pour créer des comptes en tant qu'admin

This commit is contained in:
Guams 2025-02-04 20:23:27 +01:00
parent 2259ce7e00
commit 916a8ba2e4

View File

@ -110,6 +110,20 @@ public class AuthorController {
}
}
@PostMapping("/register/admin")
public ResponseEntity<Author> 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<Author> 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")