Skip to content

Commit

Permalink
Add route predicate for denying access, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
gdude2002 committed Jul 6, 2024
1 parent 195386b commit 39522bc
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 10 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,19 @@ jobs:
run: echo ::set-output name=NAME::${GITHUB_REF#refs/tags/}

- name: Create Release
uses: softprops/action-gh-release@v1
uses: softprops/action-gh-release@2

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

with:
body_path: release.md
files: build/libs/*.jar
name: Release ${{ steps.get_tag.outputs.NAME }}

files: |
build/libs/*.jar
**/build/libs/*.jar
- name: Release webhook
run: kotlin .github/release.main.kts

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ package dev.kordex.extra.web.routes

import dev.kordex.extra.web.errors.MethodNotAllowedError
import dev.kordex.extra.web.errors.error
import dev.kordex.extra.web.routes.utils.allow
import io.ktor.server.application.*

@Suppress("StringLiteralDuplication")
public abstract class Route(public val extension: String) {
public abstract val path: String

public open suspend fun beforeRequest(verb: Verb, call: ApplicationCall): Boolean =
call.allow()

public open suspend fun delete(call: ApplicationCall) {
call.error(MethodNotAllowedError)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ public class RouteRegistry : KordExKoinComponent {
val route = routes[path]
?: return call.respond(HttpStatusCode.NotFound)

when (verb) {
Verb.DELETE -> route.delete(call)
Verb.GET -> route.get(call)
Verb.HEAD -> route.head(call)
Verb.OPTIONS -> route.options(call)
Verb.PATCH -> route.patch(call)
Verb.POST -> route.post(call)
Verb.PUT -> route.put(call)
if (route.beforeRequest(verb, call)) {
when (verb) {
Verb.DELETE -> route.delete(call)
Verb.GET -> route.get(call)
Verb.HEAD -> route.head(call)
Verb.OPTIONS -> route.options(call)
Verb.PATCH -> route.patch(call)
Verb.POST -> route.post(call)
Verb.PUT -> route.put(call)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

package dev.kordex.extra.web.routes.utils

import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent
import dev.kord.common.entity.Permission
import dev.kord.common.entity.Permissions
import dev.kord.common.entity.Snowflake
import dev.kord.core.Kord
import dev.kord.core.entity.Guild
import dev.kord.core.entity.Role
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.ApplicationCall
import io.ktor.server.response.respond
import kotlinx.serialization.Serializable
import org.koin.core.component.inject
import kotlin.collections.map
import kotlin.collections.toTypedArray

@Suppress("FunctionOnlyReturningConstant")
public suspend fun ApplicationCall.allow(): Boolean =
true

public suspend fun ApplicationCall.deny(body: DenyBuilder.() -> Unit): Boolean {
response.status(HttpStatusCode.Unauthorized)

val builder = DenyBuilder()

body(builder)
respond(builder)

return false
}

@Serializable
public class DenyBuilder : KordExKoinComponent {
private val kord: Kord by inject()

public var reasonKey: String? = null
public var reasonPlaceholders: Array<String> = arrayOf()

public var missingGuilds: MutableList<EntityInfo>? = null
public var missingPermissions: Permissions? = null
public var missingRoles: MutableList<EntityInfo>? = null

public fun reason(key: String, placeholders: Array<Any> = arrayOf()) {
reasonKey = key

reasonPlaceholders = placeholders
.map { it.toString() }
.toTypedArray()
}

public fun missingGuild(guild: Guild) {
if (missingGuilds == null) {
missingGuilds = mutableListOf()
}

missingGuilds!!.add(EntityInfo(guild.id, guild.name))
}

public suspend fun missingGuild(id: Snowflake) {
if (missingGuilds == null) {
missingGuilds = mutableListOf()
}

val guild = kord.getGuildOrNull(id)

missingGuilds!!.add(EntityInfo(id, guild?.name))
}

public fun missingPermission(perm: Permission) {
if (missingPermissions == null) {
missingPermissions = Permissions()
}

missingPermissions = missingPermissions!! + perm
}

public suspend fun missingPermissions(perms: Permissions) {
if (missingPermissions == null) {
missingPermissions = Permissions()
}

missingPermissions = missingPermissions!! + perms
}

public fun missingRole(role: Role) {
if (missingRoles == null) {
missingRoles = mutableListOf()
}

missingRoles!!.add(EntityInfo(role.id, role.name))
}

public suspend fun missingRole(guildId: Snowflake, id: Snowflake) {
if (missingRoles == null) {
missingRoles = mutableListOf()
}

val guild = kord.getGuildOrNull(guildId)
val role = guild?.getRoleOrNull(id)

missingRoles!!.add(EntityInfo(id, role?.name))
}

@Serializable
public data class EntityInfo(val id: Snowflake, val name: String?)
}

0 comments on commit 39522bc

Please sign in to comment.