From bed6b0411599bafc32c800ff793194dc98ce84bf Mon Sep 17 00:00:00 2001 From: Shiina Kin Date: Wed, 18 Sep 2024 20:07:52 +0800 Subject: [PATCH] feat: complete roleController --- .../sakurasou/constant/PermissionConstants.kt | 1 + .../io/sakurasou/controller/RoleController.kt | 52 +++++++++++++++++-- .../io/sakurasou/controller/vo/CommonVO.kt | 2 +- .../io/sakurasou/extension/RouteExtension.kt | 2 +- .../io/sakurasou/model/common/DatabaseInit.kt | 3 ++ .../kotlin/io/sakurasou/plugins/Routing.kt | 3 +- 6 files changed, 57 insertions(+), 6 deletions(-) diff --git a/app/src/main/kotlin/io/sakurasou/constant/PermissionConstants.kt b/app/src/main/kotlin/io/sakurasou/constant/PermissionConstants.kt index 6011bfd7..d7c2e99d 100644 --- a/app/src/main/kotlin/io/sakurasou/constant/PermissionConstants.kt +++ b/app/src/main/kotlin/io/sakurasou/constant/PermissionConstants.kt @@ -35,6 +35,7 @@ const val GROUP_READ_ALL = "$GROUP:$READ:$ALL" const val GROUP_WRITE = "$GROUP:$WRITE" const val GROUP_DELETE = "$GROUP:$DELETE" +const val ROLE_READ_SELF = "$ROLE:$READ:$SELF" const val ROLE_READ_ALL = "$ROLE:$READ:$ALL" const val PERMISSION_READ = "$PERMISSION:$READ" diff --git a/app/src/main/kotlin/io/sakurasou/controller/RoleController.kt b/app/src/main/kotlin/io/sakurasou/controller/RoleController.kt index a6e8a65e..7b9b00d1 100644 --- a/app/src/main/kotlin/io/sakurasou/controller/RoleController.kt +++ b/app/src/main/kotlin/io/sakurasou/controller/RoleController.kt @@ -1,20 +1,33 @@ package io.sakurasou.controller import io.github.smiley4.ktorswaggerui.dsl.routing.get +import io.github.smiley4.ktorswaggerui.dsl.routing.route import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.routing.* import io.sakurasou.constant.ROLE_READ_ALL +import io.sakurasou.constant.ROLE_READ_SELF import io.sakurasou.controller.vo.RoleVO +import io.sakurasou.extension.getPrincipal +import io.sakurasou.extension.success import io.sakurasou.plugins.AuthorizationPlugin +import io.sakurasou.service.role.RoleService /** * @author Shiina Kin * 2024/9/9 08:53 */ -fun Route.roleRoute() { +fun Route.roleRoute(roleService: RoleService) { + val controller = RoleController(roleService) route("role") { + fetchAllRolesAndPermissions(controller) + fetchAllRolesAndPermissionsOfUser(controller) + } +} + +private fun Route.fetchAllRolesAndPermissions(controller: RoleController) { + route { install(AuthorizationPlugin) { permission = ROLE_READ_ALL } @@ -29,10 +42,43 @@ fun Route.roleRoute() { } } }) { - TODO() + val rolesWithPermissions = controller.handleListAllRolesWithPermissions() + call.success(rolesWithPermissions) } } } -class RoleController { +private fun Route.fetchAllRolesAndPermissionsOfUser(controller: RoleController) { + route { + install(AuthorizationPlugin) { + permission = ROLE_READ_SELF + } + get("self", { + protected = true + response { + HttpStatusCode.OK to { + description = "success" + body> { + description = "all roles with permissions of user" + } + } + } + }) { + val principal = call.getPrincipal() + val rolesWithPermissions = controller.handleListAllRolesWithPermissionsOfUser(principal.roles) + call.success(rolesWithPermissions) + } + } +} + +class RoleController( + private val roleService: RoleService +) { + suspend fun handleListAllRolesWithPermissions(): Map { + return roleService.listRolesWithPermissions() + } + + suspend fun handleListAllRolesWithPermissionsOfUser(roles: List): Map { + return roleService.listRolesWithPermissionsOfUser(roles) + } } \ No newline at end of file diff --git a/app/src/main/kotlin/io/sakurasou/controller/vo/CommonVO.kt b/app/src/main/kotlin/io/sakurasou/controller/vo/CommonVO.kt index 6542b1ba..a186a926 100644 --- a/app/src/main/kotlin/io/sakurasou/controller/vo/CommonVO.kt +++ b/app/src/main/kotlin/io/sakurasou/controller/vo/CommonVO.kt @@ -24,7 +24,7 @@ data class CommonResponse( constructor(code: Int, message: String) : this(code, message, null) companion object { - fun success(data: T): CommonResponse { + inline fun success(data: T): CommonResponse { return CommonResponse(0, "success", data) } diff --git a/app/src/main/kotlin/io/sakurasou/extension/RouteExtension.kt b/app/src/main/kotlin/io/sakurasou/extension/RouteExtension.kt index 88cc6834..de3d92b6 100644 --- a/app/src/main/kotlin/io/sakurasou/extension/RouteExtension.kt +++ b/app/src/main/kotlin/io/sakurasou/extension/RouteExtension.kt @@ -50,7 +50,7 @@ suspend fun ApplicationCall.success() { respond(CommonResponse.success(Unit)) } -suspend fun ApplicationCall.success(data: T) { +suspend inline fun ApplicationCall.success(data: T) { respond(CommonResponse.success(data)) } diff --git a/app/src/main/kotlin/io/sakurasou/model/common/DatabaseInit.kt b/app/src/main/kotlin/io/sakurasou/model/common/DatabaseInit.kt index bb0600b5..a9cd5ddd 100644 --- a/app/src/main/kotlin/io/sakurasou/model/common/DatabaseInit.kt +++ b/app/src/main/kotlin/io/sakurasou/model/common/DatabaseInit.kt @@ -70,6 +70,7 @@ private val groupOpsPermissions = listOf( ) private val roleOpsPermissions = listOf( + ROLE_READ_SELF, ROLE_READ_ALL ) @@ -170,6 +171,7 @@ private fun initRelation() { userOpsPermissions, groupOpsPermissions, settingOpsPermissions, + roleOpsPermissions, strategyOpsPermissions, imageOpsPermissions, albumOpsPermissions @@ -177,6 +179,7 @@ private fun initRelation() { val userPermissions = listOf( USER_READ_SELF, USER_WRITE_SELF, + ROLE_READ_SELF, IMAGE_READ_SELF_SINGLE, IMAGE_READ_SELF_ALL, IMAGE_WRITE_SELF, diff --git a/app/src/main/kotlin/io/sakurasou/plugins/Routing.kt b/app/src/main/kotlin/io/sakurasou/plugins/Routing.kt index e1108d70..22e7e89d 100644 --- a/app/src/main/kotlin/io/sakurasou/plugins/Routing.kt +++ b/app/src/main/kotlin/io/sakurasou/plugins/Routing.kt @@ -13,6 +13,7 @@ import io.ktor.server.response.* import io.ktor.server.routing.* import io.sakurasou.config.InstanceCenter.authService import io.sakurasou.config.InstanceCenter.commonService +import io.sakurasou.config.InstanceCenter.roleService import io.sakurasou.config.InstanceCenter.settingService import io.sakurasou.config.InstanceCenter.userService import io.sakurasou.controller.* @@ -50,7 +51,7 @@ fun Application.configureRouting() { settingRoute(settingService) userRoute(userService) groupRoute() - roleRoute() + roleRoute(roleService) } } }