Testing branch #47
@@ -1,15 +1,20 @@
|
|||||||
package com.faf223.expensetrackerfaf.controller;
|
package com.faf223.expensetrackerfaf.controller;
|
||||||
|
|
||||||
|
import com.faf223.expensetrackerfaf.controller.auth.ChangePasswordRequest;
|
||||||
import com.faf223.expensetrackerfaf.dto.UserCreationDTO;
|
import com.faf223.expensetrackerfaf.dto.UserCreationDTO;
|
||||||
import com.faf223.expensetrackerfaf.dto.UserDTO;
|
import com.faf223.expensetrackerfaf.dto.UserDTO;
|
||||||
import com.faf223.expensetrackerfaf.dto.mappers.UserMapper;
|
import com.faf223.expensetrackerfaf.dto.mappers.UserMapper;
|
||||||
|
import com.faf223.expensetrackerfaf.model.Credential;
|
||||||
import com.faf223.expensetrackerfaf.model.User;
|
import com.faf223.expensetrackerfaf.model.User;
|
||||||
|
import com.faf223.expensetrackerfaf.repository.CredentialRepository;
|
||||||
|
import com.faf223.expensetrackerfaf.service.AuthenticationService;
|
||||||
import com.faf223.expensetrackerfaf.service.UserService;
|
import com.faf223.expensetrackerfaf.service.UserService;
|
||||||
import com.faf223.expensetrackerfaf.util.errors.ErrorResponse;
|
import com.faf223.expensetrackerfaf.util.errors.ErrorResponse;
|
||||||
import com.faf223.expensetrackerfaf.util.exceptions.UserNotCreatedException;
|
import com.faf223.expensetrackerfaf.util.exceptions.UserNotCreatedException;
|
||||||
import com.faf223.expensetrackerfaf.util.exceptions.UserNotFoundException;
|
import com.faf223.expensetrackerfaf.util.exceptions.UserNotFoundException;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
@@ -19,6 +24,9 @@ import org.springframework.validation.BindingResult;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/users")
|
@RequestMapping("/users")
|
||||||
@@ -27,29 +35,53 @@ public class UserController {
|
|||||||
|
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
private final UserMapper userMapper;
|
private final UserMapper userMapper;
|
||||||
|
private final CredentialRepository credentialRepository;
|
||||||
|
private final AuthenticationService authenticationService;
|
||||||
|
|
||||||
@PatchMapping()
|
@PatchMapping()
|
||||||
public ResponseEntity<UserDTO> updateUser(@RequestBody @Valid UserCreationDTO userDTO,
|
public ResponseEntity<UserDTO> updateUser(@RequestBody @Valid UserCreationDTO userDTO,
|
||||||
BindingResult bindingResult) {
|
BindingResult bindingResult) {
|
||||||
if(bindingResult.hasErrors())
|
if (bindingResult.hasErrors())
|
||||||
throw new UserNotCreatedException(ErrorResponse.from(bindingResult).getMessage());
|
throw new UserNotCreatedException(ErrorResponse.from(bindingResult).getMessage());
|
||||||
|
|
||||||
User user = userMapper.toUser(userDTO);
|
User user = userMapper.toUser(userDTO);
|
||||||
|
|
||||||
if (!bindingResult.hasErrors()) {
|
if (!bindingResult.hasErrors()) {
|
||||||
|
|
||||||
userService.updateUser(user);
|
userService.updateUser(user);
|
||||||
return ResponseEntity.ok(userMapper.toDto(user));
|
return ResponseEntity.ok(userMapper.toDto(user));
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw new UserNotFoundException("The user has not been found");
|
throw new UserNotFoundException("The user has not been found");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PatchMapping("/update-password")
|
||||||
|
public ResponseEntity<Void> updateUserPassword(@RequestBody ChangePasswordRequest password) {
|
||||||
|
|
||||||
|
System.out.println("Hi");
|
||||||
|
|
||||||
|
authenticationService.updatePassword(password.getPassword());
|
||||||
|
return ResponseEntity.status(HttpStatus.OK).build();
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping("/get-user-data")
|
@GetMapping("/get-user-data")
|
||||||
public ResponseEntity<UserDTO> getUser() {
|
public ResponseEntity<Map<String, String>> getUser() {
|
||||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
|
||||||
if (authentication != null && authentication.getPrincipal() instanceof UserDetails userDetails) {
|
if (authentication != null && authentication.getPrincipal() instanceof UserDetails userDetails) {
|
||||||
User user = userService.getUserByEmail(userDetails.getUsername());
|
User user = userService.getUserByEmail(userDetails.getUsername());
|
||||||
if (user != null) return ResponseEntity.ok(userMapper.toDto(user));
|
Optional<Credential> credential = credentialRepository.findByUser(user);
|
||||||
|
|
||||||
|
if (credential.isPresent()) {
|
||||||
|
Map<String, String> userData = new HashMap<>();
|
||||||
|
userData.put("firstname", user.getFirstName());
|
||||||
|
userData.put("lastname", user.getLastName());
|
||||||
|
userData.put("username", user.getUsername());
|
||||||
|
userData.put("userrole", credential.get().getRole().toString()); // Assuming UserRole is an enum
|
||||||
|
|
||||||
|
return ResponseEntity.ok(userData);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
throw new UserNotFoundException("The user has not been found");
|
throw new UserNotFoundException("The user has not been found");
|
||||||
}
|
}
|
||||||
@@ -61,5 +93,14 @@ public class UserController {
|
|||||||
|
|
||||||
return ResponseEntity.ok(userMapper.toDto(users));
|
return ResponseEntity.ok(userMapper.toDto(users));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/delete/{username}")
|
||||||
|
@PreAuthorize("hasRole('ADMIN')")
|
||||||
|
public ResponseEntity<Void> deleteUserByUsername(@PathVariable String username) {
|
||||||
|
|
||||||
|
userService.deleteByUsername(username);
|
||||||
|
|
||||||
|
return ResponseEntity.status(HttpStatus.OK).build();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.controller.auth;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class ChangePasswordRequest {
|
||||||
|
private String password;
|
||||||
|
}
|
||||||
@@ -17,5 +17,4 @@ public class UserDTO {
|
|||||||
@NotNull(message = "Username must not be null")
|
@NotNull(message = "Username must not be null")
|
||||||
@NotEmpty(message = "Username must not be empty")
|
@NotEmpty(message = "Username must not be empty")
|
||||||
private String username;
|
private String username;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,48 +1,48 @@
|
|||||||
package com.faf223.expensetrackerfaf.model;
|
package com.faf223.expensetrackerfaf.model;
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import jakarta.validation.constraints.NotEmpty;
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Entity(name = "users")
|
@Entity(name = "users")
|
||||||
@Data
|
@Data
|
||||||
@Builder
|
@Builder
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class User {
|
public class User {
|
||||||
@Id
|
@Id
|
||||||
@Column(name = "user_uuid")
|
@Column(name = "user_uuid")
|
||||||
@GeneratedValue(strategy = GenerationType.UUID)
|
@GeneratedValue(strategy = GenerationType.UUID)
|
||||||
private String userUuid;
|
private String userUuid;
|
||||||
|
|
||||||
@Column(name = "name")
|
@Column(name = "name")
|
||||||
@NotNull(message = "First name must not be null")
|
@NotNull(message = "First name must not be null")
|
||||||
@NotEmpty(message = "First name must not be empty")
|
@NotEmpty(message = "First name must not be empty")
|
||||||
private String firstName;
|
private String firstName;
|
||||||
|
|
||||||
@Column(name = "surname")
|
@Column(name = "surname")
|
||||||
@NotNull(message = "Last name must not be null")
|
@NotNull(message = "Last name must not be null")
|
||||||
@NotEmpty(message = "Last name must not be empty")
|
@NotEmpty(message = "Last name must not be empty")
|
||||||
private String lastName;
|
private String lastName;
|
||||||
|
|
||||||
@Column(name = "username")
|
@Column(name = "username")
|
||||||
@NotNull(message = "Username must not be null")
|
@NotNull(message = "Username must not be null")
|
||||||
@NotEmpty(message = "Username must not be empty")
|
@NotEmpty(message = "Username must not be empty")
|
||||||
private String username;
|
private String username;
|
||||||
|
|
||||||
@Transient
|
@Transient
|
||||||
// @NotNull(message = "Password must not be null")
|
// @NotNull(message = "Password must not be null")
|
||||||
// @NotEmpty(message = "Password must not be empty")
|
// @NotEmpty(message = "Password must not be empty")
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
|
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
||||||
@ToString.Exclude
|
@ToString.Exclude
|
||||||
private List<Expense> expenses;
|
private List<Expense> expenses;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
|
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
||||||
@ToString.Exclude
|
@ToString.Exclude
|
||||||
private List<Income> incomes;
|
private List<Income> incomes;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.faf223.expensetrackerfaf.repository;
|
package com.faf223.expensetrackerfaf.repository;
|
||||||
|
|
||||||
import com.faf223.expensetrackerfaf.model.Credential;
|
import com.faf223.expensetrackerfaf.model.Credential;
|
||||||
|
import com.faf223.expensetrackerfaf.model.User;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
@@ -9,4 +10,8 @@ import java.util.Optional;
|
|||||||
@Repository
|
@Repository
|
||||||
public interface CredentialRepository extends JpaRepository<Credential, Long> {
|
public interface CredentialRepository extends JpaRepository<Credential, Long> {
|
||||||
Optional<Credential> findByEmail(String email);
|
Optional<Credential> findByEmail(String email);
|
||||||
|
|
||||||
|
Optional<Credential> findByUser(User user);
|
||||||
|
|
||||||
|
void deleteByEmail(String email);
|
||||||
}
|
}
|
||||||
@@ -9,4 +9,6 @@ public interface UserRepository extends JpaRepository<User, String> {
|
|||||||
Optional<User> getUserByUserUuid(String userUuid);
|
Optional<User> getUserByUserUuid(String userUuid);
|
||||||
|
|
||||||
Optional<User> findByUsername(String username);
|
Optional<User> findByUsername(String username);
|
||||||
|
|
||||||
|
void deleteByUsername(String username);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ import com.faf223.expensetrackerfaf.util.exceptions.UserNotFoundException;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.security.oauth2.core.user.OAuth2User;
|
import org.springframework.security.oauth2.core.user.OAuth2User;
|
||||||
@@ -27,6 +29,7 @@ import java.util.Optional;
|
|||||||
public class AuthenticationService {
|
public class AuthenticationService {
|
||||||
|
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
|
private final UserService userService;
|
||||||
private final CredentialRepository credentialRepository;
|
private final CredentialRepository credentialRepository;
|
||||||
private final PasswordEncoder passwordEncoder;
|
private final PasswordEncoder passwordEncoder;
|
||||||
private final JwtService jwtService;
|
private final JwtService jwtService;
|
||||||
@@ -136,4 +139,22 @@ public class AuthenticationService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void updatePassword(String newPassword) {
|
||||||
|
|
||||||
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
|
||||||
|
if (authentication != null && authentication.getPrincipal() instanceof UserDetails userDetails) {
|
||||||
|
User user = userService.getUserByEmail(userDetails.getUsername());
|
||||||
|
Optional<Credential> credential = credentialRepository.findByUser(user);
|
||||||
|
|
||||||
|
if (credential.isPresent()) {
|
||||||
|
|
||||||
|
Credential updatedCredential = credential.get();
|
||||||
|
updatedCredential.setPassword(passwordEncoder.encode(newPassword));
|
||||||
|
credentialRepository.save(updatedCredential);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import com.faf223.expensetrackerfaf.model.Credential;
|
|||||||
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;
|
||||||
|
import jakarta.transaction.Transactional;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@@ -36,4 +37,20 @@ public class UserService {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteByUsername(String username) {
|
||||||
|
|
||||||
|
Optional<User> user = userRepository.findByUsername(username);
|
||||||
|
if (user.isPresent()) {
|
||||||
|
|
||||||
|
Optional<Credential> credential = credentialRepository.findByUser(user.get());
|
||||||
|
|
||||||
|
if (credential.isPresent()) {
|
||||||
|
|
||||||
|
credentialRepository.deleteByEmail(credential.get().getEmail());
|
||||||
|
userRepository.deleteByUsername(username);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user