Skip to content

Commit

Permalink
feat: add validation to prevent creation of tags with admin-only tagIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
ppolariss committed Dec 14, 2024
1 parent 5db69f7 commit 3bffb33
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ var Config struct {
QQBotGroupID *int64 `env:"GROUP_ID"`
QQBotUserID *int64 `env:"USER_ID"`
QQBotUrl *string `env:"QQ_BOT_URL"`
AdminOnlyTagIds []int `env:"ADMIN_ONLY_TAG_IDS"`
}

var DynamicConfig struct {
Expand Down
16 changes: 12 additions & 4 deletions models/tag.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package models

import (
"github.com/gofiber/fiber/v2"
"fmt"
"strings"
"sync"
"time"
"treehole_next/config"
"treehole_next/utils/sensitive"

"github.com/gofiber/fiber/v2"

"github.com/opentreehole/go-common"
"github.com/rs/zerolog/log"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -65,15 +68,20 @@ func FindOrCreateTags(tx *gorm.DB, user *User, names []string) (Tags, error) {
return nil, err
}

existTagName := make([]string, 0)
existTagNames := make([]string, 0)
for _, tag := range tags {
existTagName = append(existTagName, tag.Name)
existTagNames = append(existTagNames, tag.Name)
if slices.ContainsFunc(config.Config.AdminOnlyTagIds, func(i int) bool {
return i == tag.ID
}) {
return nil, common.Forbidden(fmt.Sprintf("标签 %s 为管理员专用标签", tag.Name))
}
}

newTags := make(Tags, 0)
for _, name := range names {
name = strings.TrimSpace(name)
if !slices.ContainsFunc(existTagName, func(s string) bool {
if !slices.ContainsFunc(existTagNames, func(s string) bool {
return strings.EqualFold(s, name)
}) {
newTags = append(newTags, &Tag{Name: name})
Expand Down

0 comments on commit 3bffb33

Please sign in to comment.