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
}
+51
View File
@@ -0,0 +1,51 @@
package rolemod
import (
"time"
"91porn-server/common/db"
"go.mongodb.org/mongo-driver/bson/primitive"
)
// 用于记录权限配置
type RoleConf struct {
ID primitive.ObjectID `json:"_id" bson:"_id,omitempty"`
RoleID int `json:"id" bson:"id,omitempty"`
Host string `json:"host" bson:"host"` // Host 定义资源的Host,允许使用增强的通配符。
Path string `json:"path" bson:"path"` // Path 定义资源的Path,允许使用增强的通配符。
Method string `json:"method" bson:"method"` // Method 定义资源的Method,允许使用增强的通配符。
AuthorizedRoles []string `json:"authorized_roles" bson:"authorized_roles"` // AuthorizedRoles定义允许访问资源的角色
ForbiddenRoles []string `json:"forbidden_roles" bson:"forbidden_roles"`
AllowAnyone bool `json:"allow_anyone" bson:"allow_anyone"`
CreatedAt time.Time `json:"createdAt" bson:"createdAt"`
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"`
}
// Incr 插入结构体
type Incr struct {
RoleID int `json:"id" bson:"id"`
Host string `json:"host" bson:"host"` // Host 定义资源的Host,允许使用增强的通配符。
Path string `json:"path" bson:"path" binding:"required"` // Path 定义资源的Path,允许使用增强的通配符。
Method string `json:"method" bson:"method"` // Method 定义资源的Method,允许使用增强的通配符。
AuthorizedRoles string `json:"authorized_roles" bson:"authorized_roles" binding:"required"` // AuthorizedRoles定义允许访问资源的角色
ForbiddenRoles string `json:"forbidden_roles" bson:"forbidden_roles"`
AllowAnyone bool `json:"allow_anyone" bson:"allow_anyone"`
}
// EditDoc 编辑结构体
type EditDoc struct {
RoleID *int `json:"id" bson:"id"`
Host *string `json:"host" bson:"host" binding:"required"` // Host 定义资源的Host,允许使用增强的通配符。
Path *string `json:"path" bson:"path" binding:"required"` // Path 定义资源的Path,允许使用增强的通配符。
Method *string `json:"method" bson:"method" binding:"required" ` // Method 定义资源的Method,允许使用增强的通配符。
AuthorizedRoles []string `json:"authorized_roles" bson:"authorized_roles"` // AuthorizedRoles定义允许访问资源的角色
ForbiddenRoles []string `json:"forbidden_roles" bson:"forbidden_roles"`
AllowAnyone *bool `json:"allow_anyone" bson:"allow_anyone"`
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"`
}
func Init() {
mdb = db.Init(table)
initIndex()
}
+33
View File
@@ -0,0 +1,33 @@
package rolemod
import (
"91porn-server/models/commod"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type ListParam struct {
ListFilter
commod.Page
}
type ListFilter struct {
UID uint64 `form:"uid" json:"uid,omitempty" bson:"uid,omitempty"` //用户ID
}
type WebResp struct {
Total int64 `json:"total"`
List interface{} `json:"list"`
}
// 用于记录权限配置
type RoleResp struct {
ID primitive.ObjectID `json:"_id" bson:"_id,omitempty"`
RoleID int `json:"id" bson:"id"`
Host string `json:"host" bson:"host"` // Host 定义资源的Host,允许使用增强的通配符。
Path string `json:"path" bson:"path"` // Path 定义资源的Path,允许使用增强的通配符。
Method string `json:"method" bson:"method"` // Method 定义资源的Method,允许使用增强的通配符。
AuthorizedRoles []string `json:"authorized_roles" bson:"authorized_roles"` // AuthorizedRoles定义允许访问资源的角色
ForbiddenRoles []string `json:"forbidden_roles" bson:"forbidden_roles"`
AllowAnyone bool `json:"allow_anyone" bson:"allow_anyone"`
}