Skip to content

Commit

Permalink
perf: 修改函数,命名
Browse files Browse the repository at this point in the history
  • Loading branch information
nullaqua committed Sep 11, 2024
1 parent ce70596 commit 26586f0
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 44 deletions.
8 changes: 4 additions & 4 deletions src/main/kotlin/subit/database/Permissions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ interface Permissions
suspend fun getPermission(block: BlockId, user: UserId): PermissionLevel
}

inline fun <reified T> Context.checkPermission(
inline fun <reified T> Context.withPermission(
user: DatabaseUser? = getLoginUser()?.toDatabaseUser(),
body: CheckPermissionInContextScope.()->T
): T = CheckPermissionInContextScope(this, user).body()

inline fun <reified T> checkPermission(
inline fun <reified T> withPermission(
user: DatabaseUser?,
body: CheckPermissionScope.()->T
): T = CheckPermissionScope(user).body()
Expand Down Expand Up @@ -143,7 +143,7 @@ open class CheckPermissionScope @PublishedApi internal constructor(val user: Dat
}
val selfPermission = getPermission(block.id)
if (selfPermission < PermissionLevel.ADMIN) return false
val otherPermission = checkPermission(other) { getPermission(block.id) }
val otherPermission = withPermission(other) { getPermission(block.id) }
return selfPermission > otherPermission && selfPermission > permission
}
}
Expand Down Expand Up @@ -271,7 +271,7 @@ class CheckPermissionInContextScope @PublishedApi internal constructor(val conte
message = "修改他人在板块${block.name}的权限要求拥有该板块管理员权限"
)
)
val otherPermission = checkPermission(other) { getPermission(block.id) }
val otherPermission = withPermission(other) { getPermission(block.id) }
if (selfPermission > otherPermission && selfPermission > permission)
return
else return finish(
Expand Down
5 changes: 2 additions & 3 deletions src/main/kotlin/subit/router/Admin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ private data class ProhibitUser(val id: UserId, val prohibit: Boolean, val time:

private suspend fun Context.prohibitUser()
{
val users = get<Users>()
val prohibits = get<Prohibits>()
val operations = get<Operations>()
val loginUser = getLoginUser() ?: return call.respond(HttpStatus.Unauthorized)
Expand All @@ -94,7 +93,7 @@ private suspend fun Context.prohibitUser()

private suspend fun Context.prohibitList()
{
checkPermission { checkHasGlobalAdmin() }
withPermission { checkHasGlobalAdmin() }
val (begin, count) = call.getPage()
call.respond(HttpStatus.OK, get<Prohibits>().getProhibitList(begin, count))
}
Expand All @@ -108,7 +107,7 @@ private suspend fun Context.changePermission()
val loginUser = getLoginUser() ?: return call.respond(HttpStatus.Unauthorized)
val changePermission = receiveAndCheckBody<ChangePermission>()
val user = SSO.getDbUser(changePermission.id) ?: return call.respond(HttpStatus.NotFound)
checkPermission { checkChangePermission(null, user, changePermission.permission) }
withPermission { checkChangePermission(null, user, changePermission.permission) }
users.changePermission(changePermission.id, changePermission.permission)
get<Operations>().addOperation(loginUser.id, changePermission)
if (loginUser.id != changePermission.id) get<Notices>().createNotice(
Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/subit/router/BannedWords.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import io.ktor.server.application.*
import io.ktor.server.routing.*
import kotlinx.serialization.Serializable
import subit.database.BannedWords
import subit.database.checkPermission
import subit.database.withPermission
import subit.database.receiveAndCheckBody
import subit.router.*
import subit.utils.HttpStatus
Expand Down Expand Up @@ -89,7 +89,7 @@ private suspend fun Context.getBannedWords()
{
val (begin, count) = call.getPage()
val bannedWords = get<BannedWords>()
checkPermission { checkHasGlobalAdmin() }
withPermission { checkHasGlobalAdmin() }
call.respond(HttpStatus.OK, bannedWords.getBannedWords(begin, count))
}

Expand All @@ -100,7 +100,7 @@ private suspend fun Context.newBannedWord()
{
val newBannedWord = receiveAndCheckBody<NewBannedWord>()
val bannedWords = get<BannedWords>()
checkPermission { checkHasGlobalAdmin() }
withPermission { checkHasGlobalAdmin() }
bannedWords.addBannedWord(newBannedWord.word)
call.respond(HttpStatus.OK)
}
Expand All @@ -109,7 +109,7 @@ private suspend fun Context.deleteBannedWord()
{
val word = call.parameters["word"] ?: return call.respond(HttpStatus.BadRequest)
val bannedWords = get<BannedWords>()
checkPermission { checkHasGlobalAdmin() }
withPermission { checkHasGlobalAdmin() }
bannedWords.removeBannedWord(word)
call.respond(HttpStatus.OK)
}
Expand All @@ -119,7 +119,7 @@ private suspend fun Context.editBannedWord()
val word = call.parameters["word"] ?: return call.respond(HttpStatus.BadRequest)
val newBannedWord = receiveAndCheckBody<NewBannedWord>()
val bannedWords = get<BannedWords>()
checkPermission { checkHasGlobalAdmin() }
withPermission { checkHasGlobalAdmin() }
bannedWords.updateBannedWord(word, newBannedWord.word)
call.respond(HttpStatus.OK)
}
18 changes: 9 additions & 9 deletions src/main/kotlin/subit/router/Block.kt
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ private suspend fun Context.newBlock()
val blocks = get<Blocks>()
if (newBlock.parent != null)
{
checkPermission { checkHasAdminIn(newBlock.parent) }
withPermission { checkHasAdminIn(newBlock.parent) }
blocks.getBlock(newBlock.parent) ?: return call.respond(HttpStatus.BadRequest)
}
else checkPermission { checkHasGlobalAdmin() }
else withPermission { checkHasGlobalAdmin() }
val id = blocks.createBlock(
name = newBlock.name,
description = newBlock.description,
Expand Down Expand Up @@ -201,7 +201,7 @@ private suspend fun Context.editBlockInfo()
val loginUser = getLoginUser() ?: return call.respond(HttpStatus.Unauthorized)
val id = call.parameters["id"]?.toBlockIdOrNull() ?: return call.respond(HttpStatus.BadRequest)
val editBlockInfo = receiveAndCheckBody<EditBlockInfo>()
checkPermission { checkHasAdminIn(id) }
withPermission { checkHasAdminIn(id) }
get<Blocks>().setPermission(
block = id,
posting = editBlockInfo.postingPermission,
Expand All @@ -217,15 +217,15 @@ private suspend fun Context.getBlockInfo()
{
val id = call.parameters["id"]?.toBlockIdOrNull() ?: return call.respond(HttpStatus.BadRequest)
val block = get<Blocks>().getBlock(id) ?: return call.respond(HttpStatus.NotFound)
checkPermission { checkCanRead(block) }
withPermission { checkCanRead(block) }
call.respond(HttpStatus.OK, block)
}

private suspend fun Context.deleteBlock()
{
val loginUser = getLoginUser() ?: return call.respond(HttpStatus.Unauthorized)
val id = call.parameters["id"]?.toBlockIdOrNull() ?: return call.respond(HttpStatus.BadRequest)
checkPermission { checkHasAdminIn(id) }
withPermission { checkHasAdminIn(id) }
val blocks = get<Blocks>()
val block = blocks.getBlock(id) ?: return call.respond(HttpStatus.NotFound)
blocks.setState(id, State.DELETED)
Expand All @@ -252,7 +252,7 @@ private suspend fun Context.changePermission()
val changePermission = receiveAndCheckBody<ChangePermission>()
val block = get<Blocks>().getBlock(changePermission.block) ?: return call.respond(HttpStatus.NotFound)
val user = SSO.getDbUser(changePermission.user) ?: return call.respond(HttpStatus.NotFound)
checkPermission { checkChangePermission(block, user, changePermission.permission) }
withPermission { checkChangePermission(block, user, changePermission.permission) }
get<Permissions>().setPermission(
bid = changePermission.block,
uid = changePermission.user,
Expand All @@ -275,11 +275,11 @@ private suspend fun Context.getPermission()
val uid = call.parameters["user"]?.toUserIdOrNull() ?: return call.respond(HttpStatus.BadRequest)
val blocks = get<Blocks>()
val user = SSO.getDbUser(uid) ?: return call.respond(HttpStatus.NotFound)
checkPermission {
withPermission {
checkCanRead(blocks.getBlock(bid) ?: return call.respond(HttpStatus.NotFound))
checkHasAdminIn(bid)
}
call.respond(HttpStatus.OK, checkPermission(user) { getPermission(bid) })
call.respond(HttpStatus.OK, withPermission(user) { getPermission(bid) })
}

private suspend fun Context.getChildren()
Expand All @@ -289,7 +289,7 @@ private suspend fun Context.getChildren()
val (begin, count) = call.getPage()
val blocks = get<Blocks>()

checkPermission()
withPermission()
{
val block = id?.let { blocks.getBlock(it) }
if (block != null) checkCanRead(block)
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/subit/router/Comment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private suspend fun Context.commentPost()
val posts = get<Posts>()

val parent = posts.getPostInfo(postId) ?: return call.respond(HttpStatus.NotFound)
checkPermission { checkCanComment(parent) }
withPermission { checkCanComment(parent) }
val commentId = posts.createPost(parent = postId, author = loginUser.id, block = parent.block, anonymous = newComment.anonymous) ?: return call.respond(HttpStatus.NotFound)
if (newComment.wordMarking != null)
{
Expand Down Expand Up @@ -156,7 +156,7 @@ private suspend fun Context.getPostComments()
val (begin, count) = call.getPage()
val posts = get<Posts>()
val post = posts.getPostInfo(postId) ?: return call.respond(HttpStatus.NotFound)
checkPermission { checkCanRead(post) }
withPermission { checkCanRead(post) }
val comments = posts.getChildPosts(postId, type, begin, count)
if (getLoginUser().hasGlobalAdmin())
call.respond(HttpStatus.OK, comments)
Expand All @@ -173,7 +173,7 @@ private suspend fun Context.getCommentComments()
val (begin, count) = call.getPage()
val posts = get<Posts>()
val comment = posts.getPostInfo(commentId) ?: return call.respond(HttpStatus.NotFound)
checkPermission { checkCanRead(comment) }
withPermission { checkCanRead(comment) }
val comments = posts.getDescendants(commentId, type, begin, count)
if (getLoginUser().hasGlobalAdmin())
call.respond(HttpStatus.OK, comments)
Expand All @@ -186,6 +186,6 @@ private suspend fun Context.getComment()
val commentId = call.parameters["commentId"]?.toPostIdOrNull() ?: return call.respond(HttpStatus.BadRequest)
val posts = get<Posts>()
val comment = posts.getPostFull(commentId) ?: return call.respond(HttpStatus.NotFound)
checkPermission { checkCanRead(comment.toPostInfo()) }
withPermission { checkCanRead(comment.toPostInfo()) }
call.respond(HttpStatus.OK, if (comment.anonymous) comment.copy(author = UserId(0)) else comment)
}
2 changes: 1 addition & 1 deletion src/main/kotlin/subit/router/Files.kt
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ private suspend fun Context.changePermission()
val loginUser = getLoginUser() ?: return call.respond(HttpStatus.Unauthorized)
val changePermission = receiveAndCheckBody<ChangePermission>()
val user = SSO.getDbUser(changePermission.id) ?: return call.respond(HttpStatus.NotFound)
checkPermission { checkChangePermission(null, user, changePermission.filePermission) }
withPermission { checkChangePermission(null, user, changePermission.filePermission) }
get<Users>().changeFilePermission(changePermission.id, changePermission.filePermission)
get<Operations>().addOperation(loginUser.id, changePermission)
call.respond(HttpStatus.OK)
Expand Down
18 changes: 9 additions & 9 deletions src/main/kotlin/subit/router/Posts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ private suspend fun Context.getPost()
val id = call.parameters["id"]?.toPostIdOrNull() ?: return call.respond(HttpStatus.BadRequest)
val postFull = get<Posts>().getPostFull(id) ?: return call.respond(HttpStatus.NotFound)
val loginUser = getLoginUser()
checkPermission { checkCanRead(postFull.toPostInfo()) }
withPermission { checkCanRead(postFull.toPostInfo()) }
if (!postFull.anonymous) call.respond(HttpStatus.OK, postFull) // 若不是匿名帖则直接返回
else if (loginUser == null || loginUser.permission < PermissionLevel.ADMIN) call.respond(
HttpStatus.OK,
Expand Down Expand Up @@ -397,7 +397,7 @@ private suspend fun Context.deletePost()
val id = call.parameters["id"]?.toPostIdOrNull() ?: return call.respond(HttpStatus.BadRequest)
val post = get<Posts>().getPostInfo(id) ?: return call.respond(HttpStatus.NotFound)
val loginUser = getLoginUser() ?: return call.respond(HttpStatus.Unauthorized)
checkPermission { checkCanDelete(post) }
withPermission { checkCanDelete(post) }
get<Posts>().setPostState(id, State.DELETED)
if (post.author != loginUser.id) get<Notices>().createNotice(
Notice.makeSystemNotice(
Expand Down Expand Up @@ -426,7 +426,7 @@ private suspend fun Context.likePost()
val post = get<Posts>().getPostInfo(id) ?: return call.respond(HttpStatus.NotFound)
val type = receiveAndCheckBody<LikePost>().type
val loginUser = getLoginUser() ?: return call.respond(HttpStatus.Unauthorized)
checkPermission { checkCanRead(post) }
withPermission { checkCanRead(post) }
when (type)
{
LikeType.LIKE -> get<Likes>().like(loginUser.id, id)
Expand Down Expand Up @@ -460,10 +460,10 @@ private suspend fun Context.newPost()
val loginUser = getLoginUser() ?: return call.respond(HttpStatus.Unauthorized)

val block = get<Blocks>().getBlock(newPost.block) ?: return call.respond(HttpStatus.NotFound)
checkPermission { checkCanPost(block) }
withPermission { checkCanPost(block) }

if (newPost.anonymous) checkPermission { checkCanAnonymous(block) }
if (newPost.top) checkPermission { checkHasAdminIn(block.id) }
if (newPost.anonymous) withPermission { checkCanAnonymous(block) }
if (newPost.top) withPermission { checkHasAdminIn(block.id) }
val id = get<Posts>().createPost(
author = loginUser.id,
anonymous = newPost.anonymous,
Expand Down Expand Up @@ -495,7 +495,7 @@ private suspend fun Context.getBlockPosts()
{
val block = call.parameters["block"]?.toBlockIdOrNull() ?: return call.respond(HttpStatus.BadRequest)
val blockFull = get<Blocks>().getBlock(block) ?: return call.respond(HttpStatus.NotFound)
checkPermission { checkCanRead(blockFull) }
withPermission { checkCanRead(blockFull) }
val type = call.parameters["sort"]
?.runCatching { Posts.PostListSort.valueOf(this) }
?.getOrNull() ?: return call.respond(HttpStatus.BadRequest)
Expand All @@ -508,7 +508,7 @@ private suspend fun Context.getBlockTopPosts()
{
val block = call.parameters["block"]?.toBlockIdOrNull() ?: return call.respond(HttpStatus.BadRequest)
val blockFull = get<Blocks>().getBlock(block) ?: return call.respond(HttpStatus.NotFound)
checkPermission { checkCanRead(blockFull) }
withPermission { checkCanRead(blockFull) }
val (begin, count) = call.getPage()
val posts = get<Posts>().getBlockTopPosts(block, begin, count)
call.respond(HttpStatus.OK, posts)
Expand All @@ -519,7 +519,7 @@ private suspend fun Context.setBlockTopPosts()
val pid = call.parameters["id"]?.toPostIdOrNull() ?: return call.respond(HttpStatus.BadRequest)
val top = call.parameters["top"]?.toBooleanStrictOrNull() ?: return call.respond(HttpStatus.BadRequest)
val postInfo = get<Posts>().getPostInfo(pid) ?: return call.respond(HttpStatus.NotFound)
checkPermission {
withPermission {
checkCanRead(postInfo)
checkHasAdminIn(postInfo.block)
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/subit/router/Report.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import subit.JWTAuth.getLoginUser
import subit.dataClasses.*
import subit.dataClasses.ReportId.Companion.toReportIdOrNull
import subit.database.Reports
import subit.database.checkPermission
import subit.database.withPermission
import subit.database.receiveAndCheckBody
import subit.router.*
import subit.utils.HttpStatus
Expand Down Expand Up @@ -114,7 +114,7 @@ private suspend fun Context.newReport()

private suspend fun Context.getReports()
{
checkPermission { checkHasGlobalAdmin() }
withPermission { checkHasGlobalAdmin() }
val begin = call.parameters["begin"]?.toLongOrNull() ?: return call.respond(HttpStatus.BadRequest)
val count = call.parameters["count"]?.toIntOrNull() ?: return call.respond(HttpStatus.BadRequest)
val handled = when (call.parameters["filter"])
Expand All @@ -129,15 +129,15 @@ private suspend fun Context.getReports()

private suspend fun Context.getReport()
{
checkPermission { checkHasGlobalAdmin() }
withPermission { checkHasGlobalAdmin() }
val id = call.parameters["id"]?.toReportIdOrNull() ?: return call.respond(HttpStatus.BadRequest)
val report = get<Reports>().getReport(id) ?: return call.respond(HttpStatus.NotFound)
call.respond(HttpStatus.OK, report)
}

private suspend fun Context.handleReport()
{
checkPermission { checkHasGlobalAdmin() }
withPermission { checkHasGlobalAdmin() }
val loginUser = getLoginUser() ?: return call.respond(HttpStatus.Unauthorized)
val id = call.parameters["id"]?.toReportIdOrNull() ?: return call.respond(HttpStatus.BadRequest)
get<Reports>().handleReport(id, loginUser.id)
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/subit/router/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private suspend fun Context.changeIntroduction()
}
else
{
checkPermission { checkHasGlobalAdmin() }
withPermission { checkHasGlobalAdmin() }
if (get<Users>().changeIntroduction(id, changeIntroduction.introduction))
{
get<Operations>().addOperation(loginUser.id, changeIntroduction)
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/subit/router/WordMarkings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import subit.dataClasses.WordMarkingInfo
import subit.database.PostVersions
import subit.database.Posts
import subit.database.WordMarkings
import subit.database.checkPermission
import subit.database.withPermission
import subit.router.Context
import subit.router.get
import subit.utils.HttpStatus
Expand Down Expand Up @@ -83,7 +83,7 @@ suspend fun Context.getWordMarkings()
val postVersions = get<PostVersions>()
val postVersion = postVersions.getPostVersion(postVersionId) ?: return call.respond(HttpStatus.NotFound)
val post = get<Posts>().getPostInfo(postVersion.post) ?: return call.respond(HttpStatus.NotFound)
checkPermission { checkCanRead(post) }
withPermission { checkCanRead(post) }
val wordMarkings = get<WordMarkings>().getWordMarkings(postVersionId)
return call.respond(HttpStatus.OK, wordMarkings)
}
Expand All @@ -95,7 +95,7 @@ suspend fun Context.getWordMarking()
val postVersions = get<PostVersions>()
val postVersion = postVersions.getPostVersion(postVersionId) ?: return call.respond(HttpStatus.NotFound)
val post = get<Posts>().getPostInfo(postVersion.post) ?: return call.respond(HttpStatus.NotFound)
checkPermission { checkCanRead(post) }
withPermission { checkCanRead(post) }
val wordMarking = get<WordMarkings>().getWordMarking(postVersionId, commentId) ?: return call.respond(HttpStatus.NotFound)
return call.respond(HttpStatus.OK, wordMarking)
}
Expand All @@ -106,6 +106,6 @@ suspend fun Context.getWordMarkingById()
val wordMarking = get<WordMarkings>().getWordMarking(WordMarkingId(id)) ?: return call.respond(HttpStatus.NotFound)
val postVersion = get<PostVersions>().getPostVersion(wordMarking.postVersion) ?: return call.respond(HttpStatus.NotFound)
val post = get<Posts>().getPostInfo(postVersion.post) ?: return call.respond(HttpStatus.NotFound)
checkPermission { checkCanRead(post) }
withPermission { checkCanRead(post) }
return call.respond(HttpStatus.OK, wordMarking)
}

0 comments on commit 26586f0

Please sign in to comment.