Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

admin api #57

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,15 @@ class OAuthRequestException : UserException(
httpErrorCode = HttpStatus.BAD_REQUEST,
msg = "소셜 로그인 요청에 실패했습니다."
)

class EmailNotMatchException : UserException(
errorCode = 0,
httpErrorCode = HttpStatus.BAD_REQUEST,
msg = "업데이트 할 유저 이메일이 기존 이메일과 일치하지 않습니다"
)

class UserNotAdminException : UserException(
errorCode = 0,
httpErrorCode = HttpStatus.BAD_REQUEST,
msg = "Admin 권한이 없습니다"
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.springframework.security.core.userdetails.UserDetails
class CustomUserDetails(private val user: UserEntity) : UserDetails {

override fun getAuthorities(): Collection<GrantedAuthority> {
return listOf(SimpleGrantedAuthority(user.role))
return listOf(SimpleGrantedAuthority(user.role.name))
}

override fun getPassword(): String {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.wafflestudio.toyproject.memoWithTags.user

enum class RoleType(val type: String) {
ROLE_USER("ROLE_USER"),
ROLE_ADMIN("ROLE_ADMIN");


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.wafflestudio.toyproject.memoWithTags.user.controller

import com.wafflestudio.toyproject.memoWithTags.user.AuthUser
import com.wafflestudio.toyproject.memoWithTags.user.dto.AdminRequest.CreateUserRequest
import com.wafflestudio.toyproject.memoWithTags.user.dto.AdminRequest.UpdateUserRequest
import com.wafflestudio.toyproject.memoWithTags.user.service.AdminService
import com.wafflestudio.toyproject.memoWithTags.user.service.UserService
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
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.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.util.*

@Tag(name = "admin api", description = "관리자 관련 api")
@RestController
@RequestMapping("/api/v1")
class AdminController(
private val adminService: AdminService,
private val userService: UserService
) {
@Operation(summary = "유저 목록 가져오기")
@GetMapping("/admin/user")
fun getUsers(@AuthUser user: User): List<User> {
adminService.isAdmin(user.id)
return adminService.getUsers()
}

@Operation(summary = "유저 계정 생성")
@PostMapping("/admin/user")
fun createUser(@AuthUser user: User, @RequestBody request: CreateUserRequest): User {
adminService.isAdmin(user.id)
return userService.register(request.email, request.password)
}

@Operation(summary = "유저 계정 삭제하기")
@DeleteMapping("/admin/user/{userId}")
fun deleteUser(@AuthUser user: User, @PathVariable userId: UUID): ResponseEntity<Unit> {
adminService.isAdmin(user.id)
adminService.deleteUser(userId)
return ResponseEntity.ok().build()
}

@Operation(summary = "유저 계정 업데이트")
@GetMapping("/admin/user")
fun getUsers(@AuthUser user: User, @RequestBody request: UpdateUserRequest): User {
adminService.isAdmin(user.id)
return adminService.updateUser(request.id, request.userUpdateInfo)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.wafflestudio.toyproject.memoWithTags.user.dto

import java.util.UUID

sealed class AdminRequest {
data class CreateUserRequest(
val email: String,
val password: String
) : AdminRequest()

data class UpdateUserRequest(
val id: UUID,
val userUpdateInfo: UserUpdateInfo
) : AdminRequest()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.wafflestudio.toyproject.memoWithTags.user.dto

class AdminResponse
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.wafflestudio.toyproject.memoWithTags.user.dto

import com.wafflestudio.toyproject.memoWithTags.user.RoleType
import com.wafflestudio.toyproject.memoWithTags.user.SocialType

class UserUpdateInfo(
val nickname: String,
val password: String,
val verified: Boolean,
val role: RoleType,
val socialType: SocialType
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.wafflestudio.toyproject.memoWithTags.user.persistence

import com.wafflestudio.toyproject.memoWithTags.memo.persistence.MemoEntity
import com.wafflestudio.toyproject.memoWithTags.tag.persistence.TagEntity
import com.wafflestudio.toyproject.memoWithTags.user.RoleType
import com.wafflestudio.toyproject.memoWithTags.user.RoleType.ROLE_USER
import com.wafflestudio.toyproject.memoWithTags.user.SocialType
import jakarta.persistence.CascadeType
import jakarta.persistence.Column
Expand All @@ -27,9 +29,9 @@ class UserEntity(
@Column(name = "verified", nullable = false)
var verified: Boolean = false,
@Column(name = "role", nullable = false)
val role: String = "ROLE_USER",
var role: RoleType = ROLE_USER,
@Column(name = "social_type", nullable = true)
val socialType: SocialType? = null,
var socialType: SocialType? = null,
@Column(name = "created_at", nullable = false)
val createdAt: Instant,
@OneToMany(mappedBy = "user", cascade = [CascadeType.ALL], orphanRemoval = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.wafflestudio.toyproject.memoWithTags.user.service

import com.wafflestudio.toyproject.memoWithTags.exception.UserNotAdminException
import com.wafflestudio.toyproject.memoWithTags.exception.UserNotFoundException
import com.wafflestudio.toyproject.memoWithTags.user.RoleType
import com.wafflestudio.toyproject.memoWithTags.user.controller.User
import com.wafflestudio.toyproject.memoWithTags.user.dto.UserUpdateInfo
import com.wafflestudio.toyproject.memoWithTags.user.persistence.UserRepository
import org.mindrot.jbcrypt.BCrypt
import org.springframework.stereotype.Service
import java.util.UUID

@Service
class AdminService(
private val userRepository: UserRepository
) {
fun isAdmin(userId: UUID) {
val userEntity = userRepository.findById(userId).orElseThrow { UserNotFoundException() }
if (userEntity.role != RoleType.ROLE_ADMIN) {
throw UserNotAdminException()
}
}

fun getUsers(): List<User> {
return userRepository.findAll().map { User.fromEntity(it) }
}

fun deleteUser(userId: UUID) {
val userEntity = userRepository.findById(userId).orElseThrow { UserNotFoundException() }
userRepository.delete(userEntity)
}

fun updateUser(userId: UUID, userUpdateInfo: UserUpdateInfo): User {
val userEntity = userRepository.findById(userId).orElseThrow { UserNotFoundException() }
userEntity.nickname = userUpdateInfo.nickname
userEntity.hashedPassword = BCrypt.hashpw(userUpdateInfo.password, BCrypt.gensalt())
userEntity.verified = userUpdateInfo.verified
userEntity.role = userUpdateInfo.role
userEntity.socialType = userUpdateInfo.socialType
userRepository.save(userEntity)
return User.fromEntity(userEntity)
}
}
Loading