Skip to content

Commit

Permalink
Merge pull request #72 from KNU-HAEDAL-Website/feat-create-board-user…
Browse files Browse the repository at this point in the history
…-role-issue-51

Feat: 게시판 생성시 유저 목록 조회 관련 권한 변경
  • Loading branch information
tfer2442 authored Aug 10, 2024
2 parents 84ccad2 + ac5e582 commit 38798d2
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
.requestMatchers(HttpMethod.GET, "/posts/generate-presigned-url").hasAnyRole("WEB_MASTER", "ADMIN", "TEAM_LEADER", "MEMBER")
.requestMatchers(HttpMethod.POST, "/notice/posts", "/event/posts").hasAnyRole("WEB_MASTER", "ADMIN")
.requestMatchers(HttpMethod.POST, "/activities/{activityId}/boards").hasAnyRole("WEB_MASTER", "ADMIN", "TEAM_LEADER")
.requestMatchers(HttpMethod.POST, "/boards/{boardId}/posts").hasAnyRole("WEB_MASTER", "ADMIN", "TEAM_LEADER", "MEMBER")
.requestMatchers(HttpMethod.POST, "/boards/{boardId}/posts").authenticated()
.requestMatchers(HttpMethod.DELETE, "/activities/{activityId}/boards/{boardId}").hasAnyRole("WEB_MASTER", "ADMIN", "TEAM_LEADER")
.requestMatchers(HttpMethod.PATCH, "/activities/{activityId}/boards/{boardId}/**").hasAnyRole("WEB_MASTER", "ADMIN", "TEAM_LEADER")
.requestMatchers("/private/users").authenticated()
.requestMatchers("/activities/{activityId}/boards","/activities/{activityId}/boards/{boardId}","/login", "/", "/join/**", "/reissue", "/swagger-ui/**", "/v3/api-docs/**", "/users/**","/semesters/**").permitAll()
.anyRequest().authenticated());

Expand Down
66 changes: 40 additions & 26 deletions src/main/java/com/haedal/haedalweb/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
//package com.haedal.haedalweb.controller;
package com.haedal.haedalweb.controller;

import com.haedal.haedalweb.dto.response.user.PrivateUserDTO;
import com.haedal.haedalweb.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "유저 API")
@RequiredArgsConstructor
@RestController
public class UserController {
private final UserService userService;
// @Operation(summary = "User 목록")
// @Parameters({
// @Parameter(name = "page", description = "현재 페이지"),
// @Parameter(name = "size", description = "한 페이지에 노출할 데이터 수")
// })
// @GetMapping
// public ResponseEntity<Page<AdminUserDTO>> getUser(@RequestParam(value = "page", defaultValue = "0") int page,
// @RequestParam(value = "size", defaultValue = "5") int size){
// Page<AdminUserDTO> activeUsers;
// activeUsers = userService.getUsers(PageRequest.of(page, size, Sort.by(Order.asc("role"), Order.asc("name"))));
//
//import io.swagger.v3.oas.annotations.tags.Tag;
//import lombok.RequiredArgsConstructor;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
////
////@Tag(name = "User API")
////@RequestMapping("/users")
////@RequiredArgsConstructor
////@RestController
////public class UserController {
////
////// @Operation(summary = "User 목록")
////// @Parameters({
////// @Parameter(name = "page", description = "현재 페이지"),
////// @Parameter(name = "size", description = "한 페이지에 노출할 데이터 수")
////// })
////// @GetMapping
////// public ResponseEntity<Page<ActiveUserDTO>> getUser(@RequestParam(value = "page", defaultValue = "0") int page,
////// @RequestParam(value = "size", defaultValue = "5") int size){
////// Page<ActiveUserDTO> activeUsers;
////// activeUsers = userService.getUsers(PageRequest.of(page, size, Sort.by(Order.asc("role"), Order.asc("name"))));
//////
////// return ResponseEntity.ok(activeUsers);
////// }
////}
// return ResponseEntity.ok(activeUsers);
// }

@Operation(summary = "User 목록 (학번 포함)")
@GetMapping("/private/users")
public ResponseEntity<List<PrivateUserDTO>> getUser(){
List<PrivateUserDTO> users = userService.getUsers();

return ResponseEntity.ok(users);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import com.haedal.haedalweb.constants.ErrorCode;
import com.haedal.haedalweb.constants.SuccessCode;
import com.haedal.haedalweb.domain.UserStatus;
import com.haedal.haedalweb.dto.response.ActiveUserDTO;
import com.haedal.haedalweb.dto.response.InActiveUserDTO;
import com.haedal.haedalweb.dto.response.user.AdminUserDTO;
import com.haedal.haedalweb.dto.request.UpdateRoleDTO;
import com.haedal.haedalweb.dto.response.common.SuccessResponse;
import com.haedal.haedalweb.service.admin.AdminUserService;
Expand All @@ -16,13 +15,15 @@
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

Expand All @@ -34,20 +35,21 @@
public class AdminUserController {
private final AdminUserService adminUserService;

@Operation(summary = "활동중인 User 목록")
@GetMapping("/active")
public ResponseEntity<List<ActiveUserDTO>> getActiveUser(){
List<ActiveUserDTO> activeUsers = adminUserService.getActiveUsers();
@Operation(summary = "User 목록")
@Parameter(name = "active", description = "활동 유저 true, 가입대기 유저 false")
@GetMapping
public ResponseEntity<List<AdminUserDTO>> getUser(@RequestParam Boolean active){
List<AdminUserDTO> users = null;

return ResponseEntity.ok(activeUsers);
}

@Operation(summary = "가입 대기 중인 User 목록")
@GetMapping("/inactive")
public ResponseEntity<List<InActiveUserDTO>> getInActiveUser(){
List<InActiveUserDTO> inActiveUsers = adminUserService.getInActiveUsers();
if (active) {
Sort sort = Sort.by(Sort.Order.asc("role"), Sort.Order.asc("name"));
users = adminUserService.getUsers(UserStatus.ACTIVE, sort);
} else {
Sort sort = Sort.by(Sort.Order.asc("regDate"), Sort.Order.asc("name"));
users = adminUserService.getUsers(UserStatus.INACTIVE, sort);
}

return ResponseEntity.ok(inActiveUsers);
return ResponseEntity.ok(users);
}

@Operation(summary = "가입 승인")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.haedal.haedalweb.dto.response;
package com.haedal.haedalweb.dto.response.user;

import java.time.LocalDateTime;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand All @@ -11,13 +12,19 @@
@AllArgsConstructor
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ActiveUserDTO {
public class AdminUserDTO {
@Schema(description = "유저 아이디", example = "haedal12")
private String userId;

@Schema(description = "유저 학번", example = "2024111234")
private Integer studentNumber;

@Schema(description = "유저 이름", example = "조대성")
private String userName;

@Schema(description = "유저 권한", example = "(해구르르, 팀장, 일반)")
private String role;

@Schema(description = "가입 날짜")
private LocalDateTime regDate;
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package com.haedal.haedalweb.dto.response;
package com.haedal.haedalweb.dto.response.user;

import java.time.LocalDateTime;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;

@Getter
@AllArgsConstructor
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class InActiveUserDTO {
public class PrivateUserDTO {
@Schema(description = "유저 아이디", example = "haedal12")
private String userId;

@Schema(description = "유저 학번", example = "2024111234")
private Integer studentNumber;

@Schema(description = "유저 이름", example = "조대성")
private String userName;
@Schema(description = "가입 날짜")
private LocalDateTime regDate;
}
19 changes: 19 additions & 0 deletions src/main/java/com/haedal/haedalweb/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import com.haedal.haedalweb.constants.ErrorCode;
import com.haedal.haedalweb.domain.User;
import com.haedal.haedalweb.domain.UserStatus;
import com.haedal.haedalweb.dto.CustomUserDetails;
import com.haedal.haedalweb.dto.response.user.PrivateUserDTO;
import com.haedal.haedalweb.exception.BusinessException;
import com.haedal.haedalweb.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand All @@ -32,4 +35,20 @@ public User getLoggedInUser() {

return findUserById(userId);
}

public List<PrivateUserDTO> getUsers() {
List<User> users = userRepository.findByUserStatus(UserStatus.ACTIVE, null);

return users.stream()
.map(this::convertToPrivateUserDTO)
.collect(Collectors.toList());
}

private PrivateUserDTO convertToPrivateUserDTO(User user) {
return PrivateUserDTO.builder()
.userId(user.getId())
.studentNumber(user.getStudentNumber())
.userName(user.getName())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import com.haedal.haedalweb.domain.Role;
import com.haedal.haedalweb.domain.User;
import com.haedal.haedalweb.domain.UserStatus;
import com.haedal.haedalweb.dto.response.ActiveUserDTO;
import com.haedal.haedalweb.dto.response.InActiveUserDTO;
import com.haedal.haedalweb.dto.response.user.AdminUserDTO;
import com.haedal.haedalweb.exception.BusinessException;
import com.haedal.haedalweb.repository.UserRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -21,38 +20,20 @@
public class AdminUserService {
private final UserRepository userRepository;

public List<ActiveUserDTO> getActiveUsers() {
Sort sort = Sort.by(Sort.Order.asc("role"), Sort.Order.asc("name"));
List<User> users = userRepository.findByUserStatus(UserStatus.ACTIVE, sort);
public List<AdminUserDTO> getUsers(UserStatus userStatus, Sort sort) {
List<User> users = userRepository.findByUserStatus(userStatus, sort);

return users.stream()
.map(this::convertToActiveUserDTO)
.map(this::convertToAdminUserDTO)
.collect(Collectors.toList());
}

public List<InActiveUserDTO> getInActiveUsers() {
Sort sort = Sort.by(Sort.Order.asc("regDate"), Sort.Order.asc("name"));
List<User> users = userRepository.findByUserStatus(UserStatus.INACTIVE, sort);

return users.stream()
.map(this::convertToInActiveUserDTO)
.collect(Collectors.toList());
}

private ActiveUserDTO convertToActiveUserDTO(User user) {
return ActiveUserDTO.builder()
private AdminUserDTO convertToAdminUserDTO(User user) {
return AdminUserDTO.builder()
.userId(user.getId())
.studentNumber(user.getStudentNumber())
.userName(user.getName())
.role(user.getRole().getLabel())
.build();
}

private InActiveUserDTO convertToInActiveUserDTO(User user) {
return InActiveUserDTO.builder()
.userId(user.getId())
.studentNumber(user.getStudentNumber())
.userName(user.getName())
.regDate(user.getRegDate())
.build();
}
Expand Down

0 comments on commit 38798d2

Please sign in to comment.