Merge pull request #48 from lumijiez/testing_branch

Testing branch
This commit was merged in pull request #48.
This commit is contained in:
Dmitrii Cravcenco
2023-12-12 10:20:35 +02:00
committed by GitHub
3 changed files with 43 additions and 7 deletions

View File

@@ -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();
}
}

View File

@@ -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!");
}
}

View File

@@ -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);
}
}
}