|
@@ -4,17 +4,15 @@ import (
|
|
|
"context"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
+ "log"
|
|
|
"strings"
|
|
|
"sync"
|
|
|
"time"
|
|
|
|
|
|
- "github.com/mhaya/game/game_cluster/internal/code"
|
|
|
"github.com/mhaya/game/game_cluster/internal/mdb"
|
|
|
"github.com/mhaya/game/game_cluster/internal/mdb/models"
|
|
|
- "github.com/mhaya/game/game_cluster/nodes/webadmin/common"
|
|
|
"github.com/mhaya/game/game_cluster/nodes/webadmin/entity"
|
|
|
"github.com/mhaya/game/game_cluster/nodes/webadmin/model"
|
|
|
- mhayaLogger "github.com/mhaya/logger"
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
@@ -29,7 +27,7 @@ func NewRole() *Role {
|
|
|
}
|
|
|
|
|
|
// List 角色列表
|
|
|
-func (r *Role) List(ctx context.Context, req entity.RoleListReq) (*entity.RoleResp, *code.Result) {
|
|
|
+func (r *Role) List(ctx context.Context, req *entity.RoleListReq) ([]entity.RoleResp, error) {
|
|
|
roles := models.Roles{}
|
|
|
rolesCollection := mdb.MDB.Collection(roles.TableName())
|
|
|
// 构建过滤器
|
|
@@ -42,69 +40,60 @@ func (r *Role) List(ctx context.Context, req entity.RoleListReq) (*entity.RoleRe
|
|
|
}
|
|
|
// 数据验证
|
|
|
if req.Page <= 0 || req.Size <= 0 {
|
|
|
- mhayaLogger.Warnf("List param error, req.Page:%d, req.Size:%d", req.Page, req.Size)
|
|
|
- return nil, common.NewResult(code.ParamError)
|
|
|
+ return nil, fmt.Errorf("invalid page or size")
|
|
|
}
|
|
|
// 设置分页选项
|
|
|
findOptions := options.Find().SetSkip(int64((req.Page - 1) * req.Size)).SetLimit(int64(req.Size))
|
|
|
countDocuments, err := rolesCollection.CountDocuments(ctx, filter)
|
|
|
if err != nil {
|
|
|
- mhayaLogger.Warnf("List CountDocuments error:", err)
|
|
|
- return nil, common.NewResult(code.InternalError)
|
|
|
+ return nil, err
|
|
|
}
|
|
|
-
|
|
|
+ req.Count = countDocuments
|
|
|
// 防御性编程
|
|
|
tableName := roles.TableName()
|
|
|
if tableName == "" {
|
|
|
- mhayaLogger.Warnf("List tableName is nil, tableName:", tableName)
|
|
|
- return nil, common.NewResult(code.InternalError)
|
|
|
+ return nil, fmt.Errorf("invalid table name")
|
|
|
}
|
|
|
|
|
|
cursor, err := rolesCollection.Find(ctx, filter, findOptions)
|
|
|
if err != nil {
|
|
|
- mhayaLogger.Warnf("List Find error:", err)
|
|
|
- return nil, common.NewResult(code.InternalError)
|
|
|
+ log.Printf("Failed to execute query: %v", err)
|
|
|
+ return nil, err
|
|
|
}
|
|
|
defer func() {
|
|
|
if err := cursor.Close(ctx); err != nil {
|
|
|
- mhayaLogger.Warnf("Failed to close cursor: %v", err)
|
|
|
+ log.Printf("Failed to close cursor: %v", err)
|
|
|
}
|
|
|
}()
|
|
|
|
|
|
- var details []*entity.RoleDetail
|
|
|
+ var result []entity.RoleResp
|
|
|
for cursor.Next(ctx) {
|
|
|
- var role *entity.RoleDetail
|
|
|
+ var role entity.RoleResp
|
|
|
if err := cursor.Decode(&role); err != nil {
|
|
|
- mhayaLogger.Warnf("List Decode error:", err)
|
|
|
- return nil, common.NewResult(code.InternalError)
|
|
|
+ log.Printf("Failed to decode document: %v", err)
|
|
|
+ return nil, err
|
|
|
}
|
|
|
- details = append(details, role)
|
|
|
+ result = append(result, role)
|
|
|
}
|
|
|
|
|
|
if err := cursor.Err(); err != nil {
|
|
|
- mhayaLogger.Warnf("List cursor error:", err)
|
|
|
- return nil, common.NewResult(code.InternalError)
|
|
|
+ log.Printf("Cursor error: %v", err)
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
- return &entity.RoleResp{
|
|
|
- Details: details,
|
|
|
- Total: countDocuments,
|
|
|
- }, nil
|
|
|
+ return result, nil
|
|
|
}
|
|
|
|
|
|
// Add 新增角色
|
|
|
-func (r *Role) Add(ctx context.Context, req entity.RoleAddReq) *code.Result {
|
|
|
+func (r *Role) Add(ctx context.Context, req entity.RoleAddReq) error {
|
|
|
// 验证角色名称是否已存在
|
|
|
- if !r.checkRoleNameExist(req.Name) {
|
|
|
- return common.NewResult(code.RoleNameExistError)
|
|
|
+ if r.checkRoleNameExist(req.Name) == false {
|
|
|
+ return fmt.Errorf("角色名称已存在")
|
|
|
}
|
|
|
-
|
|
|
// 检查上下文是否有效
|
|
|
if ctx.Err() != nil {
|
|
|
- mhayaLogger.Warnf("Add ctx error:", ctx.Err())
|
|
|
- return common.NewResult(code.InternalError)
|
|
|
+ return ctx.Err()
|
|
|
}
|
|
|
-
|
|
|
// 插入新角色记录
|
|
|
roles := models.Roles{}
|
|
|
insertData := bson.M{}
|
|
@@ -116,22 +105,19 @@ func (r *Role) Add(ctx context.Context, req entity.RoleAddReq) *code.Result {
|
|
|
collection := mdb.MDB.Collection(roles.TableName())
|
|
|
_, insertErr := collection.InsertOne(ctx, req)
|
|
|
if insertErr != nil {
|
|
|
- mhayaLogger.Warnf("Add InsertOne error:", insertErr)
|
|
|
- return common.NewResult(code.InternalError)
|
|
|
+ log.Printf("Failed to insert role: %s", insertErr)
|
|
|
+ return insertErr
|
|
|
}
|
|
|
-
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
// Update 修改角色
|
|
|
-func (r *Role) Update(ctx context.Context, req entity.RoleUpdateReq) *code.Result {
|
|
|
+func (r *Role) Update(ctx context.Context, req entity.RoleUpdateReq) error {
|
|
|
// 更新条件
|
|
|
objID, err := primitive.ObjectIDFromHex(req.Id)
|
|
|
if err != nil {
|
|
|
- mhayaLogger.Warnf("Update req.Id error:", req.Id)
|
|
|
- return common.NewResult(code.ParamError)
|
|
|
+ return fmt.Errorf("invalid ObjectID: %v", err)
|
|
|
}
|
|
|
-
|
|
|
updateCondition := bson.M{"_id": objID}
|
|
|
// 更新内容
|
|
|
updateContent := bson.M{
|
|
@@ -148,10 +134,8 @@ func (r *Role) Update(ctx context.Context, req entity.RoleUpdateReq) *code.Resul
|
|
|
// 执行更新操作
|
|
|
_, err = collection.UpdateOne(context.TODO(), updateCondition, updateContent, updateOptions)
|
|
|
if err != nil {
|
|
|
- mhayaLogger.Warnf("Update UpdateOne error:", err)
|
|
|
- return common.NewResult(code.InternalError)
|
|
|
+ return err
|
|
|
}
|
|
|
-
|
|
|
return nil
|
|
|
}
|
|
|
|
|
@@ -169,7 +153,7 @@ func (r *Role) checkRoleNameExist(name string) bool {
|
|
|
// 执行查询
|
|
|
if err := collection.FindOne(ctx, filter).Err(); err != nil {
|
|
|
if errors.Is(err, mongo.ErrNoDocuments) {
|
|
|
- mhayaLogger.Warnf("No document found with role name: %s", name)
|
|
|
+ log.Printf("No document found with role name: %s", name)
|
|
|
return true
|
|
|
}
|
|
|
return false
|
|
@@ -178,7 +162,7 @@ func (r *Role) checkRoleNameExist(name string) bool {
|
|
|
}
|
|
|
|
|
|
// Del 删除角色
|
|
|
-func (r *Role) Del(ctx context.Context, req entity.RoleDelReq) *code.Result {
|
|
|
+func (r *Role) Del(ctx context.Context, req entity.RoleDelReq) error {
|
|
|
// 创建带超时的上下文
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
defer cancel()
|
|
@@ -186,29 +170,22 @@ func (r *Role) Del(ctx context.Context, req entity.RoleDelReq) *code.Result {
|
|
|
collection := mdb.MDB.Collection(roles.TableName())
|
|
|
id, _ := primitive.ObjectIDFromHex(req.Id)
|
|
|
_, err := collection.DeleteOne(ctx, bson.M{"_id": id})
|
|
|
- if err != nil {
|
|
|
- mhayaLogger.Warnf("Del DeleteOne error:", err)
|
|
|
- return common.NewResult(code.InternalError)
|
|
|
- }
|
|
|
-
|
|
|
- return nil
|
|
|
+ return err
|
|
|
}
|
|
|
|
|
|
// AddRoleAccess 添加角色权限
|
|
|
-func (r *Role) AddRoleAccess(ctx context.Context, req entity.RoleAccessAddReq) *code.Result {
|
|
|
+func (r *Role) AddRoleAccess(ctx context.Context, req entity.RoleAccessAddReq) error {
|
|
|
// 检查上下文是否有效
|
|
|
if ctx.Err() != nil {
|
|
|
- mhayaLogger.Warnf("AddRoleAccess ctx error:", ctx.Err())
|
|
|
- return common.NewResult(code.InternalError)
|
|
|
+ return ctx.Err()
|
|
|
}
|
|
|
-
|
|
|
// 检查角色是否存在
|
|
|
roles := models.Roles{}
|
|
|
collection := mdb.MDB.Collection(roles.TableName())
|
|
|
roleIDobj, _ := primitive.ObjectIDFromHex(req.RoleId)
|
|
|
if err := collection.FindOne(ctx, bson.M{"_id": roleIDobj}).Err(); err != nil {
|
|
|
if errors.Is(err, mongo.ErrNoDocuments) {
|
|
|
- return common.NewResult(code.RoleNotExistError)
|
|
|
+ return fmt.Errorf("角色不存在")
|
|
|
}
|
|
|
}
|
|
|
// 检查权限是否存在 ->具体的权限规则表-存放路由、菜单等
|
|
@@ -222,7 +199,7 @@ func (r *Role) AddRoleAccess(ctx context.Context, req entity.RoleAccessAddReq) *
|
|
|
filter := bson.M{"_id": bson.M{"$in": accessIDS}} // 数组查询
|
|
|
if err := collection.FindOne(ctx, filter).Err(); err != nil {
|
|
|
if errors.Is(err, mongo.ErrNoDocuments) {
|
|
|
- return common.NewResult(code.AccessNotExistError)
|
|
|
+ return fmt.Errorf("权限不存在")
|
|
|
}
|
|
|
}
|
|
|
// 插入新角色权限记录
|
|
@@ -231,48 +208,40 @@ func (r *Role) AddRoleAccess(ctx context.Context, req entity.RoleAccessAddReq) *
|
|
|
collection = mdb.MDB.Collection(roleAccess.TableName())
|
|
|
_, insertErr := collection.UpdateOne(ctx, bson.M{"role_id": req.RoleId}, bson.M{"$addToSet": bson.M{"access_id": bson.M{"$each": req.AccessId}}}, options.Update().SetUpsert(true))
|
|
|
if insertErr != nil {
|
|
|
- mhayaLogger.Warnf("AddRoleAccess UpdateOne error:", insertErr)
|
|
|
- return common.NewResult(code.InternalError)
|
|
|
+ log.Printf("Failed to insert role: %s", insertErr)
|
|
|
+ return insertErr
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
// UpdateRoleAccess 修改角色权限
|
|
|
-func (r *Role) UpdateRoleAccess(ctx context.Context, req entity.RoleAccessUpdateReq) *code.Result {
|
|
|
+func (r *Role) UpdateRoleAccess(ctx context.Context, req entity.RoleAccessUpdateReq) error {
|
|
|
// 验证请求数据的有效性
|
|
|
- err := validateConcurrently(ctx, req)
|
|
|
- if err != nil {
|
|
|
- mhayaLogger.Warnf("UpdateRoleAccess validateConcurrently error:", err)
|
|
|
- return common.NewResult(code.InternalError)
|
|
|
+ if err := validateConcurrently(ctx, req); err != nil {
|
|
|
+ return err
|
|
|
}
|
|
|
-
|
|
|
// 更新角色权限
|
|
|
- err = r.updateAccessInDatabase(ctx, req)
|
|
|
- if err != nil {
|
|
|
- mhayaLogger.Warnf("UpdateRoleAccess updateAccessInDatabase error:", err)
|
|
|
- return common.NewResult(code.InternalError)
|
|
|
+ if err := r.updateAccessInDatabase(ctx, req); err != nil {
|
|
|
+ return err
|
|
|
}
|
|
|
-
|
|
|
// 返回成功
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
// DelRoleAccess 根据角色ID删除角色权限
|
|
|
-func (r *Role) DelRoleAccess(ctx context.Context, req entity.RoleAccessDelReq) *code.Result {
|
|
|
+func (r *Role) DelRoleAccess(ctx context.Context, req entity.RoleAccessDelReq) error {
|
|
|
roleAccess := models.RoleAccess{}
|
|
|
collection := mdb.MDB.Collection(roleAccess.TableName())
|
|
|
filter := bson.M{"role_id": req.RoleId}
|
|
|
_, err := collection.DeleteOne(ctx, filter)
|
|
|
if err != nil {
|
|
|
- mhayaLogger.Warnf("DelRoleAccess DeleteOne error:", err)
|
|
|
- return common.NewResult(code.InternalError)
|
|
|
+ return err
|
|
|
}
|
|
|
-
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
// GetRoleAccessList 获取角色权限列表根据角色ID
|
|
|
-func (r *Role) GetRoleAccessList(ctx context.Context, req entity.RoleAccessListReq) (*entity.AccessResp, *code.Result) {
|
|
|
+func (r *Role) GetRoleAccessList(ctx context.Context, req entity.RoleAccessListReq) ([]*entity.AccessResp, error) {
|
|
|
// 查询角色权限列表
|
|
|
roleAccess := models.RoleAccess{}
|
|
|
collection := mdb.MDB.Collection(roleAccess.TableName())
|
|
@@ -280,16 +249,14 @@ func (r *Role) GetRoleAccessList(ctx context.Context, req entity.RoleAccessListR
|
|
|
cursor, err := collection.Find(ctx, filter)
|
|
|
defer cursor.Close(ctx)
|
|
|
if err != nil {
|
|
|
- mhayaLogger.Warnf("GetRoleAccessList Find error:", err)
|
|
|
- return nil, common.NewResult(code.InternalError)
|
|
|
+ return nil, err
|
|
|
}
|
|
|
-
|
|
|
var accessIDS []string
|
|
|
for cursor.Next(ctx) {
|
|
|
var roleAccess models.RoleAccess
|
|
|
if err := cursor.Decode(&roleAccess); err != nil {
|
|
|
- mhayaLogger.Warnf("GetRoleAccessList Decode error:", err)
|
|
|
- return nil, common.NewResult(code.InternalError)
|
|
|
+ log.Printf("Failed to decode document: %v", err)
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
accessIDS = append(accessIDS, roleAccess.AccessID...)
|
|
@@ -311,17 +278,16 @@ func (r *Role) GetRoleAccessList(ctx context.Context, req entity.RoleAccessListR
|
|
|
cursor, err = collection.Find(ctx, accessFilter)
|
|
|
defer cursor.Close(ctx)
|
|
|
if err != nil {
|
|
|
- mhayaLogger.Warnf("GetRoleAccessList sub Find error:", err)
|
|
|
- return nil, common.NewResult(code.InternalError)
|
|
|
+ return nil, err
|
|
|
}
|
|
|
- var accessList []*entity.AccessDetail
|
|
|
+ var accessList []*entity.AccessResp
|
|
|
for cursor.Next(ctx) {
|
|
|
var accesss *models.Access
|
|
|
if err := cursor.Decode(&accesss); err != nil {
|
|
|
- mhayaLogger.Warnf("GetRoleAccessList sub Decode error:", err)
|
|
|
- return nil, common.NewResult(code.InternalError)
|
|
|
+ log.Printf("Failed to decode document: %v", err)
|
|
|
+ return nil, err
|
|
|
}
|
|
|
- accessList = append(accessList, &entity.AccessDetail{
|
|
|
+ accessList = append(accessList, &entity.AccessResp{
|
|
|
ID: accesss.ID,
|
|
|
ActionName: accesss.ActionName,
|
|
|
ModuleName: accesss.ModuleName,
|
|
@@ -334,18 +300,13 @@ func (r *Role) GetRoleAccessList(ctx context.Context, req entity.RoleAccessListR
|
|
|
})
|
|
|
}
|
|
|
// 格式化数据并且按照下级关系组合
|
|
|
- details := formatAccessData(accessList)
|
|
|
-
|
|
|
- return &entity.AccessResp{
|
|
|
- Details: details,
|
|
|
- Total: 0,
|
|
|
- }, nil
|
|
|
+ return formatAccessData(accessList), nil
|
|
|
}
|
|
|
|
|
|
// formatAccessData formats and organizes access data into a hierarchical structure
|
|
|
-func formatAccessData(accessData []*entity.AccessDetail) []*entity.AccessDetail {
|
|
|
- nodeMap := make(map[interface{}]*entity.AccessDetail)
|
|
|
- var rootNodes []*entity.AccessDetail
|
|
|
+func formatAccessData(accessData []*entity.AccessResp) []*entity.AccessResp {
|
|
|
+ nodeMap := make(map[interface{}]*entity.AccessResp)
|
|
|
+ var rootNodes []*entity.AccessResp
|
|
|
for i := range accessData {
|
|
|
node := accessData[i]
|
|
|
nodeMap[node.ID] = node
|
|
@@ -364,33 +325,25 @@ func formatAccessData(accessData []*entity.AccessDetail) []*entity.AccessDetail
|
|
|
}
|
|
|
|
|
|
// AddAccess 添加权限路由
|
|
|
-func (r *Role) AddAccess(ctx context.Context, req entity.AccessAddReq) *code.Result {
|
|
|
+func (r *Role) AddAccess(ctx context.Context, req entity.AccessAddReq) error {
|
|
|
// 检查上下文是否有效
|
|
|
if ctx.Err() != nil {
|
|
|
- mhayaLogger.Warnf("AddAccess ctx error:", ctx.Err())
|
|
|
- return common.NewResult(code.InternalError)
|
|
|
+ return ctx.Err()
|
|
|
}
|
|
|
-
|
|
|
access := models.Access{}
|
|
|
collection := mdb.MDB.Collection(access.TableName())
|
|
|
// 判断是否有相同的数据
|
|
|
filter := bson.M{"path": req.URL}
|
|
|
if err := collection.FindOne(ctx, filter).Err(); err == nil {
|
|
|
- return common.NewResult(code.AccessExistError)
|
|
|
+ return fmt.Errorf("权限已存在")
|
|
|
}
|
|
|
-
|
|
|
// 插入新角色权限记录
|
|
|
_, err := collection.InsertOne(ctx, req)
|
|
|
- if err != nil {
|
|
|
- mhayaLogger.Warnf("AddAccess InsertOne error:", err)
|
|
|
- return common.NewResult(code.InternalError)
|
|
|
- }
|
|
|
-
|
|
|
- return nil
|
|
|
+ return err
|
|
|
}
|
|
|
|
|
|
// DelAccess 删除权限路由
|
|
|
-func (r *Role) DelAccess(ctx context.Context, req entity.AccessDelReq) *code.Result {
|
|
|
+func (r *Role) DelAccess(ctx context.Context, req entity.AccessDelReq) error {
|
|
|
access := models.Access{}
|
|
|
collection := mdb.MDB.Collection(access.TableName())
|
|
|
// 判断是否有角色使用了该权限路由
|
|
@@ -398,22 +351,16 @@ func (r *Role) DelAccess(ctx context.Context, req entity.AccessDelReq) *code.Res
|
|
|
collection = mdb.MDB.Collection(roleAccess.TableName())
|
|
|
filter := bson.M{"access_id": bson.M{"$in": req.Id}} // 数组查询
|
|
|
if err := collection.FindOne(ctx, filter).Err(); err == nil {
|
|
|
- return common.NewResult(code.AccessHasUsedError)
|
|
|
+ return fmt.Errorf("权限已被角色使用,无法删除")
|
|
|
}
|
|
|
-
|
|
|
objID := primitive.ObjectID{}
|
|
|
objID, _ = primitive.ObjectIDFromHex(req.Id)
|
|
|
_, err := mdb.MDB.Collection(access.TableName()).DeleteOne(ctx, bson.M{"_id": objID})
|
|
|
- if err != nil {
|
|
|
- mhayaLogger.Warnf("DelAccess DeleteOne error:", err)
|
|
|
- return common.NewResult(code.InternalError)
|
|
|
- }
|
|
|
-
|
|
|
- return nil
|
|
|
+ return err
|
|
|
}
|
|
|
|
|
|
// UpdateAccess 修改权限路由
|
|
|
-func (r *Role) UpdateAccess(ctx context.Context, req entity.AccessUpdateReq) *code.Result {
|
|
|
+func (r *Role) UpdateAccess(ctx context.Context, req entity.AccessUpdateReq) error {
|
|
|
access := models.Access{}
|
|
|
collection := mdb.MDB.Collection(access.TableName())
|
|
|
// update
|
|
@@ -428,28 +375,22 @@ func (r *Role) UpdateAccess(ctx context.Context, req entity.AccessUpdateReq) *co
|
|
|
"status": req.Status}
|
|
|
// 检查是否有需要更新的字段
|
|
|
if len(updateFields) == 0 {
|
|
|
- mhayaLogger.Warnf("UpdateAccess len(updateFields) == 0")
|
|
|
- return nil
|
|
|
+ return errors.New("no fields to update")
|
|
|
}
|
|
|
-
|
|
|
// 确保 req.Id 是一个有效的 ObjectID
|
|
|
objID, err := primitive.ObjectIDFromHex(req.Id)
|
|
|
if err != nil {
|
|
|
- mhayaLogger.Warnf("UpdateAccess invalid ObjectID:%s, error:", req.Id, err)
|
|
|
- return common.NewResult(code.ParamError)
|
|
|
+ return fmt.Errorf("invalid ObjectID: %v", err)
|
|
|
}
|
|
|
-
|
|
|
_, err = collection.UpdateByID(ctx, objID, bson.M{"$set": updateFields})
|
|
|
if err != nil {
|
|
|
- mhayaLogger.Warnf("UpdateAccess UpdateByID error:", err)
|
|
|
- return common.NewResult(code.InternalError)
|
|
|
+ return fmt.Errorf("update failed: %v", err)
|
|
|
}
|
|
|
-
|
|
|
- return nil
|
|
|
+ return err
|
|
|
}
|
|
|
|
|
|
// ListAccess listAccessa
|
|
|
-func (r *Role) ListAccess(ctx context.Context, req entity.AccessListReq) (*entity.AccessResp, *code.Result) {
|
|
|
+func (r *Role) ListAccess(ctx context.Context, req *entity.AccessListReq) ([]*entity.AccessResp, error) {
|
|
|
access := models.Access{}
|
|
|
collection := mdb.MDB.Collection(access.TableName())
|
|
|
filter := bson.M{}
|
|
@@ -473,35 +414,29 @@ func (r *Role) ListAccess(ctx context.Context, req entity.AccessListReq) (*entit
|
|
|
}
|
|
|
// 数据验证
|
|
|
if req.Page <= 0 || req.Size <= 0 {
|
|
|
- mhayaLogger.Warnf("ListAccess param error, req.Page:%d, req.Size:%d", req.Page, req.Size)
|
|
|
- return nil, common.NewResult(code.ParamError)
|
|
|
+ return nil, fmt.Errorf("invalid page or size")
|
|
|
}
|
|
|
-
|
|
|
// 设置分页选项
|
|
|
findOptions := options.Find().SetSkip(int64((req.Page - 1) * req.Size)).SetLimit(int64(req.Size))
|
|
|
findOptions.SetSort(bson.M{"add_time": -1})
|
|
|
countDocuments, err := collection.CountDocuments(ctx, filter)
|
|
|
if err != nil {
|
|
|
- mhayaLogger.Warnf("ListAccess CountDocuments error:", err)
|
|
|
- return nil, common.NewResult(code.InternalError)
|
|
|
+ return nil, err
|
|
|
}
|
|
|
-
|
|
|
+ req.Count = countDocuments
|
|
|
cursor, err := collection.Find(ctx, filter, findOptions)
|
|
|
defer cursor.Close(ctx)
|
|
|
if err != nil {
|
|
|
- mhayaLogger.Warnf("ListAccess Find error:", err)
|
|
|
- return nil, common.NewResult(code.InternalError)
|
|
|
+ return nil, err
|
|
|
}
|
|
|
-
|
|
|
- var accessList []*entity.AccessDetail
|
|
|
+ var accessList []*entity.AccessResp
|
|
|
for cursor.Next(ctx) {
|
|
|
var accesss *models.Access
|
|
|
if err := cursor.Decode(&accesss); err != nil {
|
|
|
- mhayaLogger.Warnf("ListAccess Decode error:", err)
|
|
|
- return nil, common.NewResult(code.InternalError)
|
|
|
+ log.Printf("Failed to decode document: %v", err)
|
|
|
+ return nil, err
|
|
|
}
|
|
|
-
|
|
|
- accessList = append(accessList, &entity.AccessDetail{
|
|
|
+ accessList = append(accessList, &entity.AccessResp{
|
|
|
ID: accesss.ID,
|
|
|
ActionName: accesss.ActionName,
|
|
|
ModuleName: accesss.ModuleName,
|
|
@@ -513,14 +448,8 @@ func (r *Role) ListAccess(ctx context.Context, req entity.AccessListReq) (*entit
|
|
|
Status: accesss.Status,
|
|
|
})
|
|
|
}
|
|
|
-
|
|
|
// 格式化数据并且按照下级关系组合
|
|
|
- details := formatAccessData(accessList)
|
|
|
-
|
|
|
- return &entity.AccessResp{
|
|
|
- Details: details,
|
|
|
- Total: countDocuments,
|
|
|
- }, nil
|
|
|
+ return formatAccessData(accessList), nil
|
|
|
}
|
|
|
|
|
|
// updateAccessInDatabase 在数据库中更新角色权限
|
|
@@ -538,7 +467,7 @@ func (r *Role) updateAccessInDatabase(ctx context.Context, req entity.RoleAccess
|
|
|
}
|
|
|
|
|
|
// AdminBindRole 绑定角色
|
|
|
-func (r *Role) AdminBindRole(ctx context.Context, req entity.AdminBindRoleReq) *code.Result {
|
|
|
+func (r *Role) AdminBindRole(ctx context.Context, req *entity.AdminBindRoleReq) error {
|
|
|
// 例如更新角色权限表中的记录
|
|
|
role := models.Roles{}
|
|
|
collection := mdb.MDB.Collection(role.TableName())
|
|
@@ -546,47 +475,36 @@ func (r *Role) AdminBindRole(ctx context.Context, req entity.AdminBindRoleReq) *
|
|
|
filter := bson.M{"_id": roleId, "status": 1}
|
|
|
// 判断你是否存在
|
|
|
if err := collection.FindOne(ctx, filter).Err(); err != nil {
|
|
|
- mhayaLogger.Warnf("AdminBindRole Find error:", err)
|
|
|
- return common.NewResult(code.RoleNotExistOrDisabledUserError)
|
|
|
+ return fmt.Errorf("角色不存在,或者已经被禁用")
|
|
|
}
|
|
|
-
|
|
|
// 判断管理员是否存在
|
|
|
admin := model.Admin{}
|
|
|
collection = mdb.MDB.Collection(admin.TableName())
|
|
|
objID, _ := primitive.ObjectIDFromHex(req.AdminId)
|
|
|
err := collection.FindOne(ctx, bson.M{"_id": objID}).Decode(&admin)
|
|
|
if err != nil {
|
|
|
- return common.NewResult(code.AdminNotExistError)
|
|
|
+ return fmt.Errorf("管理员不存在")
|
|
|
}
|
|
|
-
|
|
|
if admin.RoleId == req.RoleId {
|
|
|
- mhayaLogger.Warnf("管理员角色和请求角色一致,无需修改")
|
|
|
- return nil
|
|
|
+ return fmt.Errorf("管理员角色和请求角色一致,无需修改")
|
|
|
}
|
|
|
-
|
|
|
if admin.Username == "admin" {
|
|
|
- mhayaLogger.Warnf("admin-超级账户不能修改角色")
|
|
|
- return common.NewResult(code.AdminMustNotUpdateError)
|
|
|
+ return fmt.Errorf("admin-超级账户不能修改角色")
|
|
|
}
|
|
|
-
|
|
|
filter = bson.M{"_id": objID, "status": 1}
|
|
|
if err := collection.FindOne(ctx, filter).Err(); err != nil {
|
|
|
- mhayaLogger.Warnf("管理员不存在 或者 已经被禁用")
|
|
|
- return common.NewResult(code.RoleNotExistOrDisabledUserError)
|
|
|
+ return fmt.Errorf("管理员不存在 或者 已经被禁用")
|
|
|
}
|
|
|
-
|
|
|
// 更新管理员数据
|
|
|
_, err = collection.UpdateByID(ctx, objID, bson.M{"$set": bson.M{"role_id": req.RoleId}})
|
|
|
if err != nil {
|
|
|
- mhayaLogger.Warnf("AdminBindRole UpdateByID error:", err)
|
|
|
- return common.NewResult(code.InternalError)
|
|
|
+ return fmt.Errorf("更新管理员失败")
|
|
|
}
|
|
|
-
|
|
|
return nil // 假设更新成功,实际应根据业务逻辑处理
|
|
|
}
|
|
|
|
|
|
// AdminUnBindRole 取消绑定角色
|
|
|
-func (r *Role) AdminUnBindRole(ctx context.Context, req entity.AdminBindRoleReq) *code.Result {
|
|
|
+func (r *Role) AdminUnBindRole(ctx context.Context, req *entity.AdminBindRoleReq) error {
|
|
|
// 例如更新角色权限表中的记录
|
|
|
admin := model.Admin{}
|
|
|
collection := mdb.MDB.Collection(admin.TableName())
|
|
@@ -594,26 +512,20 @@ func (r *Role) AdminUnBindRole(ctx context.Context, req entity.AdminBindRoleReq)
|
|
|
filter := bson.M{"_id": objID}
|
|
|
err := collection.FindOne(ctx, filter).Decode(&admin)
|
|
|
if err != nil {
|
|
|
- return common.NewResult(code.AdminNotExistError)
|
|
|
+ return fmt.Errorf("管理员不存在")
|
|
|
}
|
|
|
|
|
|
if admin.RoleId == req.RoleId {
|
|
|
- mhayaLogger.Warnf("管理员角色和请求角色一致,无需修改")
|
|
|
- return nil
|
|
|
+ return fmt.Errorf("管理员角色和请求角色一致,无需修改")
|
|
|
}
|
|
|
-
|
|
|
if admin.Username == "admin" {
|
|
|
- mhayaLogger.Warnf("admin-超级账户不能修改角色")
|
|
|
- return common.NewResult(code.AdminMustNotUpdateError)
|
|
|
+ return fmt.Errorf("admin-超级账户不能修改角色")
|
|
|
}
|
|
|
-
|
|
|
// 更新管理员数据
|
|
|
_, err = collection.UpdateByID(ctx, objID, bson.M{"$set": bson.M{"role_id": ""}})
|
|
|
if err != nil {
|
|
|
- mhayaLogger.Warnf("AdminUnBindRole UpdateByID error:", err)
|
|
|
- return common.NewResult(code.InternalError)
|
|
|
+ return fmt.Errorf("更新管理员失败")
|
|
|
}
|
|
|
-
|
|
|
return nil
|
|
|
}
|
|
|
|
|
@@ -670,27 +582,26 @@ func getRoleAccess(ctx context.Context, roleId string) (*models.RoleAccess, erro
|
|
|
return &roleAccess, nil
|
|
|
}
|
|
|
|
|
|
-func (r *Role) GetAdminRole(ctx context.Context, req entity.AdminBindRoleReq) (*entity.AdminBindRoleResp, *code.Result) {
|
|
|
+func (r *Role) GetAdminRole(ctx context.Context, req *entity.AdminBindRoleReq) (*entity.AdminBindRoleResp, error) {
|
|
|
+
|
|
|
if req.RoleId == "admin" {
|
|
|
+
|
|
|
access := models.Access{}
|
|
|
collection := mdb.MDB.Collection(access.TableName())
|
|
|
filter := bson.M{}
|
|
|
cursor, err := collection.Find(ctx, filter)
|
|
|
defer cursor.Close(ctx)
|
|
|
if err != nil {
|
|
|
- mhayaLogger.Warnf("GetAdminRole Find error:", err)
|
|
|
- return nil, common.NewResult(code.InternalError)
|
|
|
+ return nil, fmt.Errorf("查询权限失败: %v", err)
|
|
|
}
|
|
|
-
|
|
|
- var accessList []*entity.AccessDetail
|
|
|
+ var accessList []*entity.AccessResp
|
|
|
for cursor.Next(ctx) {
|
|
|
var accesss *models.Access
|
|
|
err := cursor.Decode(&accesss)
|
|
|
if err != nil {
|
|
|
- mhayaLogger.Warnf("GetAdminRole Decode error:", err)
|
|
|
- return nil, common.NewResult(code.InternalError)
|
|
|
+ return nil, fmt.Errorf("解析权限数据失败: %v", err)
|
|
|
}
|
|
|
- accessList = append(accessList, &entity.AccessDetail{
|
|
|
+ accessList = append(accessList, &entity.AccessResp{
|
|
|
ID: accesss.ID,
|
|
|
ActionName: accesss.ActionName,
|
|
|
Description: accesss.Description,
|
|
@@ -715,14 +626,12 @@ func (r *Role) GetAdminRole(ctx context.Context, req entity.AdminBindRoleReq) (*
|
|
|
|
|
|
role, err := getRole(ctx, req.RoleId)
|
|
|
if err != nil {
|
|
|
- mhayaLogger.Warnf("GetAdminRole getRole error:", err)
|
|
|
- return nil, common.NewResult(code.InternalError)
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
roleAccess, err := getRoleAccess(ctx, req.RoleId)
|
|
|
if err != nil {
|
|
|
- mhayaLogger.Warnf("GetAdminRole getRoleAccess error:", err)
|
|
|
- return nil, common.NewResult(code.InternalError)
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
var AccessIds []primitive.ObjectID
|
|
@@ -738,11 +647,9 @@ func (r *Role) GetAdminRole(ctx context.Context, req entity.AdminBindRoleReq) (*
|
|
|
|
|
|
if len(AccessIds) == 0 {
|
|
|
if len(invalidAccessIds) > 0 {
|
|
|
- mhayaLogger.Warnf("GetAdminRole 无效的权限ID:", strings.Join(invalidAccessIds, ", "))
|
|
|
- return nil, common.NewResult(code.InternalError)
|
|
|
+ return nil, fmt.Errorf("无效的权限ID: %v", strings.Join(invalidAccessIds, ", "))
|
|
|
}
|
|
|
-
|
|
|
- return nil, common.NewResult(code.NoAccessError)
|
|
|
+ return nil, fmt.Errorf("没有权限")
|
|
|
}
|
|
|
|
|
|
access := models.Access{}
|
|
@@ -752,18 +659,17 @@ func (r *Role) GetAdminRole(ctx context.Context, req entity.AdminBindRoleReq) (*
|
|
|
cursor, err := collection.Find(ctx, filter)
|
|
|
defer cursor.Close(ctx)
|
|
|
if err != nil {
|
|
|
- mhayaLogger.Warnf("GetAdminRole Find error:", err)
|
|
|
- return nil, common.NewResult(code.InternalError)
|
|
|
+ return nil, fmt.Errorf("查询权限失败: %v", err)
|
|
|
}
|
|
|
|
|
|
- var accessList []*entity.AccessDetail
|
|
|
+ var accessList []*entity.AccessResp
|
|
|
for cursor.Next(ctx) {
|
|
|
var accesss *models.Access
|
|
|
if err := cursor.Decode(&accesss); err != nil {
|
|
|
- mhayaLogger.Warnf("GetAdminRole Decode error:", err)
|
|
|
- return nil, common.NewResult(code.InternalError)
|
|
|
+ log.Printf("Failed to decode document: %v", err)
|
|
|
+ return nil, fmt.Errorf("解码权限失败: %v", err)
|
|
|
}
|
|
|
- accessList = append(accessList, &entity.AccessDetail{
|
|
|
+ accessList = append(accessList, &entity.AccessResp{
|
|
|
ID: accesss.ID,
|
|
|
ActionName: accesss.ActionName,
|
|
|
ModuleName: accesss.ModuleName,
|