Skip to content

Commit

Permalink
#25 - Refactor: AuthLevel Enum 도입, AuthLevelConvert 로 Enum <-> int 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
ahah525 committed Nov 2, 2022
1 parent 3c3a0f2 commit 606a91d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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)));
}
}
Original file line number Diff line number Diff line change
@@ -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<AuthLevel, Integer> {

// 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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; // 출금 은행명
Expand All @@ -54,16 +58,17 @@ public void modifyBankAccount(String bankName, String bankAccountNo) {
// 권한 부여
public List<GrantedAuthority> genAuthorities() {
List<GrantedAuthority> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

// 기본 권한 = 일반
Expand Down

0 comments on commit 606a91d

Please sign in to comment.