|
|
@@ -11,11 +11,9 @@ import jakarta.validation.Valid;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
-import org.springframework.web.bind.annotation.PostMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestBody;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
+import java.util.List;
|
|
|
import java.util.Optional;
|
|
|
|
|
|
@RestController("webAuthenticationController")
|
|
|
@@ -27,19 +25,13 @@ public class AuthenticationController {
|
|
|
private final PasswordEncoder passwordEncoder;
|
|
|
private final TokenService tokenService;
|
|
|
|
|
|
- public AuthenticationController(AppUserRepository appUserRepository, PasswordEncoder passwordEncoder, TokenService tokenService) {
|
|
|
+ public AuthenticationController(UserService service, AppUserRepository appUserRepository, PasswordEncoder passwordEncoder, TokenService tokenService) {
|
|
|
+ this.service = service;
|
|
|
this.appUserRepository = appUserRepository;
|
|
|
this.passwordEncoder = passwordEncoder;
|
|
|
this.tokenService = tokenService;
|
|
|
}
|
|
|
|
|
|
- // GET /api/auth/{companyId}: Busca por ID da Empresa
|
|
|
- @GetMapping("/{companyId}")
|
|
|
- @ResponseStatus(HttpStatus.OK)
|
|
|
- public AppUser getByCompanyId(@PathVariable Integer companyId) {
|
|
|
- return service.buscarPorEmpresa(companyId);
|
|
|
- }
|
|
|
-
|
|
|
@PostMapping("/register")
|
|
|
public ResponseEntity<?> register(@Valid @RequestBody RegisterDTO dto) {
|
|
|
Optional<AppUser> existing = appUserRepository.findByUserEmail(dto.getEmail());
|
|
|
@@ -116,18 +108,35 @@ public class AuthenticationController {
|
|
|
return ResponseEntity.status(status).body(new TokenDTO(token));
|
|
|
}
|
|
|
|
|
|
- // PUT /api/auth/register/{id}: Atualiza um registro existente
|
|
|
- @PutMapping("/{id}")
|
|
|
+ // GET /auth/company/{companyId}: Lista todos os usuários de uma empresa
|
|
|
+ @GetMapping("/company/{companyId}")
|
|
|
+ @ResponseStatus(HttpStatus.OK)
|
|
|
+ public List<AppUser> getUsersByCompanyId(@PathVariable Integer companyId) {
|
|
|
+ return service.buscarPorEmpresa(companyId);
|
|
|
+ }
|
|
|
+
|
|
|
+ // PUT /auth/email/{id}: Atualiza apenas o email
|
|
|
+ @PutMapping("/email/{id}")
|
|
|
+ @ResponseStatus(HttpStatus.OK)
|
|
|
+ public AppUser updateEmail(@PathVariable Long id, @RequestBody AppUser userRequest) {
|
|
|
+ AppUser user = appUserRepository.findById(id).orElseThrow();
|
|
|
+ user.setUserEmail(userRequest.getUserEmail());
|
|
|
+ return appUserRepository.save(user);
|
|
|
+ }
|
|
|
+
|
|
|
+ // PUT /auth/password/{id}: Atualiza apenas a senha (hash via passwordEncoder)
|
|
|
+ @PutMapping("/password/{id}")
|
|
|
@ResponseStatus(HttpStatus.OK)
|
|
|
- public AppUser update(@PathVariable Long id, @RequestBody AppUser user) {
|
|
|
- user.setId(id);
|
|
|
- return service.salvar(user);
|
|
|
+ public AppUser updatePassword(@PathVariable Long id, @RequestBody AppUser userRequest) {
|
|
|
+ AppUser user = appUserRepository.findById(id).orElseThrow();
|
|
|
+ user.setUserPassword(passwordEncoder.encode(userRequest.getUserPassword()));
|
|
|
+ return appUserRepository.save(user);
|
|
|
}
|
|
|
|
|
|
- // DELETE /api/auth/register/{id}: Deleta um registro
|
|
|
+ // DELETE /auth/{id}: Hard delete do usuário
|
|
|
@DeleteMapping("/{id}")
|
|
|
@ResponseStatus(HttpStatus.NO_CONTENT)
|
|
|
public void delete(@PathVariable Long id) {
|
|
|
service.deletarPorId(id);
|
|
|
}
|
|
|
-}
|
|
|
+}
|