diff --git a/src/main/java/com/hanaro/starbucks/config/SecurityConfig.java b/src/main/java/com/hanaro/starbucks/config/SecurityConfig.java index 738a680..2ffc000 100644 --- a/src/main/java/com/hanaro/starbucks/config/SecurityConfig.java +++ b/src/main/java/com/hanaro/starbucks/config/SecurityConfig.java @@ -27,17 +27,17 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { .csrf((auth) -> auth.disable()) .cors((cors) -> cors.configurationSource(corsConfigurationSource())) - .authorizeHttpRequests((auth) -> auth - // 로그인과 회원가입은 모든 사용자에게 허용한다. - .requestMatchers( - new AntPathRequestMatcher("/login"), - new AntPathRequestMatcher("/signup") - ).permitAll() // 권한이 있든 말든 모두 접근 가능 - // admin일 경우에만 /admin에 대한 요청에서 접근을 허용한다. - .requestMatchers("/admin").hasRole("ADMIN") - // 그 외 모든 요청은 인증된 사용자에게만 허용한다. - .anyRequest().authenticated() - ) +// .authorizeHttpRequests((auth) -> auth +// // 로그인과 회원가입은 모든 사용자에게 허용한다. +// .requestMatchers( +// new AntPathRequestMatcher("/login"), +// new AntPathRequestMatcher("/signup") +// ).permitAll() // 권한이 있든 말든 모두 접근 가능 +// // admin일 경우에만 /admin에 대한 요청에서 접근을 허용한다. +// .requestMatchers("/admin").hasRole("ADMIN") +// // 그 외 모든 요청은 인증된 사용자에게만 허용한다. +// .anyRequest().authenticated() +// ) // .formLogin((formLogin) -> formLogin // .loginProcessingUrl("/login") // .successHandler(((request, response, authentication) -> { @@ -47,9 +47,9 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { // .permitAll() // ); -// .authorizeHttpRequests( (auth) -> auth -// .requestMatchers( new AntPathRequestMatcher("/**") ) -// .permitAll()) + .authorizeHttpRequests( (auth) -> auth + .requestMatchers( new AntPathRequestMatcher("/**") ) + .permitAll()) .sessionManagement(sessionManagement -> sessionManagement.sessionCreationPolicy( diff --git a/src/main/java/com/hanaro/starbucks/controller/MemberController.java b/src/main/java/com/hanaro/starbucks/controller/MemberController.java index 5f03dea..8d73808 100644 --- a/src/main/java/com/hanaro/starbucks/controller/MemberController.java +++ b/src/main/java/com/hanaro/starbucks/controller/MemberController.java @@ -4,6 +4,7 @@ import com.hanaro.starbucks.dto.member.LoginReqDto; import com.hanaro.starbucks.dto.member.MemberResDto; import com.hanaro.starbucks.dto.member.SignupReqDto; +import com.hanaro.starbucks.dto.member.MemberUpdateReqDto; import com.hanaro.starbucks.service.MemberService; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; @@ -17,22 +18,26 @@ @RequiredArgsConstructor @RequestMapping("/api/v1/users") public class MemberController { - private final MemberService userService; + private final MemberService memberService; private final JwtUtil jwtUtil; @GetMapping("") public List getUsers(){ - return userService.getUsers(); + return memberService.getUsers(); + } + @GetMapping("/{userIdx}") + public MemberResDto getUser(@PathVariable int userIdx){ + return memberService.getUser(userIdx); } @PostMapping("/signup") @ResponseBody public ResponseEntity signup(@RequestBody SignupReqDto user) { System.out.println(user); - boolean findUser = userService.findUserByUserId(user.getUserId()); + boolean findUser = memberService.findUserByUserId(user.getUserId()); System.out.println(findUser); if (!findUser) { - MemberResDto newUser = userService.createUser(user); + MemberResDto newUser = memberService.createUser(user); return ResponseEntity.ok(newUser.getUserId()); } return ResponseEntity.status(HttpStatus.CONFLICT).body("이미 존재하는 사용자입니다."); @@ -41,7 +46,7 @@ public ResponseEntity signup(@RequestBody SignupReqDto user) { @PostMapping("/login") @ResponseBody public ResponseEntity login(@RequestBody LoginReqDto user) { - MemberResDto findUser = userService.findUserByUserIdAndUserPw(user.getUserId(), user.getUserPw()); + MemberResDto findUser = memberService.findUserByUserIdAndUserPw(user.getUserId(), user.getUserPw()); if (findUser != null) { String token = jwtUtil.createToken(findUser.getUserId(), Arrays.asList(findUser.getUserRole())); return ResponseEntity.ok(token); @@ -50,4 +55,14 @@ public ResponseEntity login(@RequestBody LoginReqDto user) { } } + @PutMapping("/admin/{userIdx}") + public void updateUser(@PathVariable int userIdx, @RequestBody MemberUpdateReqDto user){ + memberService.updateUser(userIdx, user); + } + + @DeleteMapping("/admin/{userIdx}") + public void deleteUser(@PathVariable int userIdx){ + memberService.deleteUser(userIdx); + } + } diff --git a/src/main/java/com/hanaro/starbucks/dto/member/MemberUpdateReqDto.java b/src/main/java/com/hanaro/starbucks/dto/member/MemberUpdateReqDto.java new file mode 100644 index 0000000..ffd353f --- /dev/null +++ b/src/main/java/com/hanaro/starbucks/dto/member/MemberUpdateReqDto.java @@ -0,0 +1,15 @@ +package com.hanaro.starbucks.dto.member; + +import lombok.Getter; + +import java.time.LocalDate; + +@Getter +public class MemberUpdateReqDto { + private String userId; + private String userPw; + private String userNickname; + private String userRole; + private Integer userPoint; + private LocalDate userJoinDate; +} \ No newline at end of file diff --git a/src/main/java/com/hanaro/starbucks/entity/Member.java b/src/main/java/com/hanaro/starbucks/entity/Member.java index b9dec30..23397d2 100644 --- a/src/main/java/com/hanaro/starbucks/entity/Member.java +++ b/src/main/java/com/hanaro/starbucks/entity/Member.java @@ -1,13 +1,12 @@ package com.hanaro.starbucks.entity; +import com.hanaro.starbucks.dto.member.MemberUpdateReqDto; import jakarta.persistence.*; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; -import org.springframework.cglib.core.Local; import java.time.LocalDate; -import java.util.Date; @Entity @Getter @@ -45,6 +44,14 @@ public Member(String userId, String userPw, String userNickname) { this.userNickname = userNickname; this.userRole = "USER"; this.userPoint = 0; + } + + public void update(MemberUpdateReqDto dto) { + this.userId = dto.getUserId(); + this.userNickname=dto.getUserNickname(); + this.userPw = dto.getUserPw(); + this.userRole = dto.getUserRole(); + this.userPoint = dto.getUserPoint(); this.userJoinDate = LocalDate.now(); } } \ No newline at end of file diff --git a/src/main/java/com/hanaro/starbucks/service/MemberService.java b/src/main/java/com/hanaro/starbucks/service/MemberService.java index aefd3c5..ef478b3 100644 --- a/src/main/java/com/hanaro/starbucks/service/MemberService.java +++ b/src/main/java/com/hanaro/starbucks/service/MemberService.java @@ -2,6 +2,7 @@ import com.hanaro.starbucks.dto.member.MemberResDto; import com.hanaro.starbucks.dto.member.SignupReqDto; +import com.hanaro.starbucks.dto.member.MemberUpdateReqDto; import com.hanaro.starbucks.entity.Member; import com.hanaro.starbucks.repository.MemberRepository; import lombok.RequiredArgsConstructor; @@ -25,6 +26,16 @@ public List getUsers(){ return memberRepository.findAll().stream().map(MemberResDto::new).collect(Collectors.toList()); } + @Transactional + public MemberResDto getUser(int idx){ + + Optional optionalMember = memberRepository.findById(idx); + if(optionalMember.isEmpty()){ + throw new IllegalArgumentException("존재하지 않는 회원입니다."); + } + return new MemberResDto(optionalMember.get()); + } + @Transactional public boolean findUserByUserId(String userId) { return memberRepository.existsByUserId(userId); @@ -57,4 +68,17 @@ public MemberResDto findUserByUserIdAndUserPw(String userId, String userPw) { } } + public void updateUser(int userIdx, MemberUpdateReqDto user){ + Optional optional = memberRepository.findById(userIdx); + if(optional.isEmpty()){ + throw new IllegalArgumentException("존재하지 않는 회원입니다."); + } + Member member = optional.get(); + member.update(user); + memberRepository.save(member); + } + + public void deleteUser(int userIdx){ + memberRepository.deleteById(userIdx); + } }