@@ -0,0 +1,131 @@
|
||||
package authoritymod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"91porn-server/common"
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/models"
|
||||
|
||||
"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.Authority
|
||||
|
||||
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: "role", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
}
|
||||
if _, err := coll(nil).CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
}
|
||||
|
||||
// Insert 插入记录
|
||||
func Insert(doc AuthorityDoc) error {
|
||||
now := time.Now()
|
||||
doc.CreatedAt = &now
|
||||
doc.UpdatedAt = &now
|
||||
if _, err := coll(nil).InsertOne(doc); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "InsertOne", err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateTo
|
||||
func UpdateTo(id ObjectID, doc AuthorityDoc) error {
|
||||
now := time.Now()
|
||||
doc.UpdatedAt = &now
|
||||
update, err := common.ToBsonM(doc)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateTo", table, "ToBsonM", err), log.Any("id", id))
|
||||
return err
|
||||
}
|
||||
result, err := coll(nil).UpdateOne(M{"_id": id}, M{"$set": update})
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateTo", table, "UpdateOne", err), log.Any("id", id), log.Any("update", update))
|
||||
return err
|
||||
}
|
||||
if result.ModifiedCount+result.UpsertedCount != 1 {
|
||||
return fmt.Errorf("coll:%s UpdateTo Count error, id:%+v update:%+v", table, id, update)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteMany DeleteMany
|
||||
func DeleteMany(idArray []ObjectID) error {
|
||||
if _, err := coll(nil).DeleteMany(M{"_id": M{"$in": idArray}}); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeleteMany", table, "DeleteMany", err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func FindOne(doc AuthorityDoc) (authority Authority, err error) {
|
||||
filter, err := common.ToBsonM(doc)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindOne", table, "ToBsonM", err))
|
||||
return
|
||||
}
|
||||
if err = coll(nil).FindOne(&authority, filter); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindOne", table, "FindOne", err))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func ExistsByID(id ObjectID) (existed bool, err error) {
|
||||
filter := M{
|
||||
"_id": id,
|
||||
}
|
||||
count, err := coll(nil).Count(filter)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "ExistsByID", table, "Count", err))
|
||||
return false, err
|
||||
}
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
// FineOneByRole 通过权限id获取权限json
|
||||
func FineOneByRole(role string) (Authority, error) {
|
||||
var auth Authority
|
||||
if err := coll(nil).FindOne(&auth, bson.M{"role": role}); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FineOneByRole", table, "FindOne", err), log.Any("role", role))
|
||||
return Authority{}, err
|
||||
}
|
||||
return auth, nil
|
||||
}
|
||||
|
||||
// IDMapByRoles role->id Map
|
||||
func IDMapByRoles(roleArray []string) (map[string]ObjectID, error) {
|
||||
filter := M{
|
||||
"role": M{"$in": roleArray},
|
||||
}
|
||||
authArray := make([]Authority, 0, len(roleArray))
|
||||
if err := coll(nil).Find(&authArray, filter); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "IDMapByRoles", table, "Find", err), log.Any("roleArray", roleArray))
|
||||
return nil, err
|
||||
}
|
||||
idMap := make(map[string]ObjectID, len(authArray))
|
||||
for _, auth := range authArray {
|
||||
idMap[auth.Role] = auth.ID
|
||||
}
|
||||
return idMap, nil
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package authoritymod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"91porn-server/common/log"
|
||||
)
|
||||
|
||||
// AuthorPage AuthorPage
|
||||
type AuthorPage struct {
|
||||
Total int `json:"total" bson:"total"`
|
||||
List []AuthorityDoc `json:"list" bson:"list"`
|
||||
}
|
||||
|
||||
// AuthorPages AuthorPages
|
||||
func AuthorPages(skip int64, limit int64) (page AuthorPage, err error) {
|
||||
pipeline := []M{
|
||||
M{
|
||||
"$facet": M{
|
||||
"total": A{M{"$count": "total"}},
|
||||
"list": A{
|
||||
M{"$sort": M{"role": 1}},
|
||||
M{"$skip": skip},
|
||||
M{"$limit": limit},
|
||||
},
|
||||
},
|
||||
},
|
||||
M{
|
||||
"$project": M{
|
||||
"total": M{"$arrayElemAt": A{"$total.total", 0}},
|
||||
"list": 1,
|
||||
},
|
||||
},
|
||||
}
|
||||
if err = coll(nil).AggregateDecode(&page, pipeline); err != nil {
|
||||
log.ZapLog.Error(fmt.Sprintf("coll:%s Page error:%+v:", table, err))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package authoritymod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type (
|
||||
// M M from Map
|
||||
M = bson.M
|
||||
|
||||
// D D from Doc
|
||||
D = bson.D
|
||||
|
||||
// A A from Array
|
||||
A = bson.A
|
||||
|
||||
// ObjectID ObjectID
|
||||
ObjectID = primitive.ObjectID
|
||||
|
||||
// 用于记录权限配置
|
||||
Authority struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
|
||||
Role string `json:"role" bson:"role"` //角色,映射到权限模型
|
||||
AuthJson string `json:"authJson" bson:"authJson"` //"路由权限数据,json格式"
|
||||
ActJson string `json:"actJson" bson:"actJson"` //"功能权限数据,json格式"
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"`
|
||||
}
|
||||
|
||||
AuthorityDoc struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
|
||||
Role *string `json:"role" bson:"role,omitempty"` //角色,映射到权限模型
|
||||
AuthJson *string `json:"authJson" bson:"authJson,omitempty" ` //"路由权限数据,json格式"
|
||||
ActJson *string `json:"actJson" bson:"actJson,omitempty"` //"功能权限数据,json格式"
|
||||
CreatedAt *time.Time `json:"createdAt,omitempty" bson:"createdAt,omitempty"`
|
||||
UpdatedAt *time.Time `json:"updatedAt,omitempty" bson:"updatedAt,omitempty"`
|
||||
}
|
||||
)
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
initIndex()
|
||||
}
|
||||
Reference in New Issue
Block a user