Bot related bot key apis

This commit is contained in:
LittleSheep 2024-08-25 20:51:58 +08:00
parent 476ef57236
commit 2b2e7b5a89
4 changed files with 41 additions and 9 deletions

10
.idea/workspace.xml generated
View File

@ -4,9 +4,11 @@
<option name="autoReloadType" value="ALL" />
</component>
<component name="ChangeListManager">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Fix compare perm node panic">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Fix compare perm node function">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/services/perms.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/perms.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/server/api/bot_token_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/bot_token_api.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/server/api/bots_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/bots_api.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/server/api/index.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/index.go" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -153,7 +155,6 @@
</option>
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value=":poop: Remove mis-imported cgo" />
<MESSAGE value=":bug: Bug fixes" />
<MESSAGE value=":bug: Fix oauth ticket need mfa" />
<MESSAGE value=":arrow_up: Support new auth api" />
@ -178,7 +179,8 @@
<MESSAGE value=":sparkles: Pagination bots api" />
<MESSAGE value=":bug: Fix query issue" />
<MESSAGE value=":bug: Fix compare perm node panic" />
<option name="LAST_COMMIT_MESSAGE" value=":bug: Fix compare perm node panic" />
<MESSAGE value=":bug: Fix compare perm node function" />
<option name="LAST_COMMIT_MESSAGE" value=":bug: Fix compare perm node function" />
</component>
<component name="VgoProject">
<settings-migrated>true</settings-migrated>

View File

@ -1,11 +1,13 @@
package api
import (
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
)
func listBotKeys(c *fiber.Ctx) error {
@ -14,7 +16,18 @@ func listBotKeys(c *fiber.Ctx) error {
}
user := c.Locals("user").(models.Account)
tx := database.C.Where("account_id = ?", user.ID)
var tx *gorm.DB
botId, _ := c.ParamsInt("botId", 0)
if botId > 0 {
var bot models.Account
if err := database.C.Where("automated_id = ? AND id = ?", user.ID, botId).First(&bot).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("bot not found: %v", err))
}
tx = database.C.Where("account_id = ?", bot.ID)
} else {
tx = database.C.Where("account_id = ?", user.ID)
}
countTx := tx
var count int64
@ -66,7 +79,18 @@ func createBotKey(c *fiber.Ctx) error {
return err
}
key, err := services.NewApiKey(user, models.ApiKey{
target := user
botId, _ := c.ParamsInt("botId", 0)
if botId > 0 {
var bot models.Account
if err := database.C.Where("automated_id = ? AND id = ?", user.ID, botId).First(&bot).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("bot not found: %v", err))
}
target = bot
}
key, err := services.NewApiKey(target, models.ApiKey{
Name: data.Name,
Description: data.Description,
Lifecycle: data.Lifecycle,

View File

@ -86,7 +86,7 @@ func deleteBot(c *fiber.Ctx) error {
}
user := c.Locals("user").(models.Account)
id, _ := c.ParamsInt("id", 0)
id, _ := c.ParamsInt("botId", 0)
var bot models.Account
if err := database.C.Where("id = ? AND automated_id = ?", id, user.ID).First(&bot).Error; err != nil {

View File

@ -105,10 +105,16 @@ func MapAPIs(app *fiber.App, baseURL string) {
{
bots.Get("/", listBots)
bots.Post("/", createBot)
bots.Delete("/:id", deleteBot)
bots.Delete("/:botId", deleteBot)
keys := bots.Group("/keys").Name("Bots' Keys")
{
keys.Get("/", listBotKeys)
keys.Post("/", createBotKey)
}
}
keys := developers.Group("/keys").Name("Keys")
keys := developers.Group("/keys").Name("Own Bots' Keys")
{
keys.Get("/", listBotKeys)
keys.Get("/:id", getBotKey)