-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/root' into root
- Loading branch information
Showing
4 changed files
with
132 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
web/backend/src/main/kotlin/dev/kordex/extra/web/routes/utils/_Permissions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?) | ||
} |