Skip to content

Commit

Permalink
Merge pull request #49 from Sopo2023/feat/#44
Browse files Browse the repository at this point in the history
chore: modify member profile
  • Loading branch information
GayeongKimm authored Jul 31, 2024
2 parents 2693d0f + 5c7e40a commit 7c2599c
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.thymeleaf.TemplateEngine;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
Expand All @@ -26,7 +25,6 @@ public class AuthEmailService {
private final MemberRepository memberRepository;
private final MailService mailService;
private final StringRedisTemplate redisTemplate;
private final TemplateEngine templateEngine;

@Value("${spring.mail.auth-code-expiration-millis}")
private long authCodeExpirationMillis;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
import kr.hs.dgsw.SOPO_server_v2.domain.member.enums.MemberCategory;
import kr.hs.dgsw.SOPO_server_v2.domain.member.enums.MemberState;
import kr.hs.dgsw.SOPO_server_v2.global.common.entity.BaseTimeEntity;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

@Entity
@Getter
@Setter
@SuperBuilder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@EntityListeners(AuditingEntityListener.class)
Expand Down Expand Up @@ -57,10 +55,4 @@ public class MemberEntity extends BaseTimeEntity {
@OneToOne(fetch = FetchType.LAZY, orphanRemoval = true)
@JoinColumn(name = "member_file")
private FileEntity memberProfile;

public void update(String memberName, String memberEmail, String memberPassword){
this.memberName = memberName;
this.memberEmail = memberEmail;
this.memberPassword = memberPassword;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public record MemberModifyReq(
String memberName,
@Email
String memberEmail,
String authCode,
String memberPassword
) {
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package kr.hs.dgsw.SOPO_server_v2.domain.member.service;

import kr.hs.dgsw.SOPO_server_v2.domain.auth.service.AuthEmailService;
import kr.hs.dgsw.SOPO_server_v2.domain.member.entity.MemberEntity;
import kr.hs.dgsw.SOPO_server_v2.domain.member.enums.MemberCategory;
import kr.hs.dgsw.SOPO_server_v2.domain.member.presentation.dto.req.MemberModifyReq;
import kr.hs.dgsw.SOPO_server_v2.domain.member.repository.MemberRepository;
import kr.hs.dgsw.SOPO_server_v2.global.error.custom.email.CodeIsWrongException;
import kr.hs.dgsw.SOPO_server_v2.global.error.custom.member.MemberNotCoincideException;
import kr.hs.dgsw.SOPO_server_v2.global.error.custom.member.NeedAuthCode;
import kr.hs.dgsw.SOPO_server_v2.global.infra.security.GetCurrentMember;
import kr.hs.dgsw.SOPO_server_v2.global.response.Response;
import lombok.RequiredArgsConstructor;
Expand All @@ -16,10 +21,37 @@
public class MemberService {
private final MemberRepository memberRepository;
private final GetCurrentMember getCurrentMember;
private final AuthEmailService authEmailService;

@Transactional(rollbackFor = Exception.class)
public Response memberModify(MemberModifyReq memberModifyReq) {
MemberEntity member = memberRepository.findByMemberId(getCurrentMember.current().getMemberId());
member.update(memberModifyReq.memberName(), member.getMemberEmail(), new BCryptPasswordEncoder().encode(memberModifyReq.memberPassword()));

String memberEmail = memberModifyReq.memberEmail();
String authCode = memberModifyReq.authCode();

if (memberEmail != null && !memberEmail.isEmpty()) {
if (authCode == null || authCode.isEmpty()) {
throw NeedAuthCode.EXCEPTION;
}
if (!authEmailService.verifiedCode(memberEmail, authCode)) {
throw CodeIsWrongException.EXCEPTION;
}
member.setMemberEmail(memberEmail);
}

String memberPassword = memberModifyReq.memberPassword();
if (memberPassword != null) {
member.setMemberPassword(new BCryptPasswordEncoder().encode(memberPassword));
}

String memberName = memberModifyReq.memberName();
if (memberName != null) {
member.setMemberName(memberName);
}

memberRepository.save(member);

return Response.of(HttpStatus.OK, "성공");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.HttpStatusEntryPoint;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfiguration;
Expand All @@ -25,6 +26,7 @@
public class SecurityConfig {
private final JwtFilter jwtFilter;
private final JwtExceptionFilter jwtExceptionFilter;
private final AccessDeniedHandler accessDeniedHandler;

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
Expand Down Expand Up @@ -54,7 +56,8 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.formLogin().disable()
.exceptionHandling()
.accessDeniedHandler((req, res, e) -> jwtExceptionFilter.responseToClient(res, ErrorResponse.of(StatusEnum.INVALID_ROLE, "권한이 없습니다")))
.authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.FORBIDDEN));
.authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.FORBIDDEN))
.accessDeniedHandler(accessDeniedHandler);

return http.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package kr.hs.dgsw.SOPO_server_v2.global.error.custom.member;

import kr.hs.dgsw.SOPO_server_v2.global.error.exception.BusinessException;
import kr.hs.dgsw.SOPO_server_v2.global.error.exception.StatusEnum;

public class NeedAuthCode extends BusinessException {
public static final BusinessException EXCEPTION = new NeedAuthCode();

public NeedAuthCode() {
super(StatusEnum.NEED_AUTH_CODE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public enum StatusEnum {
UNABLE_TO_SEND_EMAIL(403, "이메일을 보낼 수 없습니다."),
EMAIL_ALREADY_EXIST(404, "이메일이 이미 존재합니다"),
CODE_IS_WRONG(404, "인증코드가 옳지 않습니다."),
NEED_AUTH_CODE(404, "인증코드가 없습니다."),

//fcm
MESSAGE_SEND_FAILED(403, "메세지 전송에 실패했습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,7 @@ public Authentication getAuthentication(String accessToken) {
}

public Claims getClaims(String token) {
try{
return Jwts.parserBuilder()
.setSigningKey(jwtProperties.getSecretKey()).build().parseClaimsJws(token).getBody();
} catch (ExpiredJwtException e) {
throw new IllegalArgumentException("만료된 토큰");
} catch (UnsupportedJwtException e) {
throw new IllegalArgumentException("지원되지 않는 토큰");
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("잘못된 토큰");
}
return Jwts.parserBuilder().setSigningKey(jwtProperties.getSecretKey()).build().parseClaimsJws(token).getBody();
}

public String extractToken(final String token) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package kr.hs.dgsw.SOPO_server_v2.global.infra.security;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import kr.hs.dgsw.SOPO_server_v2.global.error.custom.auth.ExpiredTokenException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

@Component
public class CustomAccessDeniedHandler implements AccessDeniedHandler {

@Override
public void handle(HttpServletRequest request,
HttpServletResponse response,
AccessDeniedException accessDeniedException){
throw ExpiredTokenException.EXCEPTION;
}
}

0 comments on commit 7c2599c

Please sign in to comment.