Testing branch #48

Merged
DmitriiKaban merged 2 commits from testing_branch into master 2023-12-12 08:20:35 +00:00
3 changed files with 43 additions and 7 deletions
Showing only changes of commit 7f0a173685 - Show all commits

View File

@@ -59,8 +59,6 @@ public class UserController {
@PatchMapping("/update-password") @PatchMapping("/update-password")
public ResponseEntity<Void> updateUserPassword(@RequestBody ChangePasswordRequest password) { public ResponseEntity<Void> updateUserPassword(@RequestBody ChangePasswordRequest password) {
System.out.println("Hi");
authenticationService.updatePassword(password.getPassword()); authenticationService.updatePassword(password.getPassword());
return ResponseEntity.status(HttpStatus.OK).build(); return ResponseEntity.status(HttpStatus.OK).build();
} }
@@ -102,5 +100,22 @@ public class UserController {
return ResponseEntity.status(HttpStatus.OK).build(); 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();
}
}

View File

@@ -106,8 +106,6 @@ public class AuthenticationService {
.build(); .build();
} }
public AuthenticationResponse authenticate(AuthenticationRequest request) { public AuthenticationResponse authenticate(AuthenticationRequest request) {
authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(request.getEmail(), request.getPassword())); authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(request.getEmail(), request.getPassword()));
@@ -153,8 +151,7 @@ public class AuthenticationService {
updatedCredential.setPassword(passwordEncoder.encode(newPassword)); updatedCredential.setPassword(passwordEncoder.encode(newPassword));
credentialRepository.save(updatedCredential); credentialRepository.save(updatedCredential);
} }
} } else throw new UserNotFoundException("User not found!");
} }
} }

View File

@@ -1,6 +1,7 @@
package com.faf223.expensetrackerfaf.service; package com.faf223.expensetrackerfaf.service;
import com.faf223.expensetrackerfaf.model.Credential; import com.faf223.expensetrackerfaf.model.Credential;
import com.faf223.expensetrackerfaf.model.Role;
import com.faf223.expensetrackerfaf.model.User; import com.faf223.expensetrackerfaf.model.User;
import com.faf223.expensetrackerfaf.repository.CredentialRepository; import com.faf223.expensetrackerfaf.repository.CredentialRepository;
import com.faf223.expensetrackerfaf.repository.UserRepository; import com.faf223.expensetrackerfaf.repository.UserRepository;
@@ -29,6 +30,7 @@ public class UserService {
public User getUserById(String userUuid) { public User getUserById(String userUuid) {
return userRepository.findById(userUuid).orElse(null); return userRepository.findById(userUuid).orElse(null);
} }
public User getUserByEmail(String email) { public User getUserByEmail(String email) {
Optional<Credential> credential = credentialRepository.findByEmail(email); Optional<Credential> credential = credentialRepository.findByEmail(email);
if (credential.isPresent()) { 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);
}
}
} }