Skip to content

Commit

Permalink
[refactor/InhaBas#179] Member 전반적인 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
whitem4rk committed Dec 1, 2023
1 parent 7133fa9 commit 3ba95eb
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,17 @@ public Member(String name, String email, Role role) {
this.ibasInformation = new IbasInformation(role);
}

public String getStudentId() {
return this.studentId != null ? this.studentId.getValue() : null;
}

public String getName() {
return this.name != null ? this.name.getValue() : null;
}

public String getPhone() {
return this.phone != null ? this.phone.getValue() : null;
}

public SchoolInformation getSchoolInformation() {
return this.schoolInformation != null ? this.schoolInformation : null;
}
Expand All @@ -123,6 +127,10 @@ public boolean isDeleted() {
public void setName(String name) {
this.name = new Name(name);
}

public void setStudentId(String studentId) {
this.studentId = new StudentId(studentId);
}
public void setPhone(String phone) {
this.phone = new Phone(phone);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public List<NotApprovedMemberManagementDto> getNotApprovedMembersBySearchAndRole
.map(member -> new NotApprovedMemberManagementDto(
member.getName(),
member.getId(),
member.getStudentId().getValue(),
member.getStudentId(),
member.getPhone(),
member.getEmail(),
member.getSchoolInformation().getGrade(),
Expand All @@ -106,7 +106,7 @@ public List<ApprovedMemberManagementDto> getApprovedMembersBySearchAndRole(Strin
.map(member -> new ApprovedMemberManagementDto(
member.getName(),
member.getId(),
member.getStudentId().getValue(),
member.getStudentId(),
member.getPhone(),
member.getRole(),
member.getSchoolInformation().getGeneration(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,9 @@
public interface MemberRepository extends JpaRepository<Member, Long>, MemberRepositoryCustom {

List<Member> findAllById(Iterable<Long> memberIdList);

Member getByStudentId(StudentId studentId);

Optional<Member> findByStudentId(StudentId studentId);

Member findByIbasInformation_Role(Role role);


// OAuth
boolean existsByProviderAndUid(OAuth2Provider provider, UID uid);
Optional<Member> findByProviderAndUid(OAuth2Provider provider, UID uid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.inhabas.api.auth.domain.oauth2.member.domain.entity.Member;
import com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.Role;
import com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.StudentId;
import com.inhabas.api.auth.domain.oauth2.member.dto.MemberDuplicationQueryCondition;
import com.inhabas.api.auth.domain.oauth2.member.security.MemberAuthorityProvider;

Expand All @@ -11,7 +10,7 @@

public interface MemberRepositoryCustom {

MemberAuthorityProvider.RoleDto fetchRoleByStudentId(StudentId studentId);
MemberAuthorityProvider.RoleDto fetchRoleByStudentId(Long id);

boolean isDuplicated(MemberDuplicationQueryCondition condition);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.inhabas.api.auth.domain.oauth2.OAuth2Provider;
import com.inhabas.api.auth.domain.oauth2.member.domain.entity.Member;
import com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.Role;
import com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.StudentId;
import com.inhabas.api.auth.domain.oauth2.member.dto.MemberDuplicationQueryCondition;
import com.inhabas.api.auth.domain.oauth2.member.security.MemberAuthorityProvider;
import com.inhabas.api.auth.domain.oauth2.socialAccount.type.UID;
Expand All @@ -29,10 +28,10 @@ public class MemberRepositoryImpl implements MemberRepositoryCustom {
private final JPAQueryFactory queryFactory;

@Override
public MemberAuthorityProvider.RoleDto fetchRoleByStudentId(StudentId studentId) {
public MemberAuthorityProvider.RoleDto fetchRoleByStudentId(Long id) {
Role role = queryFactory
.select(member.ibasInformation.role).from(member)
.where(member.studentId.eq(studentId))
.where(member.id.eq(id))
.fetchOne();

return new MemberAuthorityProvider.RoleDto(role);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@

import com.inhabas.api.auth.domain.exception.InvalidUserInfoException;
import com.inhabas.api.auth.domain.oauth2.member.domain.entity.Member;
import com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.StudentId;
import com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.Role;
import com.inhabas.api.auth.domain.oauth2.member.repository.MemberRepository;
import com.inhabas.api.auth.domain.oauth2.socialAccount.type.UID;
import com.inhabas.api.auth.domain.oauth2.userAuthorityProvider.UserAuthorityProvider;
import com.inhabas.api.auth.domain.oauth2.userInfo.OAuth2UserInfo;
import com.inhabas.api.auth.domain.oauth2.userInfo.OAuth2UserInfoAuthentication;
import com.inhabas.api.auth.domain.token.securityFilter.UserPrincipalService;
import com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.Role;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;

import static com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.Role.SIGNING_UP;

Expand All @@ -33,9 +34,9 @@ public Collection<SimpleGrantedAuthority> determineAuthorities(OAuth2UserInfo oA

OAuth2UserInfoAuthentication authentication =
new OAuth2UserInfoAuthentication(oAuth2UserInfo.getId(), oAuth2UserInfo.getProvider().toString(), oAuth2UserInfo.getEmail());
StudentId studentId = (StudentId) userPrincipalService.loadUserPrincipal(authentication);
Long memberId = (Long) userPrincipalService.loadUserPrincipal(authentication);

if (Objects.isNull(studentId)) { // 기존회원이 아니면, member 테이블에 임시데이터 저장
if (Objects.isNull(memberId)) { // 기존회원이 아니면, member 테이블에 임시데이터 저장
Member member = memberRepository.findByProviderAndUid(
oAuth2UserInfo.getProvider(), new UID(oAuth2UserInfo.getId()))
.orElseThrow(InvalidUserInfoException::new);
Expand All @@ -46,7 +47,7 @@ public Collection<SimpleGrantedAuthority> determineAuthorities(OAuth2UserInfo oA
}
else {
// 기존회원이면,
RoleDto roleDto = memberRepository.fetchRoleByStudentId(studentId);
RoleDto roleDto = memberRepository.fetchRoleByStudentId(memberId);

if (roleDto.isEmpty())
throw new InvalidUserInfoException(); // 가입된 소셜 계정으로 회원 프로필을 찾을 수 없는 경우.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import com.inhabas.api.auth.domain.oauth2.OAuth2Provider;
import com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.Email;
import com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.StudentId;
import com.inhabas.api.auth.domain.oauth2.member.security.socialAccount.MemberSocialAccount;
import com.inhabas.api.auth.domain.oauth2.member.security.socialAccount.MemberSocialAccountRepository;
import com.inhabas.api.auth.domain.oauth2.socialAccount.type.UID;
import com.inhabas.api.auth.domain.oauth2.userInfo.OAuth2UserInfoAuthentication;
import com.inhabas.api.auth.domain.token.securityFilter.UserPrincipalNotFoundException;
import com.inhabas.api.auth.domain.token.securityFilter.UserPrincipalService;
import com.inhabas.api.auth.domain.oauth2.member.security.socialAccount.MemberSocialAccountRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
Expand Down Expand Up @@ -43,23 +42,23 @@ public Object loadUserPrincipal(Authentication authentication) {
Email email = new Email(oauth2UserInfoToken.getEmail());

try {
StudentId studentId = this.getMemberId(provider, uid, email).orElseThrow(UserPrincipalNotFoundException::new);
return studentId;
Long memberId = this.getMemberId(provider, uid, email).orElseThrow(UserPrincipalNotFoundException::new);
return memberId;
} catch (UserPrincipalNotFoundException e) {
log.info(e.getMessage());
return null;
}
}


private Optional<StudentId> getMemberId(OAuth2Provider provider, UID uid, Email email) {
private Optional<Long> getMemberId(OAuth2Provider provider, UID uid, Email email) {

return memberSocialAccountRepository.findMemberIdByUidAndProvider(uid, provider)
.or(() -> this.findByEmailAndProviderForLegacy(email, provider, uid));
}


private Optional<StudentId> findByEmailAndProviderForLegacy(Email email, OAuth2Provider provider, UID uid) {
private Optional<Long> findByEmailAndProviderForLegacy(Email email, OAuth2Provider provider, UID uid) {

Optional<MemberSocialAccount> memberSocialAccount =
memberSocialAccountRepository.findMemberSocialAccountByEmailAndProvider(email, provider);
Expand All @@ -69,7 +68,7 @@ private Optional<StudentId> findByEmailAndProviderForLegacy(Email email, OAuth2P
MemberSocialAccount socialAccount = memberSocialAccount.get();
socialAccount.SetUID(uid);
memberSocialAccountRepository.save(socialAccount);
return Optional.of(socialAccount.getMember().getStudentId());
return Optional.of(socialAccount.getMember().getId());

} else {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.inhabas.api.auth.domain.oauth2.member.security.socialAccount;

import com.inhabas.api.auth.domain.oauth2.OAuth2Provider;
import com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.StudentId;
import com.inhabas.api.auth.domain.oauth2.socialAccount.type.UID;

import java.util.Optional;

public interface MemberSocialAccountRepositoryCustom {

Optional<StudentId> findMemberIdByUidAndProvider(UID uid, OAuth2Provider provider);
Optional<Long> findMemberIdByUidAndProvider(UID uid, OAuth2Provider provider);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.inhabas.api.auth.domain.oauth2.member.security.socialAccount;

import com.inhabas.api.auth.domain.oauth2.OAuth2Provider;
import com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.StudentId;
import com.inhabas.api.auth.domain.oauth2.socialAccount.type.UID;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
Expand All @@ -17,9 +16,9 @@ public class MemberSocialAccountRepositoryImpl implements MemberSocialAccountRep
private final JPAQueryFactory jpaQueryFactory;

@Override
public Optional<StudentId> findMemberIdByUidAndProvider(UID uid, OAuth2Provider provider) {
public Optional<Long> findMemberIdByUidAndProvider(UID uid, OAuth2Provider provider) {
return Optional.ofNullable(jpaQueryFactory
.select(memberSocialAccount.member.studentId)
.select(memberSocialAccount.member.id)
.where(eqSocialAccount(uid, provider))
.from(memberSocialAccount)
.fetchOne());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static CommentDetailDto fromEntity(Comment comment) {
return new CommentDetailDto(
comment.getId(),
comment.getContents(),
comment.getWriter().getStudentId(),
new StudentId("11121212"), // 임시방편, 수정 필요
comment.getWriter().getName(),
comment.getWriter().getSchoolInformation().getMajor(),
comment.getDateCreated()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.inhabas.api.web.argumentResolver;

import com.inhabas.api.auth.domain.oauth2.userInfo.OAuth2UserInfoAuthentication;
import com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.StudentId;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.NotImplementedException;
import org.springframework.core.MethodParameter;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
Expand Down Expand Up @@ -37,45 +33,8 @@ public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer m
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

if (Objects.isNull(authentication)) return null; // login not processed, anonymous user!
return (Long) authentication.getPrincipal();

if (isMemberIdType(parameter))
return resolveMemberId(authentication);
else if (isOAuth2UserInfoAuthenticationType(parameter))
return authentication;
else
throw new IllegalArgumentException("지원하지 않는 타입입니다");
}

private StudentId resolveMemberId(Authentication authentication) {

StudentId studentId = null;

if (isOAuth2UserInfoAuthenticationType(authentication)) { // jwt 토큰 인증 이후
studentId = (StudentId) authentication.getPrincipal();

} else if (authentication instanceof OAuth2AuthenticationToken) { // 소셜 로그인 인증 이후
throw new NotImplementedException("소셜로그인 구현 완료 후에 작업해야됨!");
}
else {
log.warn("{} - cannot resolve authenticated User's Id!", this.getClass());
}

return studentId;
}

private boolean isMemberIdType(MethodParameter parameter) {

return parameter.getParameterType().equals(StudentId.class);
}

private boolean isOAuth2UserInfoAuthenticationType(MethodParameter parameter) {

return OAuth2UserInfoAuthentication.class
.isAssignableFrom(parameter.getParameterType());
}
private boolean isOAuth2UserInfoAuthenticationType(Authentication authentication) {

return OAuth2UserInfoAuthentication.class
.isAssignableFrom(authentication.getClass());
}
}

0 comments on commit 3ba95eb

Please sign in to comment.