完善权限系统
This commit is contained in:
@@ -16,6 +16,7 @@ import ink.snowflake.server.utils.TokenUtils.generateRefreshToken
|
||||
import ink.snowflake.server.utils.TokenUtils.getUserIdByToken
|
||||
import ink.snowflake.server.utils.dao.AIDao
|
||||
import ink.snowflake.server.utils.dao.UserDAO
|
||||
import ink.snowflake.server.utils.dao.UserDAO.getAvailableById
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.auth.*
|
||||
import io.ktor.server.request.*
|
||||
@@ -39,7 +40,7 @@ import kotlin.text.Charsets.UTF_8
|
||||
// 配置和初始化 Redis 客户端
|
||||
fun setupRedis(): RedissonClient {
|
||||
val config = Config()
|
||||
config.useSingleServer().setAddress("redis://"+LOCAL_IP+":6379")
|
||||
config.useSingleServer().setAddress("redis://" + LOCAL_IP + ":6379")
|
||||
return Redisson.create(config)
|
||||
}
|
||||
|
||||
@@ -60,19 +61,24 @@ fun Application.User(config: AppConfig) {
|
||||
BaseResponse(status = false, message = "尚未注册", data = null)
|
||||
} else {
|
||||
val userPassword = UserDAO.getPasswordById(userId)
|
||||
// 验证密码
|
||||
if (password == userPassword) {
|
||||
// 登录成功
|
||||
BaseResponse(
|
||||
status = true, data = LoginResponse(
|
||||
userId,
|
||||
generateAccessToken(config, userId),
|
||||
generateRefreshToken(config, userId)
|
||||
if (getAvailableById(userId)) {
|
||||
|
||||
// 验证密码
|
||||
if (password == userPassword) {
|
||||
// 登录成功
|
||||
BaseResponse(
|
||||
status = true, data = LoginResponse(
|
||||
userId,
|
||||
generateAccessToken(config, userId),
|
||||
generateRefreshToken(config, userId)
|
||||
)
|
||||
)
|
||||
)
|
||||
} else {
|
||||
// 账号密码不匹配
|
||||
BaseResponse(status = false, message = "账号密码不匹配,请重新登录", data = null)
|
||||
}
|
||||
} else {
|
||||
// 账号密码不匹配
|
||||
BaseResponse(status = false, message = "账号密码不匹配,请重新登录", data = null)
|
||||
BaseResponse(status = false, message = "账号已被禁用,请联系管理员", data = null)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
@@ -3,8 +3,11 @@ package ink.snowflake.server.utils.dao
|
||||
import ink.snowflake.server.model.database.UsersTable
|
||||
import ink.snowflake.server.model.response.UserInfoResponse
|
||||
import kotlinx.datetime.Clock
|
||||
import kotlinx.html.Entities
|
||||
import org.jetbrains.exposed.v1.core.SqlExpressionBuilder.eq
|
||||
import org.jetbrains.exposed.v1.datetime.timestampLiteral
|
||||
import org.jetbrains.exposed.v1.jdbc.insertAndGetId
|
||||
import org.jetbrains.exposed.v1.jdbc.select
|
||||
import org.jetbrains.exposed.v1.jdbc.selectAll
|
||||
import org.jetbrains.exposed.v1.jdbc.transactions.transaction
|
||||
import org.jetbrains.exposed.v1.json.json
|
||||
@@ -25,6 +28,19 @@ object UserDAO {
|
||||
.singleOrNull() // 如果没有找到用户,返回 null
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 根据 email 获取密码
|
||||
* @return 密码的 SHA-256 哈希值
|
||||
*/
|
||||
fun getAvailableById(id: UUID): Boolean {
|
||||
return transaction {
|
||||
UsersTable
|
||||
.selectAll().where { UsersTable.id eq id }
|
||||
.map { it[UsersTable.isActive] }
|
||||
.singleOrNull() ?: false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据 email 获取用户 ID
|
||||
|
||||
Reference in New Issue
Block a user