From 9a3e43cc61a209956ee4883b2c49a4cc78b3b05d Mon Sep 17 00:00:00 2001 From: ebcho Date: Sun, 7 Jan 2024 21:47:32 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20Role=20=EB=8F=84=EB=A9=94=EC=9D=B8?= =?UTF-8?q?=20=EC=82=AD=EC=A0=9C,=20enum=EC=9C=BC=EB=A1=9C=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/BookingServiceTest.java | 7 -- .../controller/AdminManagementController.java | 91 --------------- .../dto/request/AdminCreateRequest.java | 4 +- .../dto/request/AdminUpdateRequest.java | 3 +- .../dto/request/MemberSignUpRequest.java | 4 +- .../dto/request/PermissionCreateRequest.java | 17 --- .../dto/request/PermissionUpdateRequest.java | 11 -- .../dto/request/RoleCreateRequest.java | 17 --- .../dto/request/RoleUpdateRequest.java | 11 -- .../dto/response/AdminGetResponse.java | 2 +- .../dto/response/MemberDetailGetResponse.java | 2 +- .../dto/response/PermissionGetResponse.java | 20 ---- .../dto/response/RoleGetResponse.java | 16 --- .../pgms/apimember/service/AdminService.java | 18 +-- .../pgms/apimember/service/AuthService.java | 2 +- .../pgms/apimember/service/MemberService.java | 18 +-- .../apimember/service/PermissionService.java | 67 ----------- .../pgms/apimember/service/RoleService.java | 104 ------------------ .../pgms/coredomain/domain/member/Admin.java | 6 +- .../pgms/coredomain/domain/member/Member.java | 13 +-- .../coredomain/domain/member/Permission.java | 36 ------ .../pgms/coredomain/domain/member/Role.java | 54 --------- .../domain/member/RolePermission.java | 39 ------- .../coredomain/domain/member/enums/Role.java | 7 ++ .../member/repository/AdminRepository.java | 7 -- .../member/repository/MemberRepository.java | 14 +-- .../repository/PermissionRepository.java | 13 --- .../repository/RolePermissionRepository.java | 9 -- .../member/repository/RoleRepository.java | 13 --- core/core-infra/src/main/resources/data.sql | 22 ++-- .../OAuth2AuthenticationSuccessHandler.java | 11 +- .../security/service/UserDetailsImpl.java | 4 +- 32 files changed, 44 insertions(+), 618 deletions(-) delete mode 100644 api/api-member/src/main/java/com/pgms/apimember/dto/request/PermissionCreateRequest.java delete mode 100644 api/api-member/src/main/java/com/pgms/apimember/dto/request/PermissionUpdateRequest.java delete mode 100644 api/api-member/src/main/java/com/pgms/apimember/dto/request/RoleCreateRequest.java delete mode 100644 api/api-member/src/main/java/com/pgms/apimember/dto/request/RoleUpdateRequest.java delete mode 100644 api/api-member/src/main/java/com/pgms/apimember/dto/response/PermissionGetResponse.java delete mode 100644 api/api-member/src/main/java/com/pgms/apimember/dto/response/RoleGetResponse.java delete mode 100644 api/api-member/src/main/java/com/pgms/apimember/service/PermissionService.java delete mode 100644 api/api-member/src/main/java/com/pgms/apimember/service/RoleService.java delete mode 100644 core/core-domain/src/main/java/com/pgms/coredomain/domain/member/Permission.java delete mode 100644 core/core-domain/src/main/java/com/pgms/coredomain/domain/member/Role.java delete mode 100644 core/core-domain/src/main/java/com/pgms/coredomain/domain/member/RolePermission.java create mode 100644 core/core-domain/src/main/java/com/pgms/coredomain/domain/member/enums/Role.java delete mode 100644 core/core-domain/src/main/java/com/pgms/coredomain/domain/member/repository/PermissionRepository.java delete mode 100644 core/core-domain/src/main/java/com/pgms/coredomain/domain/member/repository/RolePermissionRepository.java delete mode 100644 core/core-domain/src/main/java/com/pgms/coredomain/domain/member/repository/RoleRepository.java diff --git a/api/api-booking/src/test/java/com/pgms/apibooking/service/BookingServiceTest.java b/api/api-booking/src/test/java/com/pgms/apibooking/service/BookingServiceTest.java index 4dd47881..9e6fdebb 100644 --- a/api/api-booking/src/test/java/com/pgms/apibooking/service/BookingServiceTest.java +++ b/api/api-booking/src/test/java/com/pgms/apibooking/service/BookingServiceTest.java @@ -51,10 +51,8 @@ import com.pgms.coredomain.domain.event.repository.EventSeatRepository; import com.pgms.coredomain.domain.event.repository.EventTimeRepository; import com.pgms.coredomain.domain.member.Member; -import com.pgms.coredomain.domain.member.Role; import com.pgms.coredomain.domain.member.enums.Provider; import com.pgms.coredomain.domain.member.repository.MemberRepository; -import com.pgms.coredomain.domain.member.repository.RoleRepository; @SpringBootTest @Import(TestConfig.class) @@ -86,20 +84,15 @@ class BookingServiceTest { @Autowired private MemberRepository memberRepository; - @Autowired - private RoleRepository roleRepository; - private Member member; @BeforeEach void setup() { - Role role = roleRepository.save(new Role("ROLE_USER")); member = memberRepository.save(Member.builder() .email("test@gmail.com") .password("test1234!") .name("홍길동") .provider(Provider.KAKAO) - .role(role) .phoneNumber("010-123-456") .build()); } diff --git a/api/api-member/src/main/java/com/pgms/apimember/controller/AdminManagementController.java b/api/api-member/src/main/java/com/pgms/apimember/controller/AdminManagementController.java index 3c9e21bb..4c06dbec 100644 --- a/api/api-member/src/main/java/com/pgms/apimember/controller/AdminManagementController.java +++ b/api/api-member/src/main/java/com/pgms/apimember/controller/AdminManagementController.java @@ -3,7 +3,6 @@ import java.net.URI; import java.util.List; -import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; @@ -20,16 +19,8 @@ import com.pgms.apimember.dto.request.AdminCreateRequest; import com.pgms.apimember.dto.request.AdminUpdateRequest; import com.pgms.apimember.dto.request.PageCondition; -import com.pgms.apimember.dto.request.PermissionCreateRequest; -import com.pgms.apimember.dto.request.PermissionUpdateRequest; -import com.pgms.apimember.dto.request.RoleCreateRequest; -import com.pgms.apimember.dto.request.RoleUpdateRequest; import com.pgms.apimember.dto.response.AdminGetResponse; -import com.pgms.apimember.dto.response.PermissionGetResponse; -import com.pgms.apimember.dto.response.RoleGetResponse; import com.pgms.apimember.service.AdminService; -import com.pgms.apimember.service.PermissionService; -import com.pgms.apimember.service.RoleService; import com.pgms.coredomain.response.ApiResponse; import jakarta.validation.Valid; @@ -42,8 +33,6 @@ public class AdminManagementController { // 슈퍼관리자 전용 컨트롤러 private final AdminService adminService; - private final RoleService roleService; - private final PermissionService permissionService; /** * 관리자 CRUD @@ -77,84 +66,4 @@ public ResponseEntity> deleteAdmins(@RequestParam List a adminService.deleteAdmins(adminIds); return ResponseEntity.noContent().build(); } - - /** - * 역할 CRUD - */ - - @PostMapping("/roles") - public ResponseEntity> createRole(@RequestBody @Valid RoleCreateRequest request) { - final Long roleId = roleService.createRole(request); - final URI uri = ServletUriComponentsBuilder - .fromCurrentRequest() - .path("/{id}") - .buildAndExpand(roleId) - .toUri(); - return ResponseEntity.created(uri).body(ApiResponse.created(roleId)); - } - - @GetMapping("/roles") - public ResponseEntity>> getAllRoles() { - List roles = roleService.getAllRoles(); - return ResponseEntity.ok(ApiResponse.ok(roles)); - } - - @PatchMapping("/roles/{id}") - public ResponseEntity updateRole(@PathVariable Long id, @RequestBody @Valid RoleUpdateRequest request) { - roleService.updateRole(id, request); - return ResponseEntity.noContent().build(); - } - - @DeleteMapping("/roles/{id}") - public ResponseEntity deleteRole(@PathVariable Long id) { - roleService.deleteRole(id); - return ResponseEntity.noContent().build(); - } - - @PostMapping("/roles/{roleId}/permissions") - public ResponseEntity> addPermissionToRole(@PathVariable Long roleId, - @RequestParam Long permissionId) { - roleService.addPermissionToRole(roleId, permissionId); - return new ResponseEntity<>(ApiResponse.created(roleId), HttpStatus.CREATED); - } - - @DeleteMapping("/roles/{roleId}/permissions") - public ResponseEntity removePermissionsFromRole(@PathVariable Long roleId, @RequestParam Long permissionId) { - roleService.removePermissionFromRole(roleId, permissionId); - return ResponseEntity.noContent().build(); - } - - /** - * 권한 CRUD - */ - - @PostMapping("/permissions") - public ResponseEntity> createPermission(@RequestBody @Valid PermissionCreateRequest request) { - final Long permissionId = permissionService.createPermission(request); - final URI uri = ServletUriComponentsBuilder - .fromCurrentRequest() - .path("/{id}") - .buildAndExpand(permissionId) - .toUri(); - return ResponseEntity.created(uri).body(ApiResponse.created(permissionId)); - } - - @GetMapping("/permissions") - public ResponseEntity>> getAllPermissions() { - List permissions = permissionService.getAllPermissions(); - return ResponseEntity.ok(ApiResponse.ok(permissions)); - } - - @PatchMapping("/permissions/{id}") - public ResponseEntity updatePermission(@PathVariable Long id, - @RequestBody @Valid PermissionUpdateRequest request) { - permissionService.updatePermission(id, request); - return ResponseEntity.noContent().build(); - } - - @DeleteMapping("/permissions/{id}") - public ResponseEntity deletePermission(@PathVariable Long id) { - permissionService.deletePermission(id); - return ResponseEntity.noContent().build(); - } } diff --git a/api/api-member/src/main/java/com/pgms/apimember/dto/request/AdminCreateRequest.java b/api/api-member/src/main/java/com/pgms/apimember/dto/request/AdminCreateRequest.java index 42763e9a..6f14759f 100644 --- a/api/api-member/src/main/java/com/pgms/apimember/dto/request/AdminCreateRequest.java +++ b/api/api-member/src/main/java/com/pgms/apimember/dto/request/AdminCreateRequest.java @@ -1,7 +1,7 @@ package com.pgms.apimember.dto.request; import com.pgms.coredomain.domain.member.Admin; -import com.pgms.coredomain.domain.member.Role; +import com.pgms.coredomain.domain.member.enums.Role; import jakarta.validation.constraints.Email; import jakarta.validation.constraints.NotBlank; @@ -29,7 +29,7 @@ public record AdminCreateRequest( String email, @NotBlank(message = "역할은 필수 항목입니다.") - String roleName + Role role ) { public static Admin toEntity(AdminCreateRequest requestDto, String encodedPassword, Role role) { return Admin.builder() diff --git a/api/api-member/src/main/java/com/pgms/apimember/dto/request/AdminUpdateRequest.java b/api/api-member/src/main/java/com/pgms/apimember/dto/request/AdminUpdateRequest.java index a6ae1238..87249e89 100644 --- a/api/api-member/src/main/java/com/pgms/apimember/dto/request/AdminUpdateRequest.java +++ b/api/api-member/src/main/java/com/pgms/apimember/dto/request/AdminUpdateRequest.java @@ -1,6 +1,7 @@ package com.pgms.apimember.dto.request; import com.pgms.coredomain.domain.member.enums.AccountStatus; +import com.pgms.coredomain.domain.member.enums.Role; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.Pattern; @@ -26,6 +27,6 @@ public record AdminUpdateRequest( AccountStatus status, @NotBlank(message = "역할을 입력해주세요.") - String roleName + Role role ) { } diff --git a/api/api-member/src/main/java/com/pgms/apimember/dto/request/MemberSignUpRequest.java b/api/api-member/src/main/java/com/pgms/apimember/dto/request/MemberSignUpRequest.java index b8dca5d8..b55e0ee5 100644 --- a/api/api-member/src/main/java/com/pgms/apimember/dto/request/MemberSignUpRequest.java +++ b/api/api-member/src/main/java/com/pgms/apimember/dto/request/MemberSignUpRequest.java @@ -1,7 +1,6 @@ package com.pgms.apimember.dto.request; import com.pgms.coredomain.domain.member.Member; -import com.pgms.coredomain.domain.member.Role; import com.pgms.coredomain.domain.member.enums.Gender; import jakarta.validation.constraints.Email; @@ -42,7 +41,7 @@ public record MemberSignUpRequest( String zipCode ) { - public Member toEntity(String encodedPassword, Role role) { + public Member toEntity(String encodedPassword) { return Member.builder() .email(email) .password(encodedPassword) @@ -53,7 +52,6 @@ public Member toEntity(String encodedPassword, Role role) { .streetAddress(streetAddress) .detailAddress(detailAddress) .zipCode(zipCode) - .role(role) .build(); } } diff --git a/api/api-member/src/main/java/com/pgms/apimember/dto/request/PermissionCreateRequest.java b/api/api-member/src/main/java/com/pgms/apimember/dto/request/PermissionCreateRequest.java deleted file mode 100644 index 07230bb9..00000000 --- a/api/api-member/src/main/java/com/pgms/apimember/dto/request/PermissionCreateRequest.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.pgms.apimember.dto.request; - -import com.pgms.coredomain.domain.member.Permission; - -import jakarta.validation.constraints.NotBlank; -import jakarta.validation.constraints.Size; - -public record PermissionCreateRequest( - @NotBlank(message = "이름을 입력해주세요.") - @Size(min = 2, max = 20, message = "이름은 2자 이상 20자 이하로 입력해주세요.") - String name -) { - - public Permission toEntity() { - return new Permission(name); - } -} diff --git a/api/api-member/src/main/java/com/pgms/apimember/dto/request/PermissionUpdateRequest.java b/api/api-member/src/main/java/com/pgms/apimember/dto/request/PermissionUpdateRequest.java deleted file mode 100644 index 9a50cace..00000000 --- a/api/api-member/src/main/java/com/pgms/apimember/dto/request/PermissionUpdateRequest.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.pgms.apimember.dto.request; - -import jakarta.validation.constraints.NotBlank; -import jakarta.validation.constraints.Size; - -public record PermissionUpdateRequest( - @NotBlank(message = "이름을 입력해주세요.") - @Size(min = 2, max = 20, message = "이름은 2자 이상 20자 이하로 입력해주세요.") - String name -) { -} diff --git a/api/api-member/src/main/java/com/pgms/apimember/dto/request/RoleCreateRequest.java b/api/api-member/src/main/java/com/pgms/apimember/dto/request/RoleCreateRequest.java deleted file mode 100644 index b69da3bc..00000000 --- a/api/api-member/src/main/java/com/pgms/apimember/dto/request/RoleCreateRequest.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.pgms.apimember.dto.request; - -import com.pgms.coredomain.domain.member.Role; - -import jakarta.validation.constraints.NotBlank; -import jakarta.validation.constraints.Size; - -public record RoleCreateRequest( - @NotBlank(message = "이름을 입력해주세요.") - @Size(min = 2, max = 20, message = "이름은 2자 이상 20자 이하로 입력해주세요.") - String name -) { - - public Role toEntity() { - return new Role(name); - } -} diff --git a/api/api-member/src/main/java/com/pgms/apimember/dto/request/RoleUpdateRequest.java b/api/api-member/src/main/java/com/pgms/apimember/dto/request/RoleUpdateRequest.java deleted file mode 100644 index 93a9abcb..00000000 --- a/api/api-member/src/main/java/com/pgms/apimember/dto/request/RoleUpdateRequest.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.pgms.apimember.dto.request; - -import jakarta.validation.constraints.NotBlank; -import jakarta.validation.constraints.Size; - -public record RoleUpdateRequest( - @NotBlank(message = "이름을 입력해주세요.") - @Size(min = 2, max = 50, message = "이름은 2자 이상 50자 이하로 입력해주세요.") - String name -) { -} diff --git a/api/api-member/src/main/java/com/pgms/apimember/dto/response/AdminGetResponse.java b/api/api-member/src/main/java/com/pgms/apimember/dto/response/AdminGetResponse.java index d0ab7060..4617c309 100644 --- a/api/api-member/src/main/java/com/pgms/apimember/dto/response/AdminGetResponse.java +++ b/api/api-member/src/main/java/com/pgms/apimember/dto/response/AdminGetResponse.java @@ -3,8 +3,8 @@ import java.time.LocalDateTime; import com.pgms.coredomain.domain.member.Admin; -import com.pgms.coredomain.domain.member.Role; import com.pgms.coredomain.domain.member.enums.AccountStatus; +import com.pgms.coredomain.domain.member.enums.Role; public record AdminGetResponse( Long id, diff --git a/api/api-member/src/main/java/com/pgms/apimember/dto/response/MemberDetailGetResponse.java b/api/api-member/src/main/java/com/pgms/apimember/dto/response/MemberDetailGetResponse.java index 2d07bf12..93702ff0 100644 --- a/api/api-member/src/main/java/com/pgms/apimember/dto/response/MemberDetailGetResponse.java +++ b/api/api-member/src/main/java/com/pgms/apimember/dto/response/MemberDetailGetResponse.java @@ -3,10 +3,10 @@ import java.time.LocalDateTime; import com.pgms.coredomain.domain.member.Member; -import com.pgms.coredomain.domain.member.Role; import com.pgms.coredomain.domain.member.enums.AccountStatus; import com.pgms.coredomain.domain.member.enums.Gender; import com.pgms.coredomain.domain.member.enums.Provider; +import com.pgms.coredomain.domain.member.enums.Role; public record MemberDetailGetResponse( Long id, diff --git a/api/api-member/src/main/java/com/pgms/apimember/dto/response/PermissionGetResponse.java b/api/api-member/src/main/java/com/pgms/apimember/dto/response/PermissionGetResponse.java deleted file mode 100644 index 898f9874..00000000 --- a/api/api-member/src/main/java/com/pgms/apimember/dto/response/PermissionGetResponse.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.pgms.apimember.dto.response; - -import java.time.LocalDateTime; - -import com.pgms.coredomain.domain.member.Permission; - -public record PermissionGetResponse( - Long id, - String name, - LocalDateTime createdAt, - LocalDateTime updatedAt -) { - public static PermissionGetResponse from(Permission permission) { - return new PermissionGetResponse( - permission.getId(), - permission.getName(), - permission.getCreatedAt(), - permission.getUpdatedAt()); - } -} diff --git a/api/api-member/src/main/java/com/pgms/apimember/dto/response/RoleGetResponse.java b/api/api-member/src/main/java/com/pgms/apimember/dto/response/RoleGetResponse.java deleted file mode 100644 index ee48ad25..00000000 --- a/api/api-member/src/main/java/com/pgms/apimember/dto/response/RoleGetResponse.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.pgms.apimember.dto.response; - -import java.time.LocalDateTime; - -import com.pgms.coredomain.domain.member.Role; - -public record RoleGetResponse( - Long id, - String name, - LocalDateTime createdAt, - LocalDateTime updatedAt -) { - public static RoleGetResponse from(Role role) { - return new RoleGetResponse(role.getId(), role.getName(), role.getCreatedAt(), role.getUpdatedAt()); - } -} diff --git a/api/api-member/src/main/java/com/pgms/apimember/service/AdminService.java b/api/api-member/src/main/java/com/pgms/apimember/service/AdminService.java index 3cb2e4d2..de3662c2 100644 --- a/api/api-member/src/main/java/com/pgms/apimember/service/AdminService.java +++ b/api/api-member/src/main/java/com/pgms/apimember/service/AdminService.java @@ -18,10 +18,8 @@ import com.pgms.apimember.dto.response.MemberSummaryGetResponse; import com.pgms.apimember.exception.AdminException; import com.pgms.coredomain.domain.member.Admin; -import com.pgms.coredomain.domain.member.Role; import com.pgms.coredomain.domain.member.repository.AdminRepository; import com.pgms.coredomain.domain.member.repository.MemberRepository; -import com.pgms.coredomain.domain.member.repository.RoleRepository; import lombok.RequiredArgsConstructor; @@ -32,7 +30,6 @@ public class AdminService { private final AdminRepository adminRepository; private final MemberRepository memberRepository; - private final RoleRepository roleRepository; private final PasswordEncoder passwordEncoder; // 슈퍼 관리자 기능 @@ -45,7 +42,7 @@ public Long createAdmin(AdminCreateRequest requestDto) { final Admin admin = AdminCreateRequest.toEntity( requestDto, passwordEncoder.encode(requestDto.password()), - getRole(requestDto.roleName()) + requestDto.role() ); return adminRepository.save(admin).getId(); } @@ -59,15 +56,14 @@ public List getAdmins(PageCondition pageCondition) { public void updateAdmin(Long adminId, AdminUpdateRequest requestDto) { validatePasswordAndConfirm(requestDto.password(), requestDto.passwordConfirm()); - final Admin admin = adminRepository.findByIdWithRole(adminId) + final Admin admin = adminRepository.findById(adminId) .orElseThrow(() -> new AdminException(ADMIN_NOT_FOUND)); - final Role role = getRole(requestDto.roleName()); admin.update( requestDto.name(), passwordEncoder.encode(requestDto.password()), requestDto.phoneNumber(), - requestDto.status(), role + requestDto.status(), requestDto.role() ); } @@ -85,7 +81,7 @@ public List getMemberDetails(List memberIds) { @Transactional(readOnly = true) public AdminGetResponse getAdmin(Long adminId) { - final Admin admin = adminRepository.findByIdWithRole(adminId) + final Admin admin = adminRepository.findById(adminId) .orElseThrow(() -> new AdminException(ADMIN_NOT_FOUND)); return AdminGetResponse.from(admin); } @@ -108,10 +104,4 @@ private void validatePasswordAndConfirm(String password, String passwordConfirm) throw new AdminException(PASSWORD_CONFIRM_NOT_MATCHED); } } - - private Role getRole(String roleName) { - return roleRepository.findByName(roleName) - .orElseThrow(() -> new AdminException(ADMIN_ROLE_NOT_FOUND)); - } - } diff --git a/api/api-member/src/main/java/com/pgms/apimember/service/AuthService.java b/api/api-member/src/main/java/com/pgms/apimember/service/AuthService.java index 1e8f51b1..67e55f42 100644 --- a/api/api-member/src/main/java/com/pgms/apimember/service/AuthService.java +++ b/api/api-member/src/main/java/com/pgms/apimember/service/AuthService.java @@ -70,7 +70,7 @@ public AuthResponse refresh(RefreshTokenRequest request) { refreshTokenRepository.delete(refreshToken); refreshTokenRepository.save(new RefreshToken(newRefreshToken, newAccessToken, refreshToken.getAccountType(), refreshToken.getEmail())); - return new AuthResponse(newAccessToken, refreshToken.getRefreshToken()); + return new AuthResponse(newAccessToken, newRefreshToken); } private UserDetailsImpl loadUserDetails(String accountType, String email) { diff --git a/api/api-member/src/main/java/com/pgms/apimember/service/MemberService.java b/api/api-member/src/main/java/com/pgms/apimember/service/MemberService.java index 4c93327e..d5d9f37c 100644 --- a/api/api-member/src/main/java/com/pgms/apimember/service/MemberService.java +++ b/api/api-member/src/main/java/com/pgms/apimember/service/MemberService.java @@ -10,13 +10,10 @@ import com.pgms.apimember.dto.request.MemberPasswordUpdateRequest; import com.pgms.apimember.dto.request.MemberSignUpRequest; import com.pgms.apimember.dto.response.MemberDetailGetResponse; -import com.pgms.apimember.exception.AdminException; import com.pgms.apimember.exception.CustomErrorCode; import com.pgms.apimember.exception.MemberException; import com.pgms.coredomain.domain.member.Member; -import com.pgms.coredomain.domain.member.Role; import com.pgms.coredomain.domain.member.repository.MemberRepository; -import com.pgms.coredomain.domain.member.repository.RoleRepository; import lombok.RequiredArgsConstructor; @@ -29,7 +26,6 @@ public class MemberService { private static final String DEFAULT_ROLE_NAME = "ROLE_USER"; private final MemberRepository memberRepository; - private final RoleRepository roleRepository; private final PasswordEncoder passwordEncoder; public Long signUp(MemberSignUpRequest requestDto) { @@ -37,18 +33,13 @@ public Long signUp(MemberSignUpRequest requestDto) { throw new MemberException(DUPLICATED_MEMBER_EMAIL); } validateNewPassword(requestDto.password(), requestDto.passwordConfirm()); - return memberRepository.save( - requestDto.toEntity( - passwordEncoder.encode(requestDto.password()), - getRole(DEFAULT_ROLE_NAME)) - ) - .getId(); + return memberRepository.save(requestDto.toEntity(passwordEncoder.encode(requestDto.password()))).getId(); } @Transactional(readOnly = true) public MemberDetailGetResponse getMemberDetail(Long memberId) { return MemberDetailGetResponse.from( - memberRepository.findByIdWithRole(memberId). + memberRepository.findById(memberId). orElseThrow(() -> new MemberException(CustomErrorCode.MEMBER_NOT_FOUND))); } @@ -106,9 +97,4 @@ private Member getAvailableMember(Long memberId) { return memberRepository.findByIdAndIsDeletedFalse(memberId) .orElseThrow(() -> new MemberException(MEMBER_NOT_FOUND)); } - - private Role getRole(String roleName) { - return roleRepository.findByName(roleName) - .orElseThrow(() -> new AdminException(ADMIN_ROLE_NOT_FOUND)); - } } diff --git a/api/api-member/src/main/java/com/pgms/apimember/service/PermissionService.java b/api/api-member/src/main/java/com/pgms/apimember/service/PermissionService.java deleted file mode 100644 index d3174559..00000000 --- a/api/api-member/src/main/java/com/pgms/apimember/service/PermissionService.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.pgms.apimember.service; - -import static com.pgms.apimember.exception.CustomErrorCode.*; - -import java.util.List; - -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -import com.pgms.apimember.dto.request.PermissionCreateRequest; -import com.pgms.apimember.dto.request.PermissionUpdateRequest; -import com.pgms.apimember.dto.response.PermissionGetResponse; -import com.pgms.apimember.exception.AdminException; -import com.pgms.coredomain.domain.member.Permission; -import com.pgms.coredomain.domain.member.repository.PermissionRepository; -import com.pgms.coredomain.domain.member.repository.RolePermissionRepository; - -import lombok.RequiredArgsConstructor; - -@Service -@Transactional -@RequiredArgsConstructor -public class PermissionService { - - private final PermissionRepository permissionRepository; - private final RolePermissionRepository rolePermissionRepository; - - public Long createPermission(PermissionCreateRequest request) { - validatePermissionNameUnique(request.name()); - Permission permission = permissionRepository.save(request.toEntity()); - return permission.getId(); - } - - @Transactional(readOnly = true) - public List getAllPermissions() { - return permissionRepository.findAll().stream() - .map(PermissionGetResponse::from) - .toList(); - } - - public void updatePermission(Long id, PermissionUpdateRequest request) { - validatePermissionNameUnique(request.name(), id); - Permission permission = permissionRepository.findById(id) - .orElseThrow(() -> new AdminException(ADMIN_PERMISSION_NOT_FOUND)); - permission.updateName(request.name()); - } - - public void deletePermission(Long id) { - if (rolePermissionRepository.countByPermissionId(id) > 0) { - throw new AdminException(PERMISSION_ASSIGNED); - } - permissionRepository.deleteById(id); - } - - private void validatePermissionNameUnique(String name) { - permissionRepository.findByName(name).ifPresent(r -> { - throw new AdminException(DUPLICATED_PERMISSION); - }); - } - - private void validatePermissionNameUnique(String name, Long id) { - // 수정하려는 엔티티 자신의 id는 제외하고 중복 검사 - permissionRepository.findByNameAndIdNot(name, id).ifPresent(r -> { - throw new AdminException(DUPLICATED_PERMISSION); - }); - } -} diff --git a/api/api-member/src/main/java/com/pgms/apimember/service/RoleService.java b/api/api-member/src/main/java/com/pgms/apimember/service/RoleService.java deleted file mode 100644 index 56aee096..00000000 --- a/api/api-member/src/main/java/com/pgms/apimember/service/RoleService.java +++ /dev/null @@ -1,104 +0,0 @@ -package com.pgms.apimember.service; - -import static com.pgms.apimember.exception.CustomErrorCode.*; - -import java.util.List; - -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -import com.pgms.apimember.dto.request.RoleCreateRequest; -import com.pgms.apimember.dto.request.RoleUpdateRequest; -import com.pgms.apimember.dto.response.RoleGetResponse; -import com.pgms.apimember.exception.AdminException; -import com.pgms.coredomain.domain.member.Permission; -import com.pgms.coredomain.domain.member.Role; -import com.pgms.coredomain.domain.member.RolePermission; -import com.pgms.coredomain.domain.member.repository.AdminRepository; -import com.pgms.coredomain.domain.member.repository.MemberRepository; -import com.pgms.coredomain.domain.member.repository.PermissionRepository; -import com.pgms.coredomain.domain.member.repository.RoleRepository; - -import lombok.RequiredArgsConstructor; - -@Service -@Transactional -@RequiredArgsConstructor -public class RoleService { - - private final RoleRepository roleRepository; - private final PermissionRepository permissionRepository; - private final AdminRepository adminRepository; - private final MemberRepository memberRepository; - - public Long createRole(RoleCreateRequest request) { - validateRoleNameUnique(request.name()); - Role role = roleRepository.save(request.toEntity()); - return role.getId(); - } - - @Transactional(readOnly = true) - public List getAllRoles() { - return roleRepository.findAll().stream() - .map(RoleGetResponse::from) - .toList(); - } - - public void updateRole(Long id, RoleUpdateRequest request) { - Role role = roleRepository.findById(id) - .orElseThrow(() -> new AdminException(ADMIN_ROLE_NOT_FOUND)); - validateRoleNameUnique(request.name(), role.getId()); - role.updateName(request.name()); - } - - public void deleteRole(Long id) { - Role role = roleRepository.findById(id) - .orElseThrow(() -> new AdminException(ADMIN_ROLE_NOT_FOUND)); - - if (isRoleInUse(id)) { - throw new AdminException(ROLE_IN_USE); - } - - roleRepository.delete(role); - } - - private boolean isRoleInUse(Long roleId) { - return adminRepository.existsByRoleId(roleId) || memberRepository.existsByRoleId(roleId); - } - - private void validateRoleNameUnique(String name) { - roleRepository.findByName(name).ifPresent(r -> { - throw new AdminException(DUPLICATED_ROLE); - }); - } - - private void validateRoleNameUnique(String name, Long id) { - // 수정하려는 엔티티 자신의 id는 제외하고 중복 검사 - roleRepository.findByNameAndIdNot(name, id).ifPresent(r -> { - throw new AdminException(DUPLICATED_ROLE); - }); - } - - public void addPermissionToRole(Long roleId, Long permissionId) { - Role role = roleRepository.findById(roleId) - .orElseThrow(() -> new AdminException(ADMIN_ROLE_NOT_FOUND)); - Permission permission = permissionRepository.findById(permissionId) - .orElseThrow(() -> new AdminException(ADMIN_PERMISSION_NOT_FOUND)); - - role.addPermissionToRole(new RolePermission(role, permission)); - } - - public void removePermissionFromRole(Long roleId, Long permissionId) { - Role role = roleRepository.findById(roleId) - .orElseThrow(() -> new AdminException(ADMIN_ROLE_NOT_FOUND)); - Permission permission = permissionRepository.findById(permissionId) - .orElseThrow(() -> new AdminException(ADMIN_PERMISSION_NOT_FOUND)); - - RolePermission rolePermission = role.getRolePermissions().stream() - .filter(rp -> rp.getPermission().getId().equals(permission.getId())) - .findFirst() - .orElseThrow(() -> new AdminException(ROLE_PERMISSION_NOT_FOUND)); - - role.removePermissionFromRole(rolePermission); - } -} diff --git a/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/Admin.java b/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/Admin.java index 102350b7..561e3c23 100644 --- a/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/Admin.java +++ b/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/Admin.java @@ -3,16 +3,15 @@ import static lombok.AccessLevel.*; import com.pgms.coredomain.domain.member.enums.AccountStatus; +import com.pgms.coredomain.domain.member.enums.Role; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.EnumType; import jakarta.persistence.Enumerated; -import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; -import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; import lombok.Builder; import lombok.Getter; @@ -45,7 +44,8 @@ public class Admin extends AccountBaseEntity { @Enumerated(EnumType.STRING) private AccountStatus status; - @ManyToOne(fetch = FetchType.LAZY) + @Enumerated(EnumType.STRING) + @Column(name = "role") private Role role; @Builder diff --git a/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/Member.java b/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/Member.java index 67cbd8fe..c146a955 100644 --- a/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/Member.java +++ b/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/Member.java @@ -5,17 +5,15 @@ import com.pgms.coredomain.domain.member.enums.AccountStatus; import com.pgms.coredomain.domain.member.enums.Gender; import com.pgms.coredomain.domain.member.enums.Provider; +import com.pgms.coredomain.domain.member.enums.Role; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.EnumType; import jakarta.persistence.Enumerated; -import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; -import jakarta.persistence.JoinColumn; -import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; import lombok.Builder; import lombok.Getter; @@ -68,8 +66,8 @@ public class Member extends AccountBaseEntity { @Enumerated(EnumType.STRING) private Provider provider; - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "role_id") + @Enumerated(EnumType.STRING) + @Column(name = "role") private Role role; @Builder @@ -83,8 +81,7 @@ public Member( String streetAddress, String detailAddress, String zipCode, - Provider provider, - Role role + Provider provider ) { this.email = email; this.password = password; @@ -96,7 +93,7 @@ public Member( this.detailAddress = detailAddress; this.zipCode = zipCode; this.provider = provider; - this.role = role; + this.role = Role.ROLE_USER; this.status = ACTIVE; } diff --git a/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/Permission.java b/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/Permission.java deleted file mode 100644 index 3858cf65..00000000 --- a/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/Permission.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.pgms.coredomain.domain.member; - -import com.pgms.coredomain.domain.common.BaseEntity; - -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import jakarta.persistence.Table; -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@Entity -@Table(name = "permission") -@Getter -@NoArgsConstructor(access = AccessLevel.PROTECTED) -public class Permission extends BaseEntity { - - @Id - @Column(name = "id", nullable = false) - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - @Column(name = "name", nullable = false) - private String name; - - public Permission(String name) { - this.name = name; - } - - public void updateName(String name) { - this.name = name; - } -} diff --git a/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/Role.java b/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/Role.java deleted file mode 100644 index e9e4ff8e..00000000 --- a/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/Role.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.pgms.coredomain.domain.member; - -import java.util.ArrayList; -import java.util.List; - -import com.pgms.coredomain.domain.common.BaseEntity; - -import jakarta.persistence.CascadeType; -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import jakarta.persistence.OneToMany; -import jakarta.persistence.Table; -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@Entity -@Table(name = "role") -@Getter -@NoArgsConstructor(access = AccessLevel.PROTECTED) -public class Role extends BaseEntity { - - @Id - @Column(name = "id", nullable = false) - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - @Column(name = "name", nullable = false) - private String name; - - @OneToMany(mappedBy = "role", cascade = CascadeType.REMOVE) - private List rolePermissions = new ArrayList<>(); - - public Role(String name) { - this.name = name; - } - - public void updateName(String name) { - this.name = name; - } - - public void addPermissionToRole(RolePermission rolePermission) { - if (!rolePermissions.contains(rolePermission)) { - rolePermissions.add(rolePermission); - } - } - - public void removePermissionFromRole(RolePermission rolePermission) { - rolePermissions.remove(rolePermission); - } -} diff --git a/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/RolePermission.java b/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/RolePermission.java deleted file mode 100644 index 247bf343..00000000 --- a/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/RolePermission.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.pgms.coredomain.domain.member; - -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.FetchType; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import jakarta.persistence.JoinColumn; -import jakarta.persistence.ManyToOne; -import jakarta.persistence.Table; -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@Entity -@Table(name = "role_permission") -@Getter -@NoArgsConstructor(access = AccessLevel.PROTECTED) -public class RolePermission { - - @Id - @Column(name = "id", nullable = false) - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - @ManyToOne(fetch = FetchType.LAZY, optional = false) - @JoinColumn(name = "role_id", nullable = false) - private Role role; - - @ManyToOne(fetch = FetchType.LAZY, optional = false) - @JoinColumn(name = "permission_id", nullable = false) - private Permission permission; - - public RolePermission(Role role, Permission permission) { - this.role = role; - this.permission = permission; - } -} diff --git a/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/enums/Role.java b/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/enums/Role.java new file mode 100644 index 00000000..6637645b --- /dev/null +++ b/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/enums/Role.java @@ -0,0 +1,7 @@ +package com.pgms.coredomain.domain.member.enums; + +public enum Role { + ROLE_SUPERADMIN, + ROLE_ADMIN, + ROLE_USER +} diff --git a/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/repository/AdminRepository.java b/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/repository/AdminRepository.java index 4b2d2de0..7498b805 100644 --- a/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/repository/AdminRepository.java +++ b/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/repository/AdminRepository.java @@ -6,7 +6,6 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; -import org.springframework.data.jpa.repository.EntityGraph; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -16,14 +15,8 @@ public interface AdminRepository extends JpaRepository { boolean existsByEmail(String email); - boolean existsByRoleId(Long roleId); - - @EntityGraph(attributePaths = {"role"}) Slice findSliceBy(Pageable pageable); - @Query("select a from Admin a join fetch a.role r where a.id = :id") - Optional findByIdWithRole(@Param("id") Long id); - Optional findByEmail(String email); @Query("select a from Admin a where a.lastLoginAt < :dateTime and a.status = 'ACTIVE'") diff --git a/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/repository/MemberRepository.java b/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/repository/MemberRepository.java index 8a0bb81c..bf57e3c1 100644 --- a/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/repository/MemberRepository.java +++ b/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/repository/MemberRepository.java @@ -4,7 +4,6 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; -import org.springframework.data.jpa.repository.EntityGraph; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -14,22 +13,13 @@ public interface MemberRepository extends JpaRepository { boolean existsByEmail(String email); - boolean existsByRoleId(Long roleId); - - @EntityGraph(attributePaths = {"role"}) Slice findSliceBy(Pageable pageable); Optional findByEmail(String email); - @Query("select m from Member m join fetch m.role r where m.email = :email") - Optional findByEmailWithRole(@Param("email") String email); - - @Query("select m from Member m join fetch m.role r where m.id = :id") - Optional findByIdWithRole(@Param("id") Long id); - - @Query("select m from Member m join fetch m.role r where m.id = :id and m.status = 'DELETED'") + @Query("select m from Member m where m.id = :id and m.status = 'DELETED'") Optional findByIdAndIsDeletedTrue(@Param("id") Long id); - @Query("select m from Member m join fetch m.role r where m.id = :id and m.status != 'DELETED'") + @Query("select m from Member m where m.id = :id and m.status != 'DELETED'") Optional findByIdAndIsDeletedFalse(@Param("id") Long id); } diff --git a/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/repository/PermissionRepository.java b/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/repository/PermissionRepository.java deleted file mode 100644 index 5b09af9c..00000000 --- a/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/repository/PermissionRepository.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.pgms.coredomain.domain.member.repository; - -import java.util.Optional; - -import org.springframework.data.jpa.repository.JpaRepository; - -import com.pgms.coredomain.domain.member.Permission; - -public interface PermissionRepository extends JpaRepository { - Optional findByName(String name); - - Optional findByNameAndIdNot(String name, Long id); -} diff --git a/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/repository/RolePermissionRepository.java b/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/repository/RolePermissionRepository.java deleted file mode 100644 index 0e4c8baf..00000000 --- a/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/repository/RolePermissionRepository.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.pgms.coredomain.domain.member.repository; - -import org.springframework.data.jpa.repository.JpaRepository; - -import com.pgms.coredomain.domain.member.RolePermission; - -public interface RolePermissionRepository extends JpaRepository { - int countByPermissionId(Long id); -} diff --git a/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/repository/RoleRepository.java b/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/repository/RoleRepository.java deleted file mode 100644 index de87c0fd..00000000 --- a/core/core-domain/src/main/java/com/pgms/coredomain/domain/member/repository/RoleRepository.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.pgms.coredomain.domain.member.repository; - -import java.util.Optional; - -import org.springframework.data.jpa.repository.JpaRepository; - -import com.pgms.coredomain.domain.member.Role; - -public interface RoleRepository extends JpaRepository { - Optional findByName(String name); - - Optional findByNameAndIdNot(String name, Long id); -} diff --git a/core/core-infra/src/main/resources/data.sql b/core/core-infra/src/main/resources/data.sql index edfa7799..02854968 100644 --- a/core/core-infra/src/main/resources/data.sql +++ b/core/core-infra/src/main/resources/data.sql @@ -38,31 +38,23 @@ VALUES ('E3', 'BEING_BOOKED', 2, 1); INSERT INTO event_review (score, content, event_id) VALUES (5, 'Great event!', 1); --- Role -INSERT INTO role (name) -VALUES ('ROLE_SUPERADMIN'), - ('ROLE_ADMIN'), - ('ROLE_USER'), - ('ROLE_GUEST'); - - -- Member INSERT INTO member (name, password, phone_number, email, birth_date, gender, street_address, detail_address, zip_code, - status, provider, role_id, last_login_at, last_password_updated_at) + status, provider, role, last_login_at, last_password_updated_at) VALUES ('김빙봉', '$2a$10$tfdM.PjviEH0zMEXVYjH.ODJPSviQRrYpb17rdMjvJtbWbSnC8nTa', '01012345678', 'john.doe@example.com', '1990-01-01', 'MALE', '123 Main St', 'Apt 101', - '12345', 'ACTIVE', 'KAKAO', 3, '2024-01-01T10:00:00', '2024-01-01T10:00:00'), + '12345', 'ACTIVE', 'KAKAO', 'ROLE_USER', '2024-01-01T10:00:00', '2024-01-01T10:00:00'), ('이행복', '$2a$10$tfdM.PjviEH0zMEXVYjH.ODJPSviQRrYpb17rdMjvJtbWbSnC8nTa', '01087654321', 'jane.smith@example.com', '1992-02-02', 'FEMALE', '456 Elm St', - 'Suite 202', '54321', 'ACTIVE', 'KAKAO', 3, '2024-01-01T10:00:00', '2024-01-01T10:00:00'); + 'Suite 202', '54321', 'ACTIVE', 'KAKAO', 'ROLE_USER', '2024-01-01T10:00:00', '2024-01-01T10:00:00'); -- Admin -INSERT INTO admin (name, email, password, phone_number, status, role_id, last_login_at, last_password_updated_at) +INSERT INTO admin (name, email, password, phone_number, status, role, last_login_at, last_password_updated_at) VALUES ('슈퍼관리자', 'superadmin@example.com', '$2a$10$tfdM.PjviEH0zMEXVYjH.ODJPSviQRrYpb17rdMjvJtbWbSnC8nTa', - '01012345678', 'ACTIVE', 1, NOW(), NOW()), + '01012345678', 'ACTIVE', 'ROLE_SUPERADMIN', NOW(), NOW()), ('관리자', 'admin@example.com', '$2a$10$tfdM.PjviEH0zMEXVYjH.ODJPSviQRrYpb17rdMjvJtbWbSnC8nTa', '01012345678', - 'ACTIVE', 2, NOW(), NOW()), + 'ACTIVE', 'ROLE_ADMIN', NOW(), NOW()), ('오래된 슈퍼관리자', 'oldSuperadmin@example.com', '$2a$10$tfdM.PjviEH0zMEXVYjH.ODJPSviQRrYpb17rdMjvJtbWbSnC8nTa', - '01012345678', 'ACTIVE', 1, '2020-01-01', NOW()); + '01012345678', 'ACTIVE', 'ROLE_SUPERADMIN', '2020-01-01', NOW()); diff --git a/core/core-security/src/main/java/com/pgms/coresecurity/security/service/OAuth2AuthenticationSuccessHandler.java b/core/core-security/src/main/java/com/pgms/coresecurity/security/service/OAuth2AuthenticationSuccessHandler.java index 9eb05eac..457093ae 100644 --- a/core/core-security/src/main/java/com/pgms/coresecurity/security/service/OAuth2AuthenticationSuccessHandler.java +++ b/core/core-security/src/main/java/com/pgms/coresecurity/security/service/OAuth2AuthenticationSuccessHandler.java @@ -13,10 +13,8 @@ import org.springframework.stereotype.Component; import com.pgms.coredomain.domain.member.Member; -import com.pgms.coredomain.domain.member.Role; import com.pgms.coredomain.domain.member.enums.Provider; import com.pgms.coredomain.domain.member.repository.MemberRepository; -import com.pgms.coredomain.domain.member.repository.RoleRepository; import com.pgms.coresecurity.security.jwt.JwtTokenProvider; import com.pgms.coresecurity.security.util.HttpResponseUtil; @@ -32,7 +30,6 @@ public class OAuth2AuthenticationSuccessHandler implements AuthenticationSuccessHandler { private final MemberRepository memberRepository; - private final RoleRepository roleRepository; private final JwtTokenProvider jwtTokenProvider; @Override @@ -58,18 +55,18 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo // 토큰 생성 후 반환 Map body = new HashMap<>(); body.put("accessToken", jwtTokenProvider.generateAccessToken((UserDetailsImpl)authenticated.getPrincipal())); + body.put("refreshToken", jwtTokenProvider.generateRefreshToken()); + + // TODO redis에 토큰 정보 저장 HttpResponseUtil.setSuccessResponse(response, HttpStatus.OK, body); } private Member createOrUpdateMember(String email, String name) { - return memberRepository.findByEmailWithRole(email).orElseGet(() -> { - Role defaultRole = roleRepository.findByName("ROLE_USER") //TODO: Role Enum으로 변경 - .orElseThrow(() -> new RuntimeException("ROLE_USER가 존재하지 않습니다.")); + return memberRepository.findByEmail(email).orElseGet(() -> { Member newMember = Member.builder() .email(email) .name(name) .provider(Provider.KAKAO) - .role(defaultRole) .build(); return memberRepository.save(newMember); }); diff --git a/core/core-security/src/main/java/com/pgms/coresecurity/security/service/UserDetailsImpl.java b/core/core-security/src/main/java/com/pgms/coresecurity/security/service/UserDetailsImpl.java index fd84bff0..dbab4700 100644 --- a/core/core-security/src/main/java/com/pgms/coresecurity/security/service/UserDetailsImpl.java +++ b/core/core-security/src/main/java/com/pgms/coresecurity/security/service/UserDetailsImpl.java @@ -25,12 +25,12 @@ public class UserDetailsImpl implements UserDetails { private Collection authorities; public static UserDetails from(Admin admin) { - List authorities = List.of(new SimpleGrantedAuthority(admin.getRole().getName())); + List authorities = List.of(new SimpleGrantedAuthority(admin.getRole().name())); return new UserDetailsImpl(admin.getId(), admin.getEmail(), admin.getPassword(), authorities); } public static UserDetails from(Member member) { - List authorities = List.of(new SimpleGrantedAuthority(member.getRole().getName())); + List authorities = List.of(new SimpleGrantedAuthority(member.getRole().name())); return new UserDetailsImpl(member.getId(), member.getEmail(), member.getPassword(), authorities); }