Skip to content

Commit

Permalink
Merge pull request EveryUniv#63 from EveryUniv/dev
Browse files Browse the repository at this point in the history
feat: 로그인 기능에서 단국대 인증 확인 로직 삭제
  • Loading branch information
gutanbug authored Jan 20, 2024
2 parents c4368db + 5be0bc6 commit 4c5330d
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.dku.council.domain.user.exception;

import com.dku.council.global.error.CustomHttpStatus;
import com.dku.council.global.error.exception.LocalizedMessageException;
import org.springframework.http.HttpStatus;

public class RequiredDkuUpdateException extends LocalizedMessageException {

public RequiredDkuUpdateException() {
super(HttpStatus.NOT_ACCEPTABLE, "required.dku-update");
super(CustomHttpStatus.REQUIRED_DKU_UPDATE, "required.dku-update");
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/dku/council/domain/user/model/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,17 @@ public void changeIsDkuChecked() {
this.isDkuChecked = !this.isDkuChecked;
}

/**
* 단국대 학생 정보를 업데이트합니다.
*
* @param age 나이
* @param gender 성별
*/
public void updateDkuInfo(String age, String gender) {
this.isDkuChecked = true;
this.age = age;
this.gender = gender;
}

/**
* 매년 1월 1일 사용자들의 나이 1을 증가시킵니다.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ public ResponseLoginDto login(RequestLoginDto dto) {
User user = userRepository.findByStudentId(dto.getStudentId())
.orElseThrow(UserNotFoundException::new);

if (!user.isDkuChecked()) {
throw new DkuAuthNotRefreshedException();
}

if (passwordEncoder.matches(dto.getPassword(), user.getPassword()) && user.isDkuChecked()) {
if (passwordEncoder.matches(dto.getPassword(), user.getPassword())) {
AuthenticationToken token = jwtProvider.issue(user);
userInfoService.cacheUserInfo(user.getId(), user);
return new ResponseLoginDto(token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.access.AccessDeniedException;
Expand Down Expand Up @@ -35,7 +36,19 @@ public class ControllerAdvisor {
protected ResponseEntity<ErrorResponseDto> localizedException(LocalizedMessageException e, Locale locale) {
ErrorResponseDto dto = new ErrorResponseDto(messageSource, locale, e);
log.error("A problem has occurred in controller advice: [id={}]", dto.getTrackingId(), e);
return filter(e, ResponseEntity.status(e.getStatus()).body(dto));
if (containsEnum(e.getStatus())) {
return filter(e, ResponseEntity.status(e.getStatusCode()).body(dto));
}
return filter(e, ResponseEntity.status(HttpStatus.valueOf(e.getStatus())).body(dto));
}

private boolean containsEnum(String constantName) {
for (CustomHttpStatus status : CustomHttpStatus.VALUES) {
if (status.name().equals(constantName)) {
return true;
}
}
return false;
}

@ExceptionHandler
Expand Down
99 changes: 99 additions & 0 deletions src/main/java/com/dku/council/global/error/CustomHttpStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.dku.council.global.error;

import lombok.Getter;
import org.springframework.lang.Nullable;

public enum CustomHttpStatus{

/**
* 600 : 단국대학교 학생 인증이 없을 경우
*/
REQUIRED_DKU_UPDATE(600, CustomSeries.DKU_ERROR, "Required DkuInfo");

public static final CustomHttpStatus[] VALUES;

static {
VALUES = values();
}

private final int value;

private final CustomSeries series;

@Getter
private final String reasonPhrase;

CustomHttpStatus(int value, CustomSeries series, String reasonPhrase) {
this.value = value;
this.series = series;
this.reasonPhrase = reasonPhrase;
}

public int value() {
return this.value;
}

public CustomSeries series() {
return this.series;
}

public String toString() {
return this.value + " " + name();
}

public static CustomHttpStatus valueOf(int statusCode) {
CustomHttpStatus status = resolve(statusCode);
if (status == null) {
throw new IllegalArgumentException("No matching constant for [" + statusCode + "]");
}
return status;
}

@Nullable
public static CustomHttpStatus resolve(int statusCode) {
for (CustomHttpStatus status : VALUES) {
if (status.value == statusCode) {
return status;
}
}
return null;
}

public enum CustomSeries {
DKU_ERROR(6);

private final int value;

CustomSeries(int value) {
this.value = value;
}

public int value() {
return this.value;
}

@Deprecated
public static CustomSeries valueOf(CustomHttpStatus status) {
return status.series;
}

public static CustomSeries valueOf(int statusCode) {
CustomSeries series = resolve(statusCode);
if (series == null) {
throw new IllegalArgumentException("No matching constant for [" + statusCode + "]");
}
return series;
}

@Nullable
public static CustomSeries resolve(int statusCode) {
int seriesCode = statusCode / 100;
for (CustomSeries series : values()) {
if (series.value == seriesCode) {
return series;
}
}
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
public class ErrorResponseDto {
private final String timestamp;
private final String trackingId;
private final HttpStatus status;
private final int statusCode;
private final String status;
private final String code;
private final List<Object> message;


public ErrorResponseDto(MessageSource messageSource, Locale locale, LocalizedMessageException e) {
this.timestamp = LocalDateTime.now().toString();
this.trackingId = UUID.randomUUID().toString();
this.statusCode = e.getStatusCode();
this.status = e.getStatus();
this.code = e.getCode();
this.message = e.getMessages(messageSource, locale);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
private void writeErrorResponse(HttpServletResponse response, Locale locale, LocalizedMessageException ex) throws IOException {
ErrorResponseDto dto = new ErrorResponseDto(messageSource, locale, ex);
log.error("A problem has occurred in filter: [id={}]", dto.getTrackingId(), ex);
writeResponse(response, dto, ex.getStatus().value());
writeResponse(response, dto, Integer.parseInt(ex.getCode()));
}

private void writeUnexpectedErrorResponse(HttpServletResponse response, Locale locale, Exception ex) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dku.council.global.error.exception;

import com.dku.council.global.error.CustomHttpStatus;
import lombok.Getter;
import org.springframework.context.MessageSource;
import org.springframework.http.HttpStatus;
Expand All @@ -11,21 +12,32 @@
@Getter
public class LocalizedMessageException extends RuntimeException {

private final HttpStatus status;
private final int code;
private final String status;
private final String messageId;
private final Object[] arguments;
private String customMessage = null;

public LocalizedMessageException(HttpStatus status, String messageId, Object... arguments) {
super(formatMessage(messageId, arguments));
this.status = status;
this.code = status.value();
this.status = status.name();
this.messageId = messageId;
this.arguments = arguments;
}

public LocalizedMessageException(Throwable cause, HttpStatus status, String messageId, Object... arguments) {
super(formatMessage(messageId, arguments), cause);
this.status = status;
this.code = status.value();
this.status = status.name();
this.messageId = messageId;
this.arguments = arguments;
}

public LocalizedMessageException(CustomHttpStatus status, String messageId, Object... arguments) {
super(formatMessage(messageId, arguments));
this.code = status.value();
this.status = status.name();
this.messageId = messageId;
this.arguments = arguments;
}
Expand All @@ -46,6 +58,10 @@ public String getCode() {
return getClass().getSimpleName();
}

public int getStatusCode() {
return this.code;
}

public static LocalizedMessageException of(Exception e) {
return new UnexpectedException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void changeDkuAuth(RequestDkuStudentDto dto) {
if (info.getStudentState().equals("재학")) {
User user = userRepository.findByStudentId(dto.getDkuStudentId())
.orElseThrow(UserNotFoundException::new);
user.changeIsDkuChecked();
user.updateDkuInfo(info.getAge(), info.getGender());
}
else {
throw new FailedAuthRefreshException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ public void create_properly_localized() {
DateTimeFormatter.ISO_DATE_TIME.parse(dto.getTimestamp());
assertThat(dto.getCode()).isEqualTo("LocalizedMessageException");
assertThat(dto.getMessage()).isEqualTo(List.of("localizedMessage"));
assertThat(dto.getStatus()).isEqualTo(HttpStatus.OK);
assertThat(dto.getStatus()).isEqualTo(HttpStatus.OK.name());
}
}

0 comments on commit 4c5330d

Please sign in to comment.