Skip to content

Commit

Permalink
Merge pull request #103 from JNU-econovation/member
Browse files Browse the repository at this point in the history
Member 관련 작업
  • Loading branch information
inferior3x authored Dec 28, 2024
2 parents 8ace277 + 99f9444 commit d67b3c0
Show file tree
Hide file tree
Showing 94 changed files with 802 additions and 318 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,30 @@
@Builder(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public final class Device extends AggregateRoot {
private Long deviceId;
private Long id;
private Long memberId;
private MacAddress mac;
private IpAddress ip;
private String name;

public static Device create(Long memberId, MacAddress mac, IpAddress ip){
public static Device create(Long memberId, MacAddress mac, IpAddress ip, String name){
Device device = Device.builder()
.memberId(memberId)
.mac(mac)
.ip(ip)
.name(name)
.build();
device.register(new DeviceCreated());
return device;
}

public static Device load(Long deviceId, Long memberId, String macAddress, String ipAddress){
public static Device load(Long id, Long memberId, String mac, String ip, String name){
return Device.builder()
.deviceId(deviceId)
.id(id)
.memberId(memberId)
.mac(MacAddress.load(macAddress))
.ip(IpAddress.load(ipAddress))
.mac(MacAddress.load(mac))
.ip(IpAddress.load(ip))
.name(name)
.build();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package com.whoz_in.domain.member;

import com.whoz_in.domain.member.exception.NoMemberException;
import com.whoz_in.domain.member.model.Member;
import java.util.Optional;

public interface MemberRepository {
void save(Member member);
Optional<Member> findByLoginId(String loginId);
default Member getByLoginId(String loginId){
return findByLoginId(loginId).orElseThrow(NoMemberException::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.whoz_in.domain.member.event;

import com.whoz_in.domain.member.model.Member;
import com.whoz_in.domain.shared.event.DomainEvent;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public final class MemberCreated extends DomainEvent {
private final Member member;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.whoz_in.domain.member.event;

import com.whoz_in.domain.member.model.MemberId;
import com.whoz_in.domain.shared.event.DomainEvent;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public final class MemberPasswordChanged extends DomainEvent {
private final MemberId memberId;
private final String newPassword;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.whoz_in.domain.member.event;

import com.whoz_in.domain.member.model.MemberId;
import com.whoz_in.domain.shared.event.DomainEvent;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public final class MemberStatusMessageChanged extends DomainEvent {
private final MemberId memberId;
private final String statusMessage;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.whoz_in.domain.member.exception;

import com.whoz_in.domain.shared.BusinessException;

public class InvalidAuthCredentialsException extends BusinessException {

public InvalidAuthCredentialsException() {
super("2003", "아이디 혹은 비밀번호가 틀렸습니다.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.whoz_in.domain.member.exception;

import com.whoz_in.domain.shared.BusinessException;

public class LoginIdPolicyViolationException extends BusinessException {

public LoginIdPolicyViolationException() {
super("2001", "아이디는 알파벳 소문자, 숫자로 6자리 이상 16자리 이하여야 합니다.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.whoz_in.domain.member.exception;

import com.whoz_in.domain.shared.BusinessException;

public class NoMemberException extends BusinessException {

public NoMemberException() {
super("2008", "존재하지 않는 멤버입니다.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.whoz_in.domain.member.exception;

import com.whoz_in.domain.shared.BusinessException;

public class NotAuthMemberException extends BusinessException {
public NotAuthMemberException() {
super("2006", "일반 로그인 정보가 없는 회원입니다.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.whoz_in.domain.member.exception;

import com.whoz_in.domain.shared.BusinessException;

public class NotOAuthMemberException extends BusinessException {

public NotOAuthMemberException() {
super("2007", "소셜 로그인 정보가 없는 회원입니다.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.whoz_in.domain.member.exception;

import com.whoz_in.domain.shared.BusinessException;

public class PasswordPolicyViolationException extends BusinessException {

public PasswordPolicyViolationException() {
super("2002", "비밀번호는 알파벳 소문자, 숫자로 6자리 이상 16자리 이하여야 합니다.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.whoz_in.domain.member.exception;

import com.whoz_in.domain.shared.BusinessException;

public class WrongPasswordException extends BusinessException {

public WrongPasswordException() {
super("2005", "틀린 비밀번호입니다.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.whoz_in.domain.member.model;

import com.whoz_in.domain.member.exception.InvalidAuthCredentialsException;
import com.whoz_in.domain.member.exception.LoginIdPolicyViolationException;
import com.whoz_in.domain.member.exception.PasswordPolicyViolationException;
import com.whoz_in.domain.member.exception.WrongPasswordException;
import com.whoz_in.domain.member.service.PasswordEncoder;
import java.util.regex.Pattern;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

//일반 로그인 정보
@Getter
@EqualsAndHashCode
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public final class AuthCredentials {
private static final String LOGIN_ID_REGEX = "^(?=.*[a-z])(?=.*\\d)[a-zA-Z\\d]{6,16}$";
private static final String PASSWORD_REGEX = "^(?=.*[a-z])(?=.*\\d)[a-zA-Z\\d]{6,16}$";
private final String loginId;
private final String encodedPassword;

public static AuthCredentials create(String loginId, String rawPassword, PasswordEncoder passwordEncoder){
//아이디가 정책에 맞는지 확인
if (!Pattern.matches(LOGIN_ID_REGEX, loginId))
throw new LoginIdPolicyViolationException();
//비밀번호가 정책에 맞는지 확인
if (!Pattern.matches(PASSWORD_REGEX, rawPassword))
throw new PasswordPolicyViolationException();
return new AuthCredentials(loginId, passwordEncoder.encode(rawPassword));
}

public static AuthCredentials load(String loginId, String encodedPassword){
if (loginId == null || encodedPassword == null)
throw new IllegalStateException("no login id or encoded password");
return new AuthCredentials(loginId, encodedPassword);
}

AuthCredentials changePassword(String rawOldPassword, String rawNewPassword, PasswordEncoder passwordEncoder){
String encodedOldPassword = passwordEncoder.encode(rawOldPassword);
//기존 비밀번호가 같은지 확인
if (!encodedPassword.equals(encodedOldPassword))
throw new WrongPasswordException();
//새로운 비밀번호가 정책에 맞는지 확인
if (!Pattern.matches(PASSWORD_REGEX, rawNewPassword))
throw new PasswordPolicyViolationException();
return new AuthCredentials(this.loginId, passwordEncoder.encode(rawNewPassword));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.whoz_in.domain.member.model;

import com.whoz_in.domain.member.event.MemberCreated;
import com.whoz_in.domain.member.event.MemberPasswordChanged;
import com.whoz_in.domain.member.event.MemberStatusMessageChanged;
import com.whoz_in.domain.member.exception.NotAuthMemberException;
import com.whoz_in.domain.member.service.PasswordEncoder;
import com.whoz_in.domain.shared.AggregateRoot;
import com.whoz_in.domain.shared.Nullable;
import java.util.Optional;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Builder(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public final class Member extends AggregateRoot {
@Getter private final MemberId id;
@Getter private final String name; //실명
@Getter private final Position mainPosition;
@Getter private final int generation; //기수
@Getter private String statusMessage; //상태 메세지
@Nullable private AuthCredentials authCredentials;
@Nullable private OAuthCredentials oAuthCredentials;

//일반 로그인이 아닐수도 있으므로 Optional
public Optional<AuthCredentials> getAuthCredentials(){
return Optional.ofNullable(authCredentials);
}
//소셜 로그인이 아닐수도 있으므로 Optional
public Optional<OAuthCredentials> getOAuthCredentials(){
return Optional.ofNullable(oAuthCredentials);
}

//일반 회원가입
public static Member create(String name, Position mainPosition, int generation, AuthCredentials authCredentials){
return create(name, mainPosition, generation, authCredentials, null);
}
//소셜 회원가입
public static Member create(String name, Position mainPosition, int generation, OAuthCredentials oAuthCredentials){
return create(name, mainPosition, generation, null, oAuthCredentials);
}
private static Member create(String name, Position mainPosition, int generation,
AuthCredentials authCredentials, OAuthCredentials oAuthCredentials){
if (authCredentials == null && oAuthCredentials == null)
throw new IllegalStateException("no auth and oauth");
Member member = builder()
.id(new MemberId())
.name(name)
.mainPosition(mainPosition)
.generation(generation)
.statusMessage("")
.authCredentials(authCredentials)
.oAuthCredentials(oAuthCredentials)
.build();
member.register(new MemberCreated(member));
return member;
}

public static Member load(MemberId id, String name, Position mainPosition, int generation, String statusMessage,
AuthCredentials authCredentials, OAuthCredentials oAuthCredentials){
return builder()
.id(id)
.name(name)
.mainPosition(mainPosition)
.generation(generation)
.statusMessage(statusMessage)
.authCredentials(authCredentials)
.oAuthCredentials(oAuthCredentials)
.build();
}

public void changePassword(String rawOldPassword, String rawNewPassword, PasswordEncoder passwordEncoder){
if (authCredentials == null)
throw new NotAuthMemberException();
this.authCredentials = this.authCredentials.changePassword(rawOldPassword, rawNewPassword, passwordEncoder);
this.register(new MemberPasswordChanged(this.getId(), this.authCredentials.getEncodedPassword()));
}

public void changeStatusMessage(String newStatusMessage){
this.statusMessage = newStatusMessage;
this.register(new MemberStatusMessageChanged(this.getId(), this.statusMessage));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.whoz_in.domain.member.model;

import java.util.UUID;

public record MemberId(UUID id) {
public MemberId() {
this(UUID.randomUUID());
}

@Override
public String toString() {
return id.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.whoz_in.domain.member.model;

import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

//소셜 로그인 정보
@Getter
@EqualsAndHashCode
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public final class OAuthCredentials {
private final SocialProvider socialProvider;
private final String socialId;

public static OAuthCredentials load(SocialProvider socialProvider, String socialId){
if (socialProvider == null || socialId == null)
throw new IllegalStateException("no social provider or social id");
return new OAuthCredentials(socialProvider, socialId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.whoz_in.domain.member.model;

public enum Position {
AI,
BE,
DE,
FE,
PM,
GAME
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.whoz_in.domain.member.model;

public enum SocialProvider {
KAKAO
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//package com.whoz_in.domain.member.service;
//
//import com.whoz_in.domain.member.model.MemberId;


//TODO: db의 힘을 빌려야 할거 같을 때
//public interface MemberIdGenerator{
// MemberId next();
//}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.whoz_in.domain.member.service;

public interface PasswordEncoder {
String encode(String plainText);
}
Loading

0 comments on commit d67b3c0

Please sign in to comment.