Initial commit

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-15 13:57:10 +08:00
co-authored by Claude Opus 5
commit 8679200f41
1897 changed files with 257900 additions and 0 deletions
+132
View File
@@ -0,0 +1,132 @@
package rolemod
import (
"fmt"
"time"
"91porn-server/common/db"
"91porn-server/common/log"
"91porn-server/models"
"91porn-server/web/webg"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var mdb *db.MongoDB
const table = models.Role
func coll(t *db.MongoTool) *db.MongoTool {
if t == nil {
return mdb.Coll(table)
}
return t.Coll(table)
}
// initIndex 初始化索引
func initIndex() {
many := []mongo.IndexModel{
{
Keys: bson.D{{Key: "path", Value: 1}, {Key: "host", Value: 1}, {Key: "method", Value: 1}},
Options: options.Index().SetUnique(true).SetSparse(true),
},
}
if _, err := coll(nil).CreateIndex(many); err != nil {
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
}
}
// 获取全部Rules
func Rules() (data []RoleResp, err error) {
if err = coll(nil).Find(&data, bson.M{}); err != nil {
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "List", table, "Find", err))
return
}
return data, nil
}
func RoleAll(t *db.MongoTool, role string) (data []RoleResp, err error) {
if err = coll(t).Find(&data, bson.M{"authorized_roles": role}); err != nil {
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "List", table, "Find", err))
return
}
return
}
func UpdateRole(inc []Incr) error {
return webg.VideoDB.Trans(func(tool *db.MongoTool) error {
//先清除角色权限
if err := RemoveRole(tool, inc[0].AuthorizedRoles); err != nil {
return err
}
return InsertRole(tool, inc)
})
}
// 去除原先的角色权限
func RemoveRole(t *db.MongoTool, role string) error {
res, err := RoleAll(t, role)
if err != nil {
return err
}
write := make([]mongo.WriteModel, len(res))
opts := options.BulkWrite()
opts.SetOrdered(false)
for i, v := range res {
filter := bson.M{
"_id": v.ID,
}
update := bson.M{
"$pull": bson.M{"authorized_roles": role, "forbidden_roles": role},
}
write[i] = mongo.NewUpdateOneModel().SetFilter(filter).SetUpdate(update)
}
if _, err = coll(t).Bulk(write, opts); err != nil {
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "Insert", err), log.Any("role", role))
return err
}
return nil
}
// InsertRole 插入一条数据
func InsertRole(t *db.MongoTool, inc []Incr) error {
write := make([]mongo.WriteModel, len(inc))
opts := options.BulkWrite()
opts.SetOrdered(false)
for i, v := range inc {
if v.Method == "" {
v.Method = "*"
}
if v.Host == "" {
v.Host = "*"
}
filter := bson.M{"id": v.RoleID, "host": v.Host, "path": v.Path, "method": v.Method}
update := bson.M{
"$setOnInsert": bson.M{
"createdAt": time.Now(),
"id": v.RoleID,
"host": v.Host,
"path": v.Path,
"method": v.Method,
},
"$set": bson.M{
"updatedAt": time.Now(),
},
"$addToSet": bson.M{
"authorized_roles": v.AuthorizedRoles,
"forbidden_roles": v.ForbiddenRoles,
},
}
write[i] = mongo.NewUpdateOneModel().
SetUpsert(true).
SetFilter(filter).
SetUpdate(update)
}
if _, err := coll(t).Bulk(write, opts); err != nil {
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "Insert", err), log.Any("inc", inc))
return err
}
return nil
}