|
@@ -0,0 +1,314 @@
|
|
|
|
+package permission
|
|
|
|
+
|
|
|
|
+import (
|
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
|
+ "net/http"
|
|
|
|
+ "sghgogs.com/sghblog/authorization-client/model/base"
|
|
|
|
+ "sghgogs.com/sghblog/authorization-client/model/response/admin"
|
|
|
|
+ "sghgogs.com/sghblog/authorization-client/response"
|
|
|
|
+ "sghgogs.com/sghblog/authorization-client/utils"
|
|
|
|
+ pb "sghgogs.com/sghblog/authorization-service/proto"
|
|
|
|
+ "sghgogs.com/sghblog/common"
|
|
|
|
+ "strconv"
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+// binding:"required"
|
|
|
|
+
|
|
|
|
+type ListResponse struct {
|
|
|
|
+ Items []admin.AdminPermission `json:"items"`
|
|
|
|
+ TotalCount int64 `json:"total_count"`
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+type Request struct {
|
|
|
|
+ Page int32 `json:"page"` // 页码, 默认1
|
|
|
|
+ PageSize int32 `json:"page_size"` // 页大小, 默认20
|
|
|
|
+ Keyword string `json:"keyword"` // 关键词
|
|
|
|
+ Status base.Status `json:"status"` // 状态 ENABLED DISABLED DELETED
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// GetAdminPermissionList
|
|
|
|
+// @summary 权限列表
|
|
|
|
+// @Tags Permissions
|
|
|
|
+// @Accept json
|
|
|
|
+// @Produce json
|
|
|
|
+// @Param Authorization header string true "Bearer 用户令牌"
|
|
|
|
+// @Param page query int true "页码"
|
|
|
|
+// @Param page_size query int true "每页数量"
|
|
|
|
+// @Param keyword query string false "关键字"
|
|
|
|
+// @Param status query string false "状态" Enums(enabled, disabled)
|
|
|
|
+// @Success 200 {object} response.ApiResponse{data=ListResponse} "成功"
|
|
|
|
+// @Failure 400 {object} response.ApiResponse "请求错误"
|
|
|
|
+// @Failure 500 {object} response.ApiResponse "内部错误"
|
|
|
|
+// @Router /v1/api/admin/permissions [get]
|
|
|
|
+func (svc *AdminPermission) GetAdminPermissionList(c *gin.Context) {
|
|
|
|
+ validationConfig := response.ValidationConfig{
|
|
|
|
+ Validators: map[string]func(interface{}) error{
|
|
|
|
+ "page": response.ValidateInt, // 验证是否为整数
|
|
|
|
+ "page_size": response.ValidateInt, // 验证是否为整数
|
|
|
|
+ },
|
|
|
|
+ Required: []string{"page", "page_size"},
|
|
|
|
+ }
|
|
|
|
+ params, err := response.ParseQueryParameters(c, validationConfig)
|
|
|
|
+ if err != nil {
|
|
|
|
+ c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, err.Error()))
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ page, _ := strconv.Atoi(params["page"].(string))
|
|
|
|
+ pageSize, _ := strconv.Atoi(params["page_size"].(string))
|
|
|
|
+ keyword, status := "", ""
|
|
|
|
+ if value, ok := params["keyword"]; ok {
|
|
|
|
+ keyword = value.(string)
|
|
|
|
+ }
|
|
|
|
+ if value, ok := params["status"]; ok {
|
|
|
|
+ status = value.(string)
|
|
|
|
+ if status != "" && !base.IsValidStatus(status) {
|
|
|
|
+ c.JSON(http.StatusBadRequest, gin.H{"error": common.ErrorMessage[common.StatusIsNotValid]})
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ ctx, _ := utils.CreateContextWithToken(c, "authorizationservice", "AdminPermissionService.GetAdminPermissionList")
|
|
|
|
+ listRequest := pb.GetAdminPermissionListRequest{
|
|
|
|
+ Page: int32(page),
|
|
|
|
+ PageSize: int32(pageSize),
|
|
|
|
+ Keyword: keyword,
|
|
|
|
+ }
|
|
|
|
+ if status != "" {
|
|
|
|
+ enum, _ := base.MapStatusToEnum(base.Status(status))
|
|
|
|
+ listRequest.Status = enum
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ list, err := svc.Service.GetAdminPermissionList(ctx, &listRequest)
|
|
|
|
+ if err != nil {
|
|
|
|
+ code, mgs := response.MicroErrorRequest(err)
|
|
|
|
+ c.JSON(code, response.ErrorResponse(code, mgs))
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ permissions := make([]admin.AdminPermission, 0)
|
|
|
|
+ for _, permission := range list.Items {
|
|
|
|
+ var item admin.AdminPermission
|
|
|
|
+ common.SwapTo(permission, &item)
|
|
|
|
+ toStatus, _ := base.MapEnumToStatus(permission.Status)
|
|
|
|
+ item.Status = string(toStatus)
|
|
|
|
+ if permission.UpdatedAt > 0 {
|
|
|
|
+ item.UpdatedAt = utils.ConvertInt64ToTime(permission.UpdatedAt)
|
|
|
|
+ }
|
|
|
|
+ item.CreatedAt = utils.ConvertInt64ToTime(permission.CreatedAt)
|
|
|
|
+ item.IsReserved = permission.IsReserved
|
|
|
|
+ item.Roles = base.RolesToResponse(permission.Roles)
|
|
|
|
+ item.Endpoint = permission.Endpoint
|
|
|
|
+ permissions = append(permissions, item)
|
|
|
|
+ }
|
|
|
|
+ c.JSON(http.StatusOK, response.SuccessResponse(ListResponse{
|
|
|
|
+ Items: permissions,
|
|
|
|
+ TotalCount: list.TotalCount,
|
|
|
|
+ }))
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// GetAdminPermission
|
|
|
|
+// @summary 获取权限详情
|
|
|
|
+// @Tags Permissions
|
|
|
|
+// @Accept json
|
|
|
|
+// @Produce json
|
|
|
|
+// @Param Authorization header string true "Bearer 用户令牌"
|
|
|
|
+// @Param id path int true "权限ID"
|
|
|
|
+// @Success 200 {object} response.ApiResponse{data=admin.AdminPermission} "成功"
|
|
|
|
+// @Failure 400 {object} response.ApiResponse "请求错误"
|
|
|
|
+// @Failure 500 {object} response.ApiResponse "内部错误"
|
|
|
|
+// @Router /v1/api/admin/permission/{permissionID} [get]
|
|
|
|
+func (svc *AdminPermission) GetAdminPermission(c *gin.Context) {
|
|
|
|
+ permissionID, err := strconv.ParseInt(c.Param("permissionID"), 10, 64)
|
|
|
|
+ if err != nil || permissionID <= 0 {
|
|
|
|
+ c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, common.ErrorMessage[common.InvalidRoleID]))
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ ctx, _ := utils.CreateContextWithToken(c, "authorizationservice", "AdminRoleService.GetAdminRole")
|
|
|
|
+ list, err := svc.Service.GetAdminPermission(ctx, &pb.GetAdminPermissionRequest{
|
|
|
|
+ Id: permissionID,
|
|
|
|
+ })
|
|
|
|
+ if err != nil {
|
|
|
|
+ code, mgs := response.MicroErrorRequest(err)
|
|
|
|
+ c.JSON(http.StatusBadRequest, response.ErrorResponse(code, mgs))
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ permission := list.Data
|
|
|
|
+ status, _ := base.MapEnumToStatus(permission.Status)
|
|
|
|
+ c.JSON(http.StatusOK, response.SuccessResponse(admin.AdminPermission{
|
|
|
|
+ ID: permission.Id,
|
|
|
|
+ Name: permission.Name,
|
|
|
|
+ Description: permission.Description,
|
|
|
|
+ Roles: base.RolesToResponse(permission.Roles),
|
|
|
|
+ CreatedAt: utils.ConvertInt64ToTime(permission.CreatedAt),
|
|
|
|
+ UpdatedAt: utils.ConvertInt64ToTime(permission.UpdatedAt),
|
|
|
|
+ Status: string(status),
|
|
|
|
+ IsReserved: permission.IsReserved,
|
|
|
|
+ }))
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+type CreateRequest struct {
|
|
|
|
+ Name string `json:"name" binding:"required,max=50,min=3"` // 权限名称
|
|
|
|
+ Description string `json:"description"` // 非必填,描述
|
|
|
|
+ Endpoint string `json:"endpoint" binding:"required"`
|
|
|
|
+ Roles []int64 `json:"roles"` // 角色
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// CreateAdminPermission
|
|
|
|
+// @summary 新增权限
|
|
|
|
+// @Tags Permissions
|
|
|
|
+// @Accept json
|
|
|
|
+// @Produce json
|
|
|
|
+// @Param Authorization header string true "Bearer 用户令牌"
|
|
|
|
+// @Param body body CreateRequest true "请求body"
|
|
|
|
+// @Success 200 {object} response.ApiResponse "成功"
|
|
|
|
+// @Failure 400 {object} response.ApiResponse "请求错误"
|
|
|
|
+// @Failure 500 {object} response.ApiResponse "内部错误"
|
|
|
|
+// @Router /v1/api/admin/permission [post]
|
|
|
|
+func (svc *AdminPermission) CreateAdminPermission(c *gin.Context) {
|
|
|
|
+ var req CreateRequest
|
|
|
|
+ if err := c.BindJSON(&req); err != nil {
|
|
|
|
+ c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, err.Error()))
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ if len(req.Roles) > 0 {
|
|
|
|
+ if isBool, err := common.ValidateNumericInt64(req.Roles); !isBool {
|
|
|
|
+ c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, err.Error()))
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ ctx, _ := utils.CreateContextWithToken(c, "authorizationservice", "AdminPermissionService.CreateAdminPermission")
|
|
|
|
+ _, err := svc.Service.CreateAdminPermission(ctx, &pb.CreateAdminPermissionRequest{
|
|
|
|
+ Name: req.Name,
|
|
|
|
+ Description: req.Description,
|
|
|
|
+ Endpoint: req.Endpoint,
|
|
|
|
+ Roles: req.Roles,
|
|
|
|
+ Status: pb.StatusEnum_ENABLED,
|
|
|
|
+ })
|
|
|
|
+ if err != nil {
|
|
|
|
+ code, mgs := response.MicroErrorRequest(err)
|
|
|
|
+ c.JSON(code, response.ErrorResponse(code, mgs))
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ c.JSON(http.StatusOK, response.SuccessResponse(nil))
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+type UpdateRequest struct {
|
|
|
|
+ Description string `json:"description"` // 非必填,描述
|
|
|
|
+ Roles []int64 `json:"roles"` // 角色
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// UpdateAdminPermission
|
|
|
|
+// @summary 更新权限
|
|
|
|
+// @Tags Permissions
|
|
|
|
+// @Accept json
|
|
|
|
+// @Produce json
|
|
|
|
+// @Param Authorization header string true "Bearer 用户令牌"
|
|
|
|
+// @Param id path int true "权限ID"
|
|
|
|
+// @Param body body UpdateRequest true "请求body"
|
|
|
|
+// @Success 200 {object} response.ApiResponse "成功"
|
|
|
|
+// @Failure 400 {object} response.ApiResponse "请求错误"
|
|
|
|
+// @Failure 500 {object} response.ApiResponse "内部错误"
|
|
|
|
+// @Router /v1/api/admin/permission/{permissionID} [put]
|
|
|
|
+func (svc *AdminPermission) UpdateAdminPermission(c *gin.Context) {
|
|
|
|
+ var req UpdateRequest
|
|
|
|
+ if err := c.BindJSON(&req); err != nil {
|
|
|
|
+ c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, err.Error()))
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ permissionID, err := strconv.ParseInt(c.Param("permissionID"), 10, 64)
|
|
|
|
+ if err != nil || permissionID <= 0 {
|
|
|
|
+ c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, common.ErrorMessage[common.InvalidRoleID]))
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ if len(req.Roles) > 0 {
|
|
|
|
+ if isBool, err := common.ValidateNumericInt64(req.Roles); !isBool {
|
|
|
|
+ c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, err.Error()))
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ctx, _ := utils.CreateContextWithToken(c, "authorizationservice", "AdminRoleService.UpdateAdminRole")
|
|
|
|
+
|
|
|
|
+ _, err = svc.Service.UpdateAdminPermission(ctx, &pb.UpdateAdminPermissionRequest{
|
|
|
|
+ PermissionId: permissionID,
|
|
|
|
+ Description: req.Description,
|
|
|
|
+ Roles: req.Roles,
|
|
|
|
+ })
|
|
|
|
+ if err != nil {
|
|
|
|
+ code, mgs := response.MicroErrorRequest(err)
|
|
|
|
+ c.JSON(code, response.ErrorResponse(code, mgs))
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ c.JSON(http.StatusOK, response.SuccessResponse(nil))
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// DeleteAdminPermission
|
|
|
|
+// @summary 删除权限
|
|
|
|
+// @Tags Permissions
|
|
|
|
+// @Accept json
|
|
|
|
+// @Produce json
|
|
|
|
+// @Param Authorization header string true "Bearer 用户令牌"
|
|
|
|
+// @Param id path int true "权限ID"
|
|
|
|
+// @Success 200 {object} response.ApiResponse "成功"
|
|
|
|
+// @Failure 400 {object} response.ApiResponse "请求错误"
|
|
|
|
+// @Failure 500 {object} response.ApiResponse "内部错误"
|
|
|
|
+// @Router /v1/api/admin/permission/{permissionID} [delete]
|
|
|
|
+func (svc *AdminPermission) DeleteAdminPermission(c *gin.Context) {
|
|
|
|
+ permissionID, err := strconv.ParseInt(c.Param("permissionID"), 10, 64)
|
|
|
|
+ if err != nil || permissionID <= 0 {
|
|
|
|
+ c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, common.ErrorMessage[common.InvalidRoleID]))
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ ctx, _ := utils.CreateContextWithToken(c, "authorizationservice", "AdminRoleService.DeleteAdminRole")
|
|
|
|
+ _, err = svc.Service.DeleteAdminPermission(ctx, &pb.DeleteAdminPermissionRequest{
|
|
|
|
+ PermissionId: permissionID,
|
|
|
|
+ })
|
|
|
|
+ if err != nil {
|
|
|
|
+ code, mgs := response.MicroErrorRequest(err)
|
|
|
|
+ c.JSON(code, response.ErrorResponse(code, mgs))
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ c.JSON(http.StatusOK, response.SuccessResponse(nil))
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+type ToggleRequest struct {
|
|
|
|
+ Status base.Status `json:"status" binding:"required"` // "enabled"|"disabled"
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// ToggleAdminPermission
|
|
|
|
+// @summary 启用/禁用
|
|
|
|
+// @Tags Permissions
|
|
|
|
+// @Accept json
|
|
|
|
+// @Produce json
|
|
|
|
+// @Param Authorization header string true "Bearer 用户令牌"
|
|
|
|
+// @Param body body ToggleRequest true "请求body"
|
|
|
|
+// @Success 200 {object} response.ApiResponse "成功"
|
|
|
|
+// @Failure 400 {object} response.ApiResponse "请求错误"
|
|
|
|
+// @Failure 500 {object} response.ApiResponse "内部错误"
|
|
|
|
+// @Router /v1/api/admin/permission/{permissionID}/toggle [put]
|
|
|
|
+func (svc *AdminPermission) ToggleAdminPermission(c *gin.Context) {
|
|
|
|
+ var req ToggleRequest
|
|
|
|
+ if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
+ c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, err.Error()))
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ permissionID, err := strconv.ParseInt(c.Param("permissionID"), 10, 64)
|
|
|
|
+ if err != nil || permissionID <= 0 {
|
|
|
|
+ c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, common.ErrorMessage[common.InvalidRoleID]))
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ enum, err := base.MapStatusToEnum(req.Status)
|
|
|
|
+ if err != nil {
|
|
|
|
+ c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, common.ErrorMessage[common.StatusIsNotValid]))
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ ctx, _ := utils.CreateContextWithToken(c, "authorizationservice", "AdminPermissionService.ToggleAdminPermission")
|
|
|
|
+ _, err = svc.Service.ToggleAdminPermission(ctx, &pb.ToggleAdminPermissionRequest{
|
|
|
|
+ PermissionId: permissionID,
|
|
|
|
+ Status: enum,
|
|
|
|
+ })
|
|
|
|
+ if err != nil {
|
|
|
|
+ code, mgs := response.MicroErrorRequest(err)
|
|
|
|
+ c.JSON(code, response.ErrorResponse(code, mgs))
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ c.JSON(http.StatusOK, response.SuccessResponse(nil))
|
|
|
|
+}
|