Add promoting/demoting user (change user role)
This commit is contained in:
@@ -59,8 +59,6 @@ public class UserController {
|
||||
@PatchMapping("/update-password")
|
||||
public ResponseEntity<Void> updateUserPassword(@RequestBody ChangePasswordRequest password) {
|
||||
|
||||
System.out.println("Hi");
|
||||
|
||||
authenticationService.updatePassword(password.getPassword());
|
||||
return ResponseEntity.status(HttpStatus.OK).build();
|
||||
}
|
||||
@@ -102,5 +100,22 @@ public class UserController {
|
||||
|
||||
return ResponseEntity.status(HttpStatus.OK).build();
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/promote/{email}")
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
public ResponseEntity<Void> promoteUser(@PathVariable String email) {
|
||||
|
||||
userService.promoteUser(email);
|
||||
|
||||
return ResponseEntity.status(HttpStatus.OK).build();
|
||||
}
|
||||
|
||||
@GetMapping("/demote/{email}")
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
public ResponseEntity<Void> demoteUser(@PathVariable String email) {
|
||||
|
||||
userService.demoteUser(email);
|
||||
|
||||
return ResponseEntity.status(HttpStatus.OK).build();
|
||||
}
|
||||
}
|
||||
@@ -106,8 +106,6 @@ public class AuthenticationService {
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public AuthenticationResponse authenticate(AuthenticationRequest request) {
|
||||
authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(request.getEmail(), request.getPassword()));
|
||||
|
||||
@@ -153,8 +151,7 @@ public class AuthenticationService {
|
||||
updatedCredential.setPassword(passwordEncoder.encode(newPassword));
|
||||
credentialRepository.save(updatedCredential);
|
||||
}
|
||||
}
|
||||
|
||||
} else throw new UserNotFoundException("User not found!");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.faf223.expensetrackerfaf.service;
|
||||
|
||||
import com.faf223.expensetrackerfaf.model.Credential;
|
||||
import com.faf223.expensetrackerfaf.model.Role;
|
||||
import com.faf223.expensetrackerfaf.model.User;
|
||||
import com.faf223.expensetrackerfaf.repository.CredentialRepository;
|
||||
import com.faf223.expensetrackerfaf.repository.UserRepository;
|
||||
@@ -29,6 +30,7 @@ public class UserService {
|
||||
public User getUserById(String userUuid) {
|
||||
return userRepository.findById(userUuid).orElse(null);
|
||||
}
|
||||
|
||||
public User getUserByEmail(String email) {
|
||||
Optional<Credential> credential = credentialRepository.findByEmail(email);
|
||||
if (credential.isPresent()) {
|
||||
@@ -53,4 +55,26 @@ public class UserService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void promoteUser(String email) {
|
||||
Optional<Credential> credential = credentialRepository.findByEmail(email);
|
||||
if (credential.isPresent()) {
|
||||
|
||||
System.out.println(email);
|
||||
Credential updatedCredential = credential.get();
|
||||
updatedCredential.setRole(Role.ROLE_ADMIN);
|
||||
credentialRepository.save(updatedCredential);
|
||||
}
|
||||
}
|
||||
|
||||
public void demoteUser(String email) {
|
||||
Optional<Credential> credential = credentialRepository.findByEmail(email);
|
||||
if (credential.isPresent()) {
|
||||
|
||||
System.out.println(email);
|
||||
Credential updatedCredential = credential.get();
|
||||
updatedCredential.setRole(Role.ROLE_USER);
|
||||
credentialRepository.save(updatedCredential);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user