Skip to content

Commit

Permalink
fix: User Authorization 상태 값 오류 수정 (#254)
Browse files Browse the repository at this point in the history
* fix: Explation img response AwsS3Util 제거

* hotfix: Reservation 등록 API ExperienceGift 파라미터 추가

* fix: User Entity @where 어노테이션 제거

* fix: 받은 선물 조회 탈퇴한 유저 조회 안되는 현상 수정

* fix: User 회원가입, 로그인 ACTIVE 유저만 조회하게 변경

* fix: User 회원가입, 로그인 ACTIVE 유저만 조회하게 변경

* fix: User 회원가입, 로그인 ACTIVE 유저만 조회하게 변경
  • Loading branch information
sejineer authored Feb 11, 2024
1 parent 99df7d4 commit c958319
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 36 deletions.
54 changes: 29 additions & 25 deletions src/main/java/com/shallwe/domain/auth/application/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.shallwe.domain.auth.dto.*;
import com.shallwe.domain.auth.exception.AlreadyExistEmailException;
import com.shallwe.domain.auth.exception.InvalidPasswordException;
import com.shallwe.domain.common.Status;
import com.shallwe.domain.shopowner.domain.ShopOwner;
import com.shallwe.domain.shopowner.domain.repository.ShopOwnerRepository;
import com.shallwe.domain.auth.dto.ShopOwnerChangePasswordReq;
Expand All @@ -18,12 +19,14 @@
import com.shallwe.domain.user.domain.Role;
import com.shallwe.domain.auth.domain.Token;
import com.shallwe.domain.user.domain.User;
import com.shallwe.global.config.security.token.UserPrincipal;
import com.shallwe.global.error.DefaultAuthenticationException;
import com.shallwe.global.payload.ErrorCode;
import com.shallwe.global.payload.Message;
import com.shallwe.domain.auth.domain.repository.TokenRepository;
import com.shallwe.domain.user.domain.repository.UserRepository;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
Expand All @@ -38,6 +41,7 @@
@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
@Slf4j
public class AuthService {

private final CustomTokenProviderService customTokenProviderService;
Expand All @@ -50,7 +54,7 @@ public class AuthService {

@Transactional
public AuthRes signUp(final SignUpReq signUpReq) {
if (userRepository.existsByEmail(signUpReq.getEmail()))
if (userRepository.existsByEmailAndStatus(signUpReq.getEmail(), Status.ACTIVE))
throw new AlreadyExistEmailException();

User newUser = User.builder()
Expand All @@ -65,11 +69,11 @@ public AuthRes signUp(final SignUpReq signUpReq) {

userRepository.save(newUser);

Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(
signUpReq.getEmail(),
signUpReq.getProviderId()
)
UserPrincipal userPrincipal = UserPrincipal.createUser(newUser);
Authentication authentication = new UsernamePasswordAuthenticationToken(
userPrincipal,
null,
userPrincipal.getAuthorities()
);

TokenMapping tokenMapping = customTokenProviderService.createToken(authentication);
Expand All @@ -87,17 +91,17 @@ public AuthRes signUp(final SignUpReq signUpReq) {

@Transactional
public AuthRes signIn(final SignInReq signInReq) {
User user = userRepository.findByEmail(signInReq.getEmail())
User user = userRepository.findByEmailAndStatus(signInReq.getEmail(), Status.ACTIVE)
.orElseThrow(InvalidUserException::new);
if (!user.getProviderId().equals(signInReq.getProviderId())) {
throw new InvalidPasswordException();
}

Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(
signInReq.getEmail(),
signInReq.getProviderId()
)
UserPrincipal userPrincipal = UserPrincipal.createUser(user);
Authentication authentication = new UsernamePasswordAuthenticationToken(
userPrincipal,
null,
userPrincipal.getAuthorities()
);

TokenMapping tokenMapping = customTokenProviderService.createToken(authentication);
Expand Down Expand Up @@ -156,7 +160,7 @@ public Message signOut(final RefreshTokenReq tokenRefreshRequest) {

@Transactional
public AuthRes shopOwnerSignUp(final ShopOwnerSignUpReq shopOwnerSignUpReq) {
if (shopOwnerRepository.existsByPhoneNumber(shopOwnerSignUpReq.getPhoneNumber())) {
if (shopOwnerRepository.existsByPhoneNumberAndStatus(shopOwnerSignUpReq.getPhoneNumber(), Status.ACTIVE)) {
throw new AlreadyExistPhoneNumberException();
}

Expand All @@ -169,11 +173,11 @@ public AuthRes shopOwnerSignUp(final ShopOwnerSignUpReq shopOwnerSignUpReq) {

shopOwnerRepository.save(shopOwner);

Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(
shopOwnerSignUpReq.getPhoneNumber(),
shopOwnerSignUpReq.getPassword()
)
UserPrincipal userPrincipal = UserPrincipal.createShopOwner(shopOwner);
Authentication authentication = new UsernamePasswordAuthenticationToken(
userPrincipal,
null,
userPrincipal.getAuthorities()
);

TokenMapping tokenMapping = customTokenProviderService.createToken(authentication);
Expand All @@ -194,18 +198,18 @@ public AuthRes shopOwnerSignUp(final ShopOwnerSignUpReq shopOwnerSignUpReq) {

@Transactional
public AuthRes shopOwnerSignIn(final ShopOwnerSignInReq shopOwnerSignInReq) {
ShopOwner shopOwner = shopOwnerRepository.findShopOwnerByPhoneNumber(shopOwnerSignInReq.getPhoneNumber())
ShopOwner shopOwner = shopOwnerRepository.findShopOwnerByPhoneNumberAndStatus(shopOwnerSignInReq.getPhoneNumber(), Status.ACTIVE)
.orElseThrow(InvalidPhoneNumberException::new);

if (!passwordEncoder.matches(shopOwnerSignInReq.getPassword(), shopOwner.getPassword())) {
throw new InvalidPasswordException();
}

Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(
shopOwnerSignInReq.getPhoneNumber(),
shopOwnerSignInReq.getPassword()
)
UserPrincipal userPrincipal = UserPrincipal.createShopOwner(shopOwner);
Authentication authentication = new UsernamePasswordAuthenticationToken(
userPrincipal,
null,
userPrincipal.getAuthorities()
);

TokenMapping tokenMapping = customTokenProviderService.createToken(authentication);
Expand All @@ -226,7 +230,7 @@ public AuthRes shopOwnerSignIn(final ShopOwnerSignInReq shopOwnerSignInReq) {

@Transactional
public Message shopOwnerChangePassword(final ShopOwnerChangePasswordReq shopOwnerChangePasswordReq) {
ShopOwner shopOwner = shopOwnerRepository.findShopOwnerByPhoneNumber(shopOwnerChangePasswordReq.getPhoneNumber())
ShopOwner shopOwner = shopOwnerRepository.findShopOwnerByPhoneNumberAndStatus(shopOwnerChangePasswordReq.getPhoneNumber(), Status.ACTIVE)
.orElseThrow(InvalidShopOwnerException::new);

shopOwner.changePassword(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Optional;

import com.shallwe.domain.common.Status;
import com.shallwe.global.DefaultAssert;
import com.shallwe.global.config.security.auth.OAuth2UserInfo;
import com.shallwe.global.config.security.auth.OAuth2UserInfoFactory;
Expand Down Expand Up @@ -40,7 +41,7 @@ private OAuth2User processOAuth2User(OAuth2UserRequest oAuth2UserRequest, OAuth2
OAuth2UserInfo oAuth2UserInfo = OAuth2UserInfoFactory.getOAuth2UserInfo(oAuth2UserRequest.getClientRegistration().getRegistrationId(), oAuth2User.getAttributes());
DefaultAssert.isAuthentication(!oAuth2UserInfo.getEmail().isEmpty());

Optional<User> userOptional = userRepository.findByEmail(oAuth2UserInfo.getEmail());
Optional<User> userOptional = userRepository.findByEmailAndStatus(oAuth2UserInfo.getEmail(), Status.ACTIVE);
User user;
if(userOptional.isPresent()) {
user = userOptional.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Optional;

import com.shallwe.domain.common.Status;
import com.shallwe.domain.shopowner.domain.ShopOwner;
import com.shallwe.domain.shopowner.domain.repository.ShopOwnerRepository;
import com.shallwe.global.config.security.token.UserPrincipal;
Expand All @@ -28,12 +29,12 @@ public class CustomUserDetailsService implements UserDetailsService {
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {

Optional<User> user = userRepository.findByEmail(email);
Optional<User> user = userRepository.findByEmailAndStatus(email, Status.ACTIVE);
if (user.isPresent()) {
return UserPrincipal.createUser(user.get());
}

Optional<ShopOwner> shopOwner = shopOwnerRepository.findShopOwnerByPhoneNumber(email);
Optional<ShopOwner> shopOwner = shopOwnerRepository.findShopOwnerByPhoneNumberAndStatus(email, Status.ACTIVE);
if (shopOwner.isPresent()) {
return UserPrincipal.createShopOwner(shopOwner.get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public ReservationResponse addUserReservation(UserReservationCreate reservationR
User sender = userRepository.findById(userPrincipal.getId())
.orElseThrow(InvalidUserException::new);

User receiver = userRepository.findByPhoneNumber(reservationRequest.getPhoneNumber())
User receiver = userRepository.findByPhoneNumberAndStatus(reservationRequest.getPhoneNumber(), Status.ACTIVE)
.orElseThrow(InvalidUserException::new);

ExperienceGift experienceGift = experienceGiftRepository.findById(reservationRequest.getExperienceGiftId())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.shallwe.domain.shopowner.domain.repository;

import com.shallwe.domain.common.Status;
import com.shallwe.domain.shopowner.domain.ShopOwner;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.Optional;

public interface ShopOwnerRepository extends JpaRepository<ShopOwner, Long> {

boolean existsByPhoneNumber(String phoneNumber);
boolean existsByPhoneNumberAndStatus(String phoneNumber, Status status);

Optional<ShopOwner> findShopOwnerByPhoneNumber(String phoneNumber);
Optional<ShopOwner> findShopOwnerByPhoneNumberAndStatus(String phoneNumber, Status status);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

import java.util.Optional;

import com.shallwe.domain.common.Status;
import com.shallwe.domain.user.domain.User;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {

Optional<User> findByEmail(String email);
Boolean existsByEmailAndStatus(String email, Status status);

Boolean existsByEmail(String email);
Optional<User> findByPhoneNumberAndStatus(String phoneNumber, Status status);

Optional<User> findByPhoneNumber(String phoneNumber);
Optional<User> findByEmailAndStatus(String email, Status status);

}
2 changes: 0 additions & 2 deletions src/main/java/com/shallwe/domain/user/dto/UserDetailRes.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import com.shallwe.domain.common.Status;
import com.shallwe.domain.user.domain.Gender;
import com.shallwe.domain.user.domain.User;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.RequiredArgsConstructor;

@Data
public class UserDetailRes {
Expand Down

0 comments on commit c958319

Please sign in to comment.