From 1e9079452519ebef9beda9708e0eedd4d24ffd87 Mon Sep 17 00:00:00 2001 From: baegteun Date: Sun, 9 Oct 2022 07:58:19 +0900 Subject: [PATCH] =?UTF-8?q?:recycle:=20::=20=EC=9D=B4=EB=A6=84=EA=B0=99?= =?UTF-8?q?=EC=9D=80=20=EC=9E=90=EC=9E=98=ED=95=9C=20refactoring?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/persentation/ClientController.kt | 20 ++++++------- ...ientOneResDto.kt => SingleClientResDto.kt} | 2 +- .../client/services/GetAllClientsService.kt | 18 ------------ .../client/services/GetMyAllClientsService.kt | 18 ++++++++++++ ...Service.kt => GetMyDetailClientService.kt} | 8 +++--- .../client/services/RegisterClientService.kt | 8 ++++-- .../client/services/UpdateClientService.kt | 6 ++-- .../gauth/global/security/SecurityConfig.kt | 6 ++++ .../msg/gauth/global/util/CurrentUserUtil.kt | 28 ------------------- 9 files changed, 47 insertions(+), 67 deletions(-) rename src/main/kotlin/com/msg/gauth/domain/client/persentation/dto/response/{ClientOneResDto.kt => SingleClientResDto.kt} (92%) delete mode 100644 src/main/kotlin/com/msg/gauth/domain/client/services/GetAllClientsService.kt create mode 100644 src/main/kotlin/com/msg/gauth/domain/client/services/GetMyAllClientsService.kt rename src/main/kotlin/com/msg/gauth/domain/client/services/{GetOneClientService.kt => GetMyDetailClientService.kt} (68%) delete mode 100644 src/main/kotlin/com/msg/gauth/global/util/CurrentUserUtil.kt diff --git a/src/main/kotlin/com/msg/gauth/domain/client/persentation/ClientController.kt b/src/main/kotlin/com/msg/gauth/domain/client/persentation/ClientController.kt index a78cc52b..11adf695 100644 --- a/src/main/kotlin/com/msg/gauth/domain/client/persentation/ClientController.kt +++ b/src/main/kotlin/com/msg/gauth/domain/client/persentation/ClientController.kt @@ -2,11 +2,11 @@ package com.msg.gauth.domain.client.persentation import com.msg.gauth.domain.client.persentation.dto.request.ClientRegisterReqDto import com.msg.gauth.domain.client.persentation.dto.request.ClientUpdateReqDto -import com.msg.gauth.domain.client.persentation.dto.response.ClientOneResDto +import com.msg.gauth.domain.client.persentation.dto.response.SingleClientResDto import com.msg.gauth.domain.client.persentation.dto.response.ClientDetailResDto import com.msg.gauth.domain.client.persentation.dto.response.ClientRegisterResDto -import com.msg.gauth.domain.client.services.GetAllClientsService -import com.msg.gauth.domain.client.services.GetOneClientService +import com.msg.gauth.domain.client.services.GetMyAllClientsService +import com.msg.gauth.domain.client.services.GetMyDetailClientService import com.msg.gauth.domain.client.services.RegisterClientService import com.msg.gauth.domain.client.services.UpdateClientService import org.springframework.http.ResponseEntity @@ -21,27 +21,27 @@ import org.springframework.web.bind.annotation.RestController @RestController @RequestMapping("/client") class ClientController( - private val getAllClientsService: GetAllClientsService, + private val getMyAllClientsService: GetMyAllClientsService, private val updateClientService: UpdateClientService, - private val getOneClientService: GetOneClientService, + private val getMyDetailClientService: GetMyDetailClientService, private val registerClientService: RegisterClientService, ) { @PostMapping - fun registerClient(@RequestBody clientRegisterReqDto: ClientRegisterReqDto):ResponseEntity{ + fun registerClient(@RequestBody clientRegisterReqDto: ClientRegisterReqDto): ResponseEntity{ val clientRegisterResDto = registerClientService.execute(clientRegisterReqDto) return ResponseEntity.ok(clientRegisterResDto) } @GetMapping - fun getMyAllClients(): ResponseEntity>{ - val result = getAllClientsService.execute() + fun getMyAllClients(): ResponseEntity>{ + val result = getMyAllClientsService.execute() return ResponseEntity.ok(result) } @GetMapping("/{id}") - fun getMyOneClient(@PathVariable id: Long): ResponseEntity{ - val clientOneResDto = getOneClientService.execute(id) + fun getMyDetailClient(@PathVariable id: Long): ResponseEntity{ + val clientOneResDto = getMyDetailClientService.execute(id) return ResponseEntity.ok(clientOneResDto) } diff --git a/src/main/kotlin/com/msg/gauth/domain/client/persentation/dto/response/ClientOneResDto.kt b/src/main/kotlin/com/msg/gauth/domain/client/persentation/dto/response/SingleClientResDto.kt similarity index 92% rename from src/main/kotlin/com/msg/gauth/domain/client/persentation/dto/response/ClientOneResDto.kt rename to src/main/kotlin/com/msg/gauth/domain/client/persentation/dto/response/SingleClientResDto.kt index 40d2434d..39a02ce6 100644 --- a/src/main/kotlin/com/msg/gauth/domain/client/persentation/dto/response/ClientOneResDto.kt +++ b/src/main/kotlin/com/msg/gauth/domain/client/persentation/dto/response/SingleClientResDto.kt @@ -2,7 +2,7 @@ package com.msg.gauth.domain.client.persentation.dto.response import com.msg.gauth.domain.client.Client -data class ClientOneResDto( +data class SingleClientResDto( val clientId: String, val serviceName: String, val serviceUri: String, diff --git a/src/main/kotlin/com/msg/gauth/domain/client/services/GetAllClientsService.kt b/src/main/kotlin/com/msg/gauth/domain/client/services/GetAllClientsService.kt deleted file mode 100644 index 1fd4877a..00000000 --- a/src/main/kotlin/com/msg/gauth/domain/client/services/GetAllClientsService.kt +++ /dev/null @@ -1,18 +0,0 @@ -package com.msg.gauth.domain.client.services - -import com.msg.gauth.domain.client.persentation.dto.response.ClientOneResDto -import com.msg.gauth.domain.client.repository.ClientRepository -import com.msg.gauth.global.util.CurrentUserUtil -import org.springframework.stereotype.Service -import org.springframework.transaction.annotation.Transactional - -@Service -@Transactional(readOnly = true) -class GetAllClientsService( - private val clientRepository: ClientRepository, - private val currentUserUtil: CurrentUserUtil, -) { - fun execute(): List = - clientRepository.findAllByCreatedBy(currentUserUtil.getCurrentUser()) - .map { ClientOneResDto(it) } -} \ No newline at end of file diff --git a/src/main/kotlin/com/msg/gauth/domain/client/services/GetMyAllClientsService.kt b/src/main/kotlin/com/msg/gauth/domain/client/services/GetMyAllClientsService.kt new file mode 100644 index 00000000..c00c25d4 --- /dev/null +++ b/src/main/kotlin/com/msg/gauth/domain/client/services/GetMyAllClientsService.kt @@ -0,0 +1,18 @@ +package com.msg.gauth.domain.client.services + +import com.msg.gauth.domain.client.persentation.dto.response.SingleClientResDto +import com.msg.gauth.domain.client.repository.ClientRepository +import com.msg.gauth.domain.user.utils.UserUtil +import org.springframework.stereotype.Service +import org.springframework.transaction.annotation.Transactional + +@Service +@Transactional(readOnly = true) +class GetMyAllClientsService( + private val clientRepository: ClientRepository, + private val userUtil: UserUtil, +) { + fun execute(): List = + clientRepository.findAllByCreatedBy(userUtil.fetchCurrentUser()) + .map { SingleClientResDto(it) } +} \ No newline at end of file diff --git a/src/main/kotlin/com/msg/gauth/domain/client/services/GetOneClientService.kt b/src/main/kotlin/com/msg/gauth/domain/client/services/GetMyDetailClientService.kt similarity index 68% rename from src/main/kotlin/com/msg/gauth/domain/client/services/GetOneClientService.kt rename to src/main/kotlin/com/msg/gauth/domain/client/services/GetMyDetailClientService.kt index 6bdf77a5..3decba3b 100644 --- a/src/main/kotlin/com/msg/gauth/domain/client/services/GetOneClientService.kt +++ b/src/main/kotlin/com/msg/gauth/domain/client/services/GetMyDetailClientService.kt @@ -3,18 +3,18 @@ package com.msg.gauth.domain.client.services import com.msg.gauth.domain.client.exception.ClientNotFindException import com.msg.gauth.domain.client.persentation.dto.response.ClientDetailResDto import com.msg.gauth.domain.client.repository.ClientRepository -import com.msg.gauth.global.util.CurrentUserUtil +import com.msg.gauth.domain.user.utils.UserUtil import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @Service @Transactional(readOnly = true) -class GetOneClientService( +class GetMyDetailClientService( private val clientRepository: ClientRepository, - private val currentUserUtil: CurrentUserUtil, + private val userUtil: UserUtil ) { fun execute(id: Long): ClientDetailResDto{ - val client = clientRepository.findByIdAndCreatedBy(id, currentUserUtil.getCurrentUser()) ?: throw ClientNotFindException() + val client = clientRepository.findByIdAndCreatedBy(id, userUtil.fetchCurrentUser()) ?: throw ClientNotFindException() return ClientDetailResDto(client) } } \ No newline at end of file diff --git a/src/main/kotlin/com/msg/gauth/domain/client/services/RegisterClientService.kt b/src/main/kotlin/com/msg/gauth/domain/client/services/RegisterClientService.kt index 9c03e7eb..d8fc73c2 100644 --- a/src/main/kotlin/com/msg/gauth/domain/client/services/RegisterClientService.kt +++ b/src/main/kotlin/com/msg/gauth/domain/client/services/RegisterClientService.kt @@ -3,19 +3,21 @@ package com.msg.gauth.domain.client.services import com.msg.gauth.domain.client.persentation.dto.request.ClientRegisterReqDto import com.msg.gauth.domain.client.persentation.dto.response.ClientRegisterResDto import com.msg.gauth.domain.client.repository.ClientRepository -import com.msg.gauth.global.util.CurrentUserUtil +import com.msg.gauth.domain.user.utils.UserUtil import org.springframework.stereotype.Service +import org.springframework.transaction.annotation.Transactional import java.util.UUID @Service class RegisterClientService( private val clientRepository: ClientRepository, - private val currentUserUtil: CurrentUserUtil, + private val userUtil: UserUtil ) { + @Transactional fun execute(clientRegisterDto: ClientRegisterReqDto): ClientRegisterResDto { val clientSecret = createUUID() val clientId = createUUID() - val user = currentUserUtil.getCurrentUser() + val user = userUtil.fetchCurrentUser() val client = clientRegisterDto.toEntity(user, clientSecret, clientId) return ClientRegisterResDto(clientRepository.save(client)) } diff --git a/src/main/kotlin/com/msg/gauth/domain/client/services/UpdateClientService.kt b/src/main/kotlin/com/msg/gauth/domain/client/services/UpdateClientService.kt index 6360fd57..de26ec6f 100644 --- a/src/main/kotlin/com/msg/gauth/domain/client/services/UpdateClientService.kt +++ b/src/main/kotlin/com/msg/gauth/domain/client/services/UpdateClientService.kt @@ -3,18 +3,18 @@ package com.msg.gauth.domain.client.services import com.msg.gauth.domain.client.exception.ClientNotFindException import com.msg.gauth.domain.client.persentation.dto.request.ClientUpdateReqDto import com.msg.gauth.domain.client.repository.ClientRepository -import com.msg.gauth.global.util.CurrentUserUtil +import com.msg.gauth.domain.user.utils.UserUtil import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @Service class UpdateClientService( private val clientRepository: ClientRepository, - private val currentUserUtil: CurrentUserUtil, + private val userUtil: UserUtil ) { @Transactional fun updateClient(id: Long, clientUpdateReqDto: ClientUpdateReqDto){ - val client = clientRepository.findByIdAndCreatedBy(id, currentUserUtil.getCurrentUser()) ?: throw ClientNotFindException() + val client = clientRepository.findByIdAndCreatedBy(id, userUtil.fetchCurrentUser()) ?: throw ClientNotFindException() client.update(clientUpdateReqDto) } } \ No newline at end of file diff --git a/src/main/kotlin/com/msg/gauth/global/security/SecurityConfig.kt b/src/main/kotlin/com/msg/gauth/global/security/SecurityConfig.kt index b4b1cf91..a2a8b626 100644 --- a/src/main/kotlin/com/msg/gauth/global/security/SecurityConfig.kt +++ b/src/main/kotlin/com/msg/gauth/global/security/SecurityConfig.kt @@ -48,6 +48,12 @@ class SecurityConfig( .antMatchers(HttpMethod.POST, "/email").permitAll() .antMatchers(HttpMethod.GET, "/email/authentication").permitAll() + // Client + .antMatchers(HttpMethod.GET, "/client").authenticated() + .antMatchers(HttpMethod.POST, "/client").authenticated() + .antMatchers(HttpMethod.GET, "/client/{id}").authenticated() + .antMatchers(HttpMethod.PATCH, "/client/{id}").authenticated() + .anyRequest().denyAll() .and() .exceptionHandling() diff --git a/src/main/kotlin/com/msg/gauth/global/util/CurrentUserUtil.kt b/src/main/kotlin/com/msg/gauth/global/util/CurrentUserUtil.kt deleted file mode 100644 index 0601681f..00000000 --- a/src/main/kotlin/com/msg/gauth/global/util/CurrentUserUtil.kt +++ /dev/null @@ -1,28 +0,0 @@ -package com.msg.gauth.global.util - -import com.msg.gauth.domain.user.User -import com.msg.gauth.domain.user.exception.UserNotFoundException -import com.msg.gauth.domain.user.repository.UserRepository -import com.msg.gauth.global.security.auth.AuthDetails -import org.springframework.security.core.context.SecurityContextHolder -import org.springframework.security.core.userdetails.UserDetails -import org.springframework.stereotype.Component - -@Component -class CurrentUserUtil( - private val userRepository: UserRepository -) { - private fun getCurrentEmail(): String{ - val principal = SecurityContextHolder.getContext().authentication.principal - val email: String = - if(principal is UserDetails){ - (principal as AuthDetails).username - }else{ - principal.toString() - } - return email - } - - fun getCurrentUser(): User = - userRepository.findByEmail(getCurrentEmail())?: throw UserNotFoundException() -} \ No newline at end of file