Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 회원탈퇴한 유저는 사용할 수 없도록 코드 수정 #141

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Optional;

@Component
@RequiredArgsConstructor
Expand All @@ -27,15 +26,14 @@ public class OAuth2LoginSuccessHandler implements AuthenticationSuccessHandler {
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
OAuth2User oAuth2User = (OAuth2User)authentication.getPrincipal();
// OAuth2Attributes oAuth2Attributes = OAuth2Attributes.of(provider, userNameAttributeName, oAuth2User.getAttributes());
// Optional<User> findUser = userRepository.findByAuthId()
// Optional<User> findUser = userRepository.findByAuthIdAndAccountExpired()

//jwt 생성
String accessToken = jwtTokenProvider.createAccessToken(oAuth2User);
String refreshToken = jwtTokenProvider.createRefreshToken(oAuth2User);

//refreshtoken을 DB에 저장해야함
Optional<User> userOptional = userRepository.findByAuthId(oAuth2User.getName());
User user = userOptional.get(); //TODO: null값 체크하기
User user = userRepository.findByAuthIdAndAccountExpired(authentication.getName(), false).orElseThrow(()-> new RuntimeException("올바른 유저 ID가 아닙니다."));
user.setRefreshToken(refreshToken);

// tokenResponse(response, accessToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private OAuth2User process(OAuth2UserRequest userRequest, OAuth2User oAuth2User)

OAuth2Attributes oAuth2Attributes = OAuth2Attributes.of(provider, userNameAttributeName, oAuth2User.getAttributes());

Optional<User> userOptional = userRepository.findByAuthId(oAuth2Attributes.getAuthId());
Optional<User> userOptional = userRepository.findByAuthIdAndAccountExpired(oAuth2Attributes.getAuthId(), false);

User user;
boolean newUser=false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ public List<Calendar> readCalendarsByDate(List<Calendar> calendarList, LocalDate

private User getUser(Authentication authentication) throws NoSuchElementException {
try{
Optional<User> userOptional = userRepository.findByAuthId(authentication.getName());
return userOptional.get();
return userRepository.findByAuthIdAndAccountExpired(authentication.getName(), false).orElseThrow(()-> new RuntimeException("올바른 유저 ID가 아닙니다."));
}catch (RuntimeException e){
throw new NoSuchElementException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public CategoryController(CategoryService categoryService, UserRepository userRe
@PostMapping
public ResponseEntity<Map<String, Object>> addCategory(Authentication authentication, @Valid @RequestBody CategoryRequestDto categoryRequestDto) {
// 카테고리 생성 API
Optional<User> userOptional = userRepository.findByAuthId(authentication.getName());
User user = userOptional.get();
User user = userRepository.findByAuthIdAndAccountExpired(authentication.getName(), false).orElseThrow(()-> new RuntimeException("올바른 유저 ID가 아닙니다."));
CategoryResponseDto newCategory = categoryService.createCategory(user, categoryRequestDto);
Map<String, Object> data = new LinkedHashMap<>();
data.put("Category", newCategory);
Expand All @@ -49,8 +48,7 @@ public ResponseEntity<Map<String, Object>> addCategory(Authentication authentica
@PatchMapping("/{categoryId}")
public ResponseEntity<Map<String, Object>> updateCategory(Authentication authentication,@PathVariable int categoryId, @Valid @RequestBody CategoryRequestDto categoryRequestDto) {
// 카테고리 수정 API
Optional<User> userOptional = userRepository.findByAuthId(authentication.getName());
User user = userOptional.get();
User user = userRepository.findByAuthIdAndAccountExpired(authentication.getName(), false).orElseThrow(()-> new RuntimeException("올바른 유저 ID가 아닙니다."));
CategoryResponseDto updatedCategory = categoryService.updateCategory(user, categoryId, categoryRequestDto);
Map<String, Object> data = new LinkedHashMap<>();
data.put("Category", updatedCategory);
Expand All @@ -70,8 +68,7 @@ public ResponseEntity<Map<String, Object>> updateCategory(Authentication authent
public ResponseEntity<Map<String, Object>> deleteCategory(Authentication authentication, @PathVariable int categoryId) {
// 카테고리 삭제 API
try{
Optional<User> userOptional = userRepository.findByAuthId(authentication.getName());
User user = userOptional.get();
User user = userRepository.findByAuthIdAndAccountExpired(authentication.getName(), false).orElseThrow(()-> new RuntimeException("올바른 유저 ID가 아닙니다."));
categoryService.deleteCategory(user,categoryId);
Map<String, Object> result = new LinkedHashMap<>();
//result.put("status", 200);
Expand All @@ -87,8 +84,7 @@ public ResponseEntity<Map<String, Object>> deleteCategory(Authentication authent
public ResponseEntity<Map<String, Object>> inactiveCategory(Authentication authentication, @PathVariable int categoryId) {
//카테고리 종료 API
try{
Optional<User> userOptional = userRepository.findByAuthId(authentication.getName());
User user = userOptional.get();
User user = userRepository.findByAuthIdAndAccountExpired(authentication.getName(), false).orElseThrow(()-> new RuntimeException("올바른 유저 ID가 아닙니다."));
categoryService.inactiveCategory(user, categoryId);
Map<String, Object> result = new LinkedHashMap<>();
return ResponseEntity.ok().body(result);
Expand All @@ -101,8 +97,7 @@ public ResponseEntity<Map<String, Object>> inactiveCategory(Authentication authe
public ResponseEntity<Map<String, Object>> activeCategory(Authentication authentication, @PathVariable int categoryId) {
//종료된 카테고리 복원 API
try{
Optional<User> userOptional = userRepository.findByAuthId(authentication.getName());
User user = userOptional.get();
User user = userRepository.findByAuthIdAndAccountExpired(authentication.getName(), false).orElseThrow(()-> new RuntimeException("올바른 유저 ID가 아닙니다."));
categoryService.activeCategory(user, categoryId);
Map<String, Object> result = new LinkedHashMap<>();
return ResponseEntity.ok().body(result);
Expand All @@ -115,8 +110,7 @@ public ResponseEntity<Map<String, Object>> activeCategory(Authentication authent
public ResponseEntity<Map<String, Object>> getAllCategories(Authentication authentication) {
// 특정 유저 카테고리 목록 조회 API (카테고리 목록)
try {
Optional<User> userOptional = userRepository.findByAuthId(authentication.getName());
User user = userOptional.get();
User user = userRepository.findByAuthIdAndAccountExpired(authentication.getName(), false).orElseThrow(()-> new RuntimeException("올바른 유저 ID가 아닙니다."));
List<CategoryResponseDto> allCategories = categoryService.getAllCategories(user);
Map<String, Object> data = new LinkedHashMap<>();
data.put("CategoryList", allCategories);
Expand All @@ -135,8 +129,7 @@ public ResponseEntity<Map<String, Object>> getAllCategories(Authentication authe
public ResponseEntity<Map<String, Object>> getHomeCategories(Authentication authentication, @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) {
// 특정 유저 카테고리 목록 조회 API (home)
try {
Optional<User> userOptional = userRepository.findByAuthId(authentication.getName());
User user = userOptional.get();
User user = userRepository.findByAuthIdAndAccountExpired(authentication.getName(), false).orElseThrow(()-> new RuntimeException("올바른 유저 ID가 아닙니다."));
List<CategoryResponseDto> homeCategories = categoryService.getHomeCategories(user, date);
Map<String, Object> data = new LinkedHashMap<>();
data.put("CategoryList", homeCategories);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public ResponseEntity<Void> buyItem(Authentication authentication, @PathVariable


private User findUser(Authentication authentication){
Optional<User> userOptional = userRepository.findByAuthId(authentication.getName());
User user = userRepository.findByAuthIdAndAccountExpired(authentication.getName(), false).orElseThrow(()-> new RuntimeException("올바른 유저 ID가 아닙니다."));
// User user = userOptional.get(); //TODO: get값이 NULL인 경우를 체크해줘야함
return userOptional.get();
return user;
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/umc/mada/exception/ControllerAdvice.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public ResponseEntity<ErrorResponse> buyDuplicateItemHandler(final BuyOwnedItemE
public ResponseEntity<ErrorResponse> notAllowToWearingHandler(final NotAllowToWearingException e){
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ErrorResponse(e.getMessage()));
}

@ExceptionHandler(NotFoundUserException.class)
public ResponseEntity<ErrorResponse> notFoundUserHandler(final NotFoundUserException e){
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ErrorResponse(e.getMessage()));
}

@ExceptionHandler(ServerInternalException.class)
public ResponseEntity<ErrorResponse> testErrorHandler(final ServerInternalException e, HttpServletRequest httpServletRequest){
ErrorResponse errorResponse = ErrorResponse.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.umc.mada.exception;

public class NotFoundUserException extends RuntimeException{
public NotFoundUserException(final String message){
super(message);
}
}
5 changes: 2 additions & 3 deletions src/main/java/com/umc/mada/my/service/MyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.umc.mada.my.dto.MyResponseDto;
import com.umc.mada.my.repository.MyRepository;
import com.umc.mada.user.domain.User;
import com.umc.mada.user.service.UserService;
import com.umc.mada.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
Expand Down Expand Up @@ -36,8 +35,8 @@ public List<MyResponseDto> findRandomSaying() {
}

private User getUser(Authentication authentication){
Optional<User> optionalUser = userRepository.findByAuthId(authentication.getName());
return optionalUser.get();
User user= userRepository.findByAuthIdAndAccountExpired(authentication.getName(), false).orElseThrow(()-> new RuntimeException("올바른 유저 ID가 아닙니다."));
return user;
}

public Map<String, Object> findMyProfileList(Authentication authentication) {
Expand Down
Loading
Loading