From 606a91d9ce57c23ce2d12f7ff1de8724a1001b99 Mon Sep 17 00:00:00 2001 From: ahah525 Date: Wed, 2 Nov 2022 10:49:11 +0900 Subject: [PATCH] =?UTF-8?q?#25=20-=20Refactor:=20AuthLevel=20Enum=20?= =?UTF-8?q?=EB=8F=84=EC=9E=85,=20AuthLevelConvert=20=EB=A1=9C=20Enum=20<->?= =?UTF-8?q?=20int=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mutbooks/app/member/entity/AuthLevel.java | 27 ++++++++++++++++++ .../app/member/entity/AuthLevelConverter.java | 28 +++++++++++++++++++ .../mutbooks/app/member/entity/Member.java | 17 +++++++---- .../app/member/service/MemberService.java | 7 +++-- 4 files changed, 70 insertions(+), 9 deletions(-) create mode 100644 3Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/member/entity/AuthLevel.java create mode 100644 3Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/member/entity/AuthLevelConverter.java diff --git a/3Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/member/entity/AuthLevel.java b/3Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/member/entity/AuthLevel.java new file mode 100644 index 0000000..52f5be4 --- /dev/null +++ b/3Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/member/entity/AuthLevel.java @@ -0,0 +1,27 @@ +package com.example.mutbooks.app.member.entity; + +import lombok.Getter; + +import java.util.Arrays; + +@Getter +public enum AuthLevel { + USER(3, "USER"), + ADMIN(7, "ADMIN"); + + private final int code; + private final String value; + + AuthLevel(int code, String value) { + this.code = code; + this.value = value; + } + + // Enum 에서 code 값으로 해당 Enum 을 찾는 메서드 + public static AuthLevel ofCode(Integer code) { + return Arrays.stream(AuthLevel.values()) + .filter(v -> v.getCode() == code) + .findFirst() + .orElseThrow(() -> new RuntimeException(String.format("code=[%d]가 존재하지 않습니다.", code))); + } +} diff --git a/3Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/member/entity/AuthLevelConverter.java b/3Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/member/entity/AuthLevelConverter.java new file mode 100644 index 0000000..97a8fe7 --- /dev/null +++ b/3Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/member/entity/AuthLevelConverter.java @@ -0,0 +1,28 @@ +package com.example.mutbooks.app.member.entity; + +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; + +/** + * AttributeConverter X, Y + * X : 엔티티의 속성에 대응하는 타입 + * Y : DB 에 대응하는 타입 + */ +@Converter +public class AuthLevelConverter implements AttributeConverter { + + // Enum -> db 데이터(code:Integer) + @Override + public Integer convertToDatabaseColumn(AuthLevel attribute) { + if(attribute == null) + return null; + + return attribute.getCode(); + } + + // db 데이터(code:Integer) -> Enum + @Override + public AuthLevel convertToEntityAttribute(Integer dbData) { + return AuthLevel.ofCode(dbData); + } +} diff --git a/3Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/member/entity/Member.java b/3Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/member/entity/Member.java index d1a7954..5580d94 100644 --- a/3Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/member/entity/Member.java +++ b/3Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/member/entity/Member.java @@ -9,6 +9,7 @@ import org.springframework.util.StringUtils; import javax.persistence.Column; +import javax.persistence.Convert; import javax.persistence.Entity; import java.util.ArrayList; import java.util.List; @@ -28,7 +29,10 @@ public class Member extends BaseEntity { private String nickname; @Column(unique = true) private String email; - private Integer authLevel; // 권한레벨(3 = 일반, 7 = 관리자) + + @Convert(converter = AuthLevelConverter.class) + private AuthLevel authLevel; // 권한레벨(3 = 일반, 7 = 관리자) + private int restCash; // 예치금 private String bankName; // 출금 은행명 @@ -54,16 +58,17 @@ public void modifyBankAccount(String bankName, String bankAccountNo) { // 권한 부여 public List genAuthorities() { List authorities = new ArrayList<>(); - authorities.add(new SimpleGrantedAuthority("MEMBER")); // 일반 회원 + // 모든 로그인한 회원에게는 USER 권한 부여 + authorities.add(new SimpleGrantedAuthority(AuthLevel.USER.getValue())); // 일반 회원 - // nickname 이 있으면 작가 권한 + // nickname 이 있으면 AUTHOR 권한 부여 if (StringUtils.hasText(nickname)) { authorities.add(new SimpleGrantedAuthority("AUTHOR")); // 작가 회원 } - // authLevel 이 7이면 관리자 권한 - if (this.authLevel == 7) { - authorities.add(new SimpleGrantedAuthority("ADMIN")); // 관리자 회원 + // authLevel 이 7이면 ADMIN 권한 부여 + if (this.authLevel == AuthLevel.ADMIN) { + authorities.add(new SimpleGrantedAuthority(AuthLevel.ADMIN.getValue())); // 관리자 회원 } return authorities; diff --git a/3Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/member/service/MemberService.java b/3Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/member/service/MemberService.java index 7ba1bbc..11d8c37 100644 --- a/3Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/member/service/MemberService.java +++ b/3Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/member/service/MemberService.java @@ -4,6 +4,7 @@ import com.example.mutbooks.app.cash.entity.CashLog; import com.example.mutbooks.app.cash.service.CashService; import com.example.mutbooks.app.mail.service.MailService; +import com.example.mutbooks.app.member.entity.AuthLevel; import com.example.mutbooks.app.member.entity.Member; import com.example.mutbooks.app.member.exception.PasswordNotMatchedException; import com.example.mutbooks.app.member.form.JoinForm; @@ -32,10 +33,10 @@ public class MemberService { @Transactional public Member join(JoinForm joinForm) { - int authLevel = 3; // 디폴트 일반 권한 - // TODO: username 이 admin 인 회원을 관리자 회원으로 설정 + AuthLevel authLevel = AuthLevel.USER; // 디폴트 USER 권한 + // username 이 admin 인 회원을 관리자 회원으로 설정 if(joinForm.getUsername().equals("admin")) { - authLevel = 7; + authLevel = AuthLevel.ADMIN; } // 기본 권한 = 일반