Skip to content

Commit 48e7866

Browse files
committed
fix(user-admin): 사용자 목록 정렬 및 권한별 접근 로직 개선
1 parent 3af9d71 commit 48e7866

File tree

3 files changed

+19
-26
lines changed

3 files changed

+19
-26
lines changed

src/main/java/inha/gdgoc/domain/user/controller/UserAdminController.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,14 @@ public class UserAdminController {
3232
@GetMapping
3333
public ResponseEntity<ApiResponse<Page<UserSummaryResponse>, PageMeta>> list(
3434
@RequestParam(required = false) String q,
35-
@RequestParam(required = false) UserRole role,
36-
@RequestParam(required = false) TeamType team,
3735
@RequestParam(defaultValue = "0") int page,
3836
@RequestParam(defaultValue = "20") int size,
3937
@RequestParam(defaultValue = "name") String sort,
4038
@RequestParam(defaultValue = "ASC") String dir
4139
) {
4240
Sort.Direction direction = "ASC".equalsIgnoreCase(dir) ? Sort.Direction.ASC : Sort.Direction.DESC;
4341
Pageable pageable = PageRequest.of(page, size, Sort.by(direction, sort));
44-
45-
Page<UserSummaryResponse> result = userAdminService.listUsers(q, role, team, pageable);
42+
Page<UserSummaryResponse> result = userAdminService.listUsers(q, pageable);
4643
return ResponseEntity.ok(ApiResponse.ok("USER_SUMMARY_LIST_RETRIEVED", result, PageMeta.of(result)));
4744
}
4845

src/main/java/inha/gdgoc/domain/user/repository/UserRepository.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
public interface UserRepository extends JpaRepository<User, Long>, UserRepositoryCustom {
2121

2222
boolean existsByNameAndEmail(String name, String email);
23-
2423
boolean existsByEmail(String email);
2524

2625
/* ===== 출석/팀 뷰용 기본 쿼리 ===== */
@@ -38,22 +37,13 @@ public interface UserRepository extends JpaRepository<User, Long>, UserRepositor
3837
List<User> findByTeam(TeamType team);
3938

4039
@Query("""
41-
select new inha.gdgoc.domain.user.dto.response.UserSummaryResponse(
42-
u.id, u.name, u.major, u.studentId, u.email, u.userRole, u.team
43-
)
44-
from User u
45-
where
46-
(
47-
:q is null or :q = '' or
48-
lower(u.name) like lower(concat('%', :q, '%')) or
49-
lower(u.email) like lower(concat('%', :q, '%')) or
50-
u.studentId like concat('%', :q, '%') or
51-
lower(u.major) like lower(concat('%', :q, '%'))
52-
)
53-
and (:role is null or u.userRole = :role)
54-
and (:team is null or u.team = :team)
55-
""")
56-
Page<UserSummaryResponse> findSummaries(@Param("q") String q, @Param("role") inha.gdgoc.domain.user.enums.UserRole role, @Param("team") inha.gdgoc.domain.user.enums.TeamType team, Pageable pageable);
40+
select new inha.gdgoc.domain.user.dto.response.UserSummaryResponse(
41+
u.id, u.name, u.major, u.studentId, u.email, u.userRole, u.team
42+
)
43+
from User u
44+
where (:q is null or :q = '' or u.name like concat('%', :q, '%'))
45+
""")
46+
Page<UserSummaryResponse> findSummaries(@Param("q") String q, Pageable pageable);
5747

5848
@NotNull Optional<User> findById(@NotNull Long id);
5949
}

src/main/java/inha/gdgoc/domain/user/service/UserAdminService.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ public class UserAdminService {
2929
/* ======================= 목록 ======================= */
3030

3131
@Transactional(readOnly = true)
32-
public Page<UserSummaryResponse> listUsers(String q, UserRole role, TeamType team, Pageable pageable) {
32+
public Page<UserSummaryResponse> listUsers(String q, Pageable pageable) {
3333
Pageable fixed = rewriteSort(pageable);
34-
// 레포지토리에 role/team 조건 추가한 메서드가 있어야 함
35-
return userRepository.findSummaries(q, role, team, fixed);
34+
return userRepository.findSummaries(q, fixed);
3635
}
3736

3837
private Pageable rewriteSort(Pageable pageable) {
@@ -48,14 +47,21 @@ private Pageable rewriteSort(Pageable pageable) {
4847

4948
if ("userRole".equals(prop)) {
5049
hasUserRoleOrder = true;
51-
String roleRankCase = "CASE u.userRole " + "WHEN 'GUEST' THEN 0 " + "WHEN 'MEMBER' THEN 1 " + "WHEN 'CORE' THEN 2 " + "WHEN 'LEAD' THEN 3 " + "WHEN 'ORGANIZER' THEN 4 " + "WHEN 'ADMIN' THEN 5 " + "ELSE -1 END";
50+
String roleRankCase =
51+
"(CASE " +
52+
" WHEN u.userRole = 'GUEST' THEN 0 " +
53+
" WHEN u.userRole = 'MEMBER' THEN 1 " +
54+
" WHEN u.userRole = 'CORE' THEN 2 " +
55+
" WHEN u.userRole = 'LEAD' THEN 3 " +
56+
" WHEN u.userRole = 'ORGANIZER' THEN 4 " +
57+
" WHEN u.userRole = 'ADMIN' THEN 5 " +
58+
" ELSE -1 END)";
5259
composed = composed.and(JpaSort.unsafe(dir, roleRankCase));
5360
} else {
5461
composed = composed.and(Sort.by(new Sort.Order(dir, prop)));
5562
}
5663
}
5764

58-
// ROLE 정렬이 있으면 같은 권한 내 name ASC로 안정화
5965
if (hasUserRoleOrder) {
6066
composed = composed.and(Sort.by("name").ascending());
6167
}

0 commit comments

Comments
 (0)