Initial commit
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
package authorityMod
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"stat/common/log"
|
||||
"stat/common/mongos"
|
||||
"stat/common/utils"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
func Init() {
|
||||
// 获取数据库表
|
||||
db = mongos.New(mongos.MgoManager, CollAuthority)
|
||||
|
||||
// 初始化索引
|
||||
indexes := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{"role", 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
}
|
||||
if _, err := db.CreateIndex(context.Background(), indexes); err != nil {
|
||||
panic(fmt.Sprintf("Model %s InitIndex error %+v", CollAuthority, err))
|
||||
}
|
||||
}
|
||||
|
||||
// 添加角色
|
||||
func AddRole(ctx context.Context, doc *Authority) (err error) {
|
||||
if _, err = db.InsertOne(ctx, doc); err != nil {
|
||||
log.Error(fmt.Sprintf("model %s AddRole InsertOne error", CollAuthority), log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 更新角色
|
||||
func UpdateRole(ctx context.Context, roleId int64, doc UpdateDoc) (err error) {
|
||||
filter := bson.M{"_id": roleId}
|
||||
update, _ := utils.ToBsonM(doc)
|
||||
|
||||
if _, err = db.UpdateOne(ctx, filter, bson.M{"$set": update}); err != nil {
|
||||
log.Error(fmt.Sprintf("model %s UpdateRole UpdateOne error", CollAuthority), log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 删除角色
|
||||
func DelRoleById(ctx context.Context, roleId int64) (err error) {
|
||||
filter := bson.M{"_id": roleId}
|
||||
|
||||
if _, err = db.DeleteOne(ctx, filter); err != nil {
|
||||
log.Error(fmt.Sprintf("model %s DelRole DeleteOne error", CollAuthority), log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 角色列表
|
||||
func RoleList(ctx context.Context, skip int64, limit int64) (count int64, data []Authority, err error) {
|
||||
filter := bson.M{}
|
||||
opts := options.Find().SetSkip(skip).SetLimit(limit)
|
||||
|
||||
if err = db.Find(ctx, &data, filter, opts); err != nil {
|
||||
log.Error(fmt.Sprintf("model %s RoleList Find error", CollAuthority), log.E(err))
|
||||
return
|
||||
}
|
||||
if count, err = db.Count(ctx, filter); err != nil {
|
||||
log.Error(fmt.Sprintf("model %s RoleList Count error", CollAuthority), log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 根据角色获取权限
|
||||
func GetAuthorityByRole(ctx context.Context, role string) (data Authority, err error) {
|
||||
query := bson.M{"role": role}
|
||||
|
||||
if err = db.FindOne(ctx, &data, query); err != nil {
|
||||
log.Error(fmt.Sprintf("model %s GetAuthorityByRole FindOne error", CollAuthority), log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 根据id获取权限
|
||||
func GetAuthorityById(ctx context.Context, id int64) (data Authority, err error) {
|
||||
query := bson.M{"_id": id}
|
||||
|
||||
if err = db.FindOne(ctx, &data, query); err != nil {
|
||||
log.Error(fmt.Sprintf("model %s GetAuthorityById FindOne error", CollAuthority), log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 根据ids获取权限列表
|
||||
func GetAuthorityByIds(ctx context.Context, ids []int64) (data []Authority, err error) {
|
||||
if len(ids) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
query := bson.M{"_id": bson.M{"$in": ids}}
|
||||
|
||||
if err = db.Find(ctx, &data, query); err != nil {
|
||||
log.Error(fmt.Sprintf("model %s GetAuthorityByIds Find error", CollAuthority), log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 获取角色信息,排除当前角色
|
||||
func GetRoleExcludeRole(ctx context.Context, roleId int64, role string) (data Authority, err error) {
|
||||
filter := bson.M{"_id": bson.M{"$ne": roleId}, "role": role}
|
||||
|
||||
if err = db.FindOne(ctx, &data, filter); err != nil {
|
||||
log.Error(fmt.Sprintf("model %s GetRoleExcludeRole FindOne error", CollAuthority),
|
||||
log.Any("roleId", roleId),
|
||||
log.Any("role", role),
|
||||
log.E(err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package authorityMod
|
||||
|
||||
import (
|
||||
"stat/common/mongos"
|
||||
"time"
|
||||
)
|
||||
|
||||
const CollAuthority = "authority"
|
||||
|
||||
var db *mongos.MongoTool
|
||||
|
||||
type Authority struct {
|
||||
Id int64 `bson:"_id"` // _id
|
||||
Role string `bson:"role"` // 角色
|
||||
AuthJson string `bson:"authJson"` // 权限json数据
|
||||
CreatedAt time.Time `bson:"createdAt"` // 创建时间
|
||||
UpdatedAt time.Time `bson:"updatedAt"` // 修改时间
|
||||
}
|
||||
|
||||
type UpdateDoc struct {
|
||||
Role *string `bson:"role,omitempty"` // 角色
|
||||
AuthJson *string `bson:"authJson,omitempty"` // 权限json数据
|
||||
UpdatedAt *time.Time `bson:"updatedAt,omitempty"` // 修改时间
|
||||
}
|
||||
Reference in New Issue
Block a user