Skip to content

Commit

Permalink
Feat : 유저 블락 기능 (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
Astin01 authored Sep 21, 2024
1 parent 1d6df56 commit 8f3234c
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import solitour_backend.solitour.auth.service.dto.response.OauthLinkResponse;
import solitour_backend.solitour.auth.support.google.GoogleConnector;
import solitour_backend.solitour.auth.support.kakao.KakaoConnector;
import solitour_backend.solitour.user.user_status.UserStatus;


@RequiredArgsConstructor
Expand All @@ -43,8 +44,8 @@ public ResponseEntity<OauthLinkResponse> access(@RequestParam String type, @Requ
}

@GetMapping(value = "/login", params = {"type", "code", "redirectUrl"})
public ResponseEntity<LoginResponse> login(HttpServletResponse response, @RequestParam String type,
@RequestParam String code, @RequestParam String redirectUrl) {
public ResponseEntity<UserStatus> login(HttpServletResponse response, @RequestParam String type,
@RequestParam String code, @RequestParam String redirectUrl) {
LoginResponse loginResponse = oauthService.requestAccessToken(type, code, redirectUrl);

String accessCookieHeader = setCookieHeader(loginResponse.getAccessToken());
Expand All @@ -53,7 +54,7 @@ public ResponseEntity<LoginResponse> login(HttpServletResponse response, @Reques
response.addHeader("Set-Cookie", accessCookieHeader);
response.addHeader("Set-Cookie", refreshCookieHeader);

return ResponseEntity.ok().build();
return ResponseEntity.ok(loginResponse.getLoginStatus());
}

@PostMapping("/logout")
Expand All @@ -77,7 +78,8 @@ public ResponseEntity<Void> reissueAccessToken(HttpServletResponse response,

@Authenticated
@DeleteMapping()
public ResponseEntity<String> deleteUser(HttpServletResponse response, @AuthenticationPrincipal Long id, @RequestParam String type) {
public ResponseEntity<String> deleteUser(HttpServletResponse response, @AuthenticationPrincipal Long id,
@RequestParam String type) {
Token token = tokenRepository.findByUserId(id)
.orElseThrow(() -> new TokenNotExistsException("토큰이 존재하지 않습니다"));
String oauthRefreshToken = getOauthAccessToken(type, token.getOauthToken());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.time.LocalDateTime;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import jdk.jshell.spi.ExecutionControl.UserException;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatusCode;
Expand All @@ -29,6 +30,9 @@
import solitour_backend.solitour.auth.support.kakao.dto.KakaoUserResponse;
import solitour_backend.solitour.image.s3.S3Uploader;
import solitour_backend.solitour.user.entity.User;
import solitour_backend.solitour.user.exception.BlockedUserException;
import solitour_backend.solitour.user.exception.DeletedUserException;
import solitour_backend.solitour.user.exception.DormantUserException;
import solitour_backend.solitour.user.repository.UserRepository;
import solitour_backend.solitour.user.user_status.UserStatus;
import solitour_backend.solitour.user_image.entity.UserImage;
Expand Down Expand Up @@ -77,7 +81,7 @@ public LoginResponse requestAccessToken(String type, String code, String redirec
Cookie accessCookie = createCookie("access_token", token, ACCESS_COOKIE_AGE);
Cookie refreshCookie = createCookie("refresh_token", refreshToken, REFRESH_COOKIE_AGE);

return new LoginResponse(accessCookie, refreshCookie);
return new LoginResponse(accessCookie, refreshCookie,user.getUserStatus());
}

private Cookie createCookie(String name, String value, int maxAge) {
Expand All @@ -99,6 +103,8 @@ private User checkAndSaveUser(String type, String code, String redirectUrl) {
User user = userRepository.findByOauthId(id)
.orElseGet(() -> saveKakaoUser(kakaoUserResponse));

checkUserStatus(user);

Token token = tokenRepository.findByUserId(user.getId())
.orElseGet(() -> tokenService.saveToken(tokenResponse, user));

Expand All @@ -115,6 +121,15 @@ private User checkAndSaveUser(String type, String code, String redirectUrl) {
}
}

private void checkUserStatus(User user) {
UserStatus userStatus = user.getUserStatus();
switch (userStatus){
case BLOCK -> throw new BlockedUserException("차단된 계정입니다.");
case DELETE -> throw new DeletedUserException("탈퇴한 계정입니다.");
case DORMANT -> throw new DormantUserException("휴면 계정입니다.");
}
}

private void saveToken(KakaoTokenResponse tokenResponse, User user) {
Token token = Token.builder()
.user(user)
Expand Down Expand Up @@ -160,7 +175,7 @@ private User saveKakaoUser(KakaoUserResponse response) {
UserImage savedUserImage = userImageService.saveUserImage(imageUrl);

User user = User.builder()
.userStatus(UserStatus.ACTIVATE)
.userStatus(UserStatus.INACTIVATE)
.oauthId(String.valueOf(response.getId()))
.provider("kakao")
.isAdmin(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import solitour_backend.solitour.user.user_status.UserStatus;

@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
Expand All @@ -13,4 +14,5 @@ public class LoginResponse {

private Cookie accessToken;
private Cookie refreshToken;
private UserStatus loginStatus;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import solitour_backend.solitour.image.exception.ImageRequestValidationFailedException;
import solitour_backend.solitour.information.exception.InformationNotExistsException;
import solitour_backend.solitour.information.exception.InformationNotManageException;
import solitour_backend.solitour.user.exception.BlockedUserException;
import solitour_backend.solitour.user.exception.DeletedUserException;
import solitour_backend.solitour.user.exception.DormantUserException;
import solitour_backend.solitour.user.exception.UserNotExistsException;
import solitour_backend.solitour.zone_category.exception.ZoneCategoryAlreadyExistsException;
import solitour_backend.solitour.zone_category.exception.ZoneCategoryNotExistsException;
Expand Down Expand Up @@ -81,14 +84,25 @@ public ResponseEntity<String> notFoundException(Exception exception) {
}

@ExceptionHandler({GatheringNotManagerException.class,
ForbiddenAccessException.class
ForbiddenAccessException.class,
BlockedUserException.class,
DeletedUserException.class
})
public ResponseEntity<String> forbiddenException(Exception exception) {
return ResponseEntity
.status(HttpStatus.FORBIDDEN)
.body(exception.getMessage());
}

@ExceptionHandler({
DormantUserException.class
})
public ResponseEntity<String> dormantException(Exception exception) {
return ResponseEntity
.status(HttpStatus.LOCKED)
.body(exception.getMessage());
}

@ExceptionHandler({
TokenNotExistsException.class
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@
public class UserController {

private final UserService userService;
private final OauthService oauthservice;
private final KakaoConnector kakaoConnector;
private final GoogleConnector googleConnector;

public static final int PAGE_SIZE = 6;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package solitour_backend.solitour.user.exception;

public class BlockedUserException extends RuntimeException {

public BlockedUserException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package solitour_backend.solitour.user.exception;

public class DeletedUserException extends RuntimeException {

public DeletedUserException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package solitour_backend.solitour.user.exception;

public class DormantUserException extends RuntimeException {

public DormantUserException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
@Getter
public enum UserStatus {
ACTIVATE("활성화"),
INACTIVATE("비활성화"),
BLOCK("차단"),
DORMANT("휴먼"),
DELETE("삭제"),
MANAGER("관리자");
Expand Down

0 comments on commit 8f3234c

Please sign in to comment.