package authorctrl import ( "encoding/json" "91porn-server/common" "91porn-server/common/constant" "91porn-server/common/stderr" "91porn-server/models/commod" "91porn-server/models/l/operatorlgmod" "91porn-server/models/v/adminmod" "91porn-server/models/v/authoritymod" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson/primitive" ) // List doc // @Summary 获取权限列表 // @Description 获取权限列表 // @Tags web-权限 // @Accept mpfd,json // @Produce json,html // @Param pageNumber query int true "当前页" // @Param pageSize query int true "每页条数" // @Success 200 {string} json "{"msg": "操作成功", "date": { "total":10, "list":[] }}" // @Failure 400 {string} json "{"msg": "操作失败"}" // @Router /web/author/list [get] func List(c *gin.Context) { var arg struct { commod.Page } if err := c.ShouldBind(&arg); err != nil { common.ServeJSON(c, stderr.ErrParamError, "auth List arg error "+err.Error()) return } skip := int64((arg.PageNumber - 1) * arg.PageSize) limit := int64(arg.PageSize) page, err := authoritymod.AuthorPages(skip, limit) if err != nil { common.ServeJSON(c, stderr.Failure, "auth List error: "+err.Error()) return } common.ServeJSON(c, stderr.Success, page) } // Update doc // @Summary 修改权限 // @Description 修改权限 // @Tags web-权限 // @Accept mpfd,json // @Produce json,html // @Param authID formData string true "权限ID" // @Param Role formData string true "角色" // @Param AuthJson formData string true "权限数据,json格式" // @Success 200 {string} json "{"msg": "操作成功"}" // @Failure 400 {string} json "{"msg": "操作失败"}" // @Router /web/author/update [post] func Update(c *gin.Context) { manager, err := common.GetAdminAct(c) if err != nil { common.ServeJSON(c, stderr.AdminIDErr, err.Error()) return } var arg struct { AuthID primitive.ObjectID `form:"authID" json:"authID,omitempty" binding:"required"` Role *string `form:"role" json:"role" binding:""` //角色 AuthJson *string `form:"authJson" json:"authJson" binding:""` //"路由权限数据,json格式" ActJson *string `form:"actJson" json:"actJson" binding:""` //"功能权限数据,json格式" } if err = c.ShouldBind(&arg); err != nil { common.ServeJSON(c, stderr.ErrParamError, "author Add arg error "+err.Error()) return } doc := authoritymod.AuthorityDoc{ Role: arg.Role, AuthJson: arg.AuthJson, ActJson: arg.ActJson, } if err = authoritymod.UpdateTo(arg.AuthID, doc); err != nil { common.ServeJSON(c, stderr.Failure, "auth Update error: "+err.Error()) return } log, _ := json.Marshal(arg) _ = operatorlgmod.RecordOperation(manager, constant.AuthorManager, constant.Modify, string(log), c.Request.URL.RequestURI()) common.ServeJSON(c, stderr.Success, "") } // Add doc // @Summary 添加权限 // @Description 批量删除权限 // @Tags web-权限 // @Accept mpfd,json // @Produce json,html // @Param Role formData string true "角色" // @Param AuthJson formData string false "权限数据,json格式" // @Success 200 {string} json "{"msg": "操作成功"}" // @Failure 400 {string} json "{"msg": "操作失败"}" // @Router /web/author/add [post] func Add(c *gin.Context) { manager, err := common.GetAdminAct(c) if err != nil { common.ServeJSON(c, stderr.AdminIDErr, err.Error()) return } var arg struct { Role *string `form:"role" json:"role" binding:"required"` //角色 AuthJson *string `form:"authJson" json:"authJson" binding:""` //"权限数据,json格式" } if err = c.ShouldBind(&arg); err != nil { common.ServeJSON(c, stderr.ErrParamError, "author Add arg error "+err.Error()) return } doc := authoritymod.AuthorityDoc{ Role: arg.Role, AuthJson: arg.AuthJson, } if err = authoritymod.Insert(doc); err != nil { common.ServeJSON(c, stderr.Failure, "auth Add error: "+err.Error()) return } log, _ := json.Marshal(arg) _ = operatorlgmod.RecordOperation(manager, constant.AuthorManager, constant.Add, string(log), c.Request.URL.RequestURI()) common.ServeJSON(c, stderr.Success, "") } // Delete doc // @Summary 批量删除权限 // @Description 批量删除权限 // @Tags web-权限 // @Accept mpfd,json // @Produce json,html // @Param authIDArray formData array true "权限ID数组" // @Success 200 {string} json "{"msg": "操作成功"}" // @Failure 400 {string} json "{"msg": "操作失败"}" // @Router /web/author/del [delete] func Delete(c *gin.Context) { manager, err := common.GetAdminAct(c) if err != nil { common.ServeJSON(c, stderr.AdminIDErr, err.Error()) return } var arg struct { AuthIDArray []string `form:"authIDArray" json:"authIDArray" binding:"required"` } if err = c.ShouldBind(&arg); err != nil { common.ServeJSON(c, stderr.ErrParamError, "author Del arg error "+err.Error()) return } authIDArray, err := common.IDArray(arg.AuthIDArray) if err != nil || len(authIDArray) == 0 { common.ServeJSON(c, stderr.ErrParamError, err) return } //检查权限是否存在 for _, authID := range authIDArray { doc := authoritymod.AuthorityDoc{ ID: authID, } auth, err := authoritymod.FindOne(doc) if err != nil { common.ServeJSON(c, stderr.Failure, "") return } if auth.ID.IsZero() { common.ServeJSON(c, stderr.AuthIsNotExist, "") return } using, err := adminmod.IsUsing(auth.Role) if err != nil { common.ServeJSON(c, stderr.ErrDbQueryError, "") return } if using { common.ServeJSON(c, stderr.AuthInUsing, "") return } } if err = authoritymod.DeleteMany(authIDArray); err != nil { common.ServeJSON(c, stderr.Failure, "auth DeleteMany error: "+err.Error()) return } log, _ := json.Marshal(arg) _ = operatorlgmod.RecordOperation(manager, constant.AuthorManager, constant.Delete, string(log), c.Request.URL.RequestURI()) common.ServeJSON(c, stderr.Success, "") }