Skip to content

Commit

Permalink
feat: complete roleController
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiinaKin committed Sep 18, 2024
1 parent 7bab4f5 commit bed6b04
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
52 changes: 49 additions & 3 deletions app/src/main/kotlin/io/sakurasou/controller/RoleController.kt
Original file line number Diff line number Diff line change
@@ -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
}
Expand 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<List<RoleVO>> {
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<String, RoleVO> {
return roleService.listRolesWithPermissions()
}

suspend fun handleListAllRolesWithPermissionsOfUser(roles: List<String>): Map<String, RoleVO> {
return roleService.listRolesWithPermissionsOfUser(roles)
}
}
2 changes: 1 addition & 1 deletion app/src/main/kotlin/io/sakurasou/controller/vo/CommonVO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ data class CommonResponse<T>(
constructor(code: Int, message: String) : this(code, message, null)

companion object {
fun <T> success(data: T): CommonResponse<T> {
inline fun <reified T> success(data: T): CommonResponse<T> {
return CommonResponse(0, "success", data)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ suspend fun ApplicationCall.success() {
respond(CommonResponse.success(Unit))
}

suspend fun <T> ApplicationCall.success(data: T) {
suspend inline fun <reified T> ApplicationCall.success(data: T) {
respond(CommonResponse.success(data))
}

Expand Down
3 changes: 3 additions & 0 deletions app/src/main/kotlin/io/sakurasou/model/common/DatabaseInit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ private val groupOpsPermissions = listOf(
)

private val roleOpsPermissions = listOf(
ROLE_READ_SELF,
ROLE_READ_ALL
)

Expand Down Expand Up @@ -170,13 +171,15 @@ private fun initRelation() {
userOpsPermissions,
groupOpsPermissions,
settingOpsPermissions,
roleOpsPermissions,
strategyOpsPermissions,
imageOpsPermissions,
albumOpsPermissions
).flatten()
val userPermissions = listOf(
USER_READ_SELF,
USER_WRITE_SELF,
ROLE_READ_SELF,
IMAGE_READ_SELF_SINGLE,
IMAGE_READ_SELF_ALL,
IMAGE_WRITE_SELF,
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/kotlin/io/sakurasou/plugins/Routing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -50,7 +51,7 @@ fun Application.configureRouting() {
settingRoute(settingService)
userRoute(userService)
groupRoute()
roleRoute()
roleRoute(roleService)
}
}
}
Expand Down

0 comments on commit bed6b04

Please sign in to comment.