You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
253 lines
7.5 KiB
253 lines
7.5 KiB
package controller |
|
|
|
import ( |
|
"net/http" |
|
"strconv" |
|
|
|
"gofaster/internal/auth/model" |
|
"gofaster/internal/auth/service" |
|
"gofaster/internal/shared/response" |
|
|
|
"github.com/gin-gonic/gin" |
|
) |
|
|
|
type RoleController struct { |
|
roleService *service.RoleService |
|
} |
|
|
|
func NewRoleController(roleService *service.RoleService) *RoleController { |
|
return &RoleController{ |
|
roleService: roleService, |
|
} |
|
} |
|
|
|
// CreateRole 创建角色 |
|
// @Summary 创建角色 |
|
// @Description 创建新的角色 |
|
// @Tags 角色管理 |
|
// @Accept json |
|
// @Produce json |
|
// @Param role body model.Role true "角色信息" |
|
// @Success 200 {object} response.Response{data=model.Role} "创建成功" |
|
// @Failure 400 {object} response.Response "请求参数错误" |
|
// @Failure 500 {object} response.Response "服务器内部错误" |
|
// @Security BearerAuth |
|
// @Router /auth/roles [post] |
|
func (c *RoleController) CreateRole(ctx *gin.Context) { |
|
var role model.Role |
|
if err := ctx.ShouldBindJSON(&role); err != nil { |
|
response.Error(ctx, http.StatusBadRequest, "请求参数错误", err.Error()) |
|
return |
|
} |
|
|
|
if err := c.roleService.CreateRole(ctx.Request.Context(), &role); err != nil { |
|
response.Error(ctx, http.StatusInternalServerError, "创建角色失败", err.Error()) |
|
return |
|
} |
|
|
|
response.Success(ctx, "角色创建成功", role) |
|
} |
|
|
|
// UpdateRole 更新角色 |
|
// @Summary 更新角色 |
|
// @Description 根据ID更新角色信息 |
|
// @Tags 角色管理 |
|
// @Accept json |
|
// @Produce json |
|
// @Param id path int true "角色ID" |
|
// @Param role body model.Role true "角色信息" |
|
// @Success 200 {object} response.Response{data=model.Role} "更新成功" |
|
// @Failure 400 {object} response.Response "请求参数错误" |
|
// @Failure 500 {object} response.Response "服务器内部错误" |
|
// @Security BearerAuth |
|
// @Router /auth/roles/{id} [put] |
|
func (c *RoleController) UpdateRole(ctx *gin.Context) { |
|
idStr := ctx.Param("id") |
|
id, err := strconv.ParseUint(idStr, 10, 32) |
|
if err != nil { |
|
response.Error(ctx, http.StatusBadRequest, "请求参数错误", "无效的角色ID") |
|
return |
|
} |
|
|
|
var role model.Role |
|
if err := ctx.ShouldBindJSON(&role); err != nil { |
|
response.Error(ctx, http.StatusBadRequest, "请求参数错误", err.Error()) |
|
return |
|
} |
|
|
|
role.ID = uint(id) |
|
|
|
if err := c.roleService.UpdateRole(ctx.Request.Context(), &role); err != nil { |
|
response.Error(ctx, http.StatusInternalServerError, "更新角色失败", err.Error()) |
|
return |
|
} |
|
|
|
response.Success(ctx, "角色更新成功", role) |
|
} |
|
|
|
// DeleteRole 删除角色 |
|
// @Summary 删除角色 |
|
// @Description 根据ID删除角色 |
|
// @Tags 角色管理 |
|
// @Accept json |
|
// @Produce json |
|
// @Param id path int true "角色ID" |
|
// @Success 200 {object} response.Response "删除成功" |
|
// @Failure 400 {object} response.Response "请求参数错误" |
|
// @Failure 500 {object} response.Response "服务器内部错误" |
|
// @Security BearerAuth |
|
// @Router /auth/roles/{id} [delete] |
|
func (c *RoleController) DeleteRole(ctx *gin.Context) { |
|
idStr := ctx.Param("id") |
|
id, err := strconv.ParseUint(idStr, 10, 32) |
|
if err != nil { |
|
response.Error(ctx, http.StatusBadRequest, "请求参数错误", "无效的角色ID") |
|
return |
|
} |
|
|
|
if err := c.roleService.DeleteRole(ctx.Request.Context(), uint(id)); err != nil { |
|
response.Error(ctx, http.StatusInternalServerError, "删除角色失败", err.Error()) |
|
return |
|
} |
|
|
|
response.Success(ctx, "角色删除成功", nil) |
|
} |
|
|
|
// GetRole 获取角色详情 |
|
// @Summary 获取角色详情 |
|
// @Description 根据ID获取角色详细信息 |
|
// @Tags 角色管理 |
|
// @Accept json |
|
// @Produce json |
|
// @Param id path int true "角色ID" |
|
// @Success 200 {object} response.Response{data=model.Role} "获取成功" |
|
// @Failure 400 {object} response.Response "请求参数错误" |
|
// @Failure 404 {object} response.Response "角色不存在" |
|
// @Security BearerAuth |
|
// @Router /auth/roles/{id} [get] |
|
func (c *RoleController) GetRole(ctx *gin.Context) { |
|
idStr := ctx.Param("id") |
|
id, err := strconv.ParseUint(idStr, 10, 32) |
|
if err != nil { |
|
response.Error(ctx, http.StatusBadRequest, "请求参数错误", "无效的角色ID") |
|
return |
|
} |
|
|
|
role, err := c.roleService.GetRole(ctx.Request.Context(), uint(id)) |
|
if err != nil { |
|
response.Error(ctx, http.StatusNotFound, "角色不存在", err.Error()) |
|
return |
|
} |
|
|
|
response.Success(ctx, "获取角色成功", role) |
|
} |
|
|
|
// ListRoles 获取角色列表 |
|
// @Summary 获取角色列表 |
|
// @Description 分页获取角色列表 |
|
// @Tags 角色管理 |
|
// @Accept json |
|
// @Produce json |
|
// @Param page query int false "页码" default(1) |
|
// @Param pageSize query int false "每页数量" default(10) |
|
// @Success 200 {object} response.Response{data=object{data=[]model.Role,page=int,size=int,total=int}} "获取成功" |
|
// @Failure 500 {object} response.Response "服务器内部错误" |
|
// @Security BearerAuth |
|
// @Router /auth/roles [get] |
|
func (c *RoleController) ListRoles(ctx *gin.Context) { |
|
pageStr := ctx.DefaultQuery("page", "1") |
|
pageSizeStr := ctx.DefaultQuery("pageSize", "10") |
|
|
|
page, err := strconv.Atoi(pageStr) |
|
if err != nil || page < 1 { |
|
page = 1 |
|
} |
|
|
|
pageSize, err := strconv.Atoi(pageSizeStr) |
|
if err != nil || pageSize < 1 || pageSize > 100 { |
|
pageSize = 10 |
|
} |
|
|
|
roles, total, err := c.roleService.ListRoles(ctx.Request.Context(), page, pageSize) |
|
if err != nil { |
|
response.Error(ctx, http.StatusInternalServerError, "获取角色列表失败", err.Error()) |
|
return |
|
} |
|
|
|
response.Success(ctx, "获取角色列表成功", gin.H{ |
|
"data": roles, |
|
"page": page, |
|
"size": pageSize, |
|
"total": total, |
|
}) |
|
} |
|
|
|
// AssignRolesToUser 为用户分配角色 |
|
func (c *RoleController) AssignRolesToUser(ctx *gin.Context) { |
|
userIDStr := ctx.Param("userId") |
|
userID, err := strconv.ParseUint(userIDStr, 10, 32) |
|
if err != nil { |
|
response.Error(ctx, http.StatusBadRequest, "请求参数错误", "无效的用户ID") |
|
return |
|
} |
|
|
|
var request struct { |
|
RoleIDs []uint `json:"role_ids" binding:"required"` |
|
} |
|
|
|
if err := ctx.ShouldBindJSON(&request); err != nil { |
|
response.Error(ctx, http.StatusBadRequest, "请求参数错误", err.Error()) |
|
return |
|
} |
|
|
|
if err := c.roleService.AssignRolesToUser(ctx.Request.Context(), uint(userID), request.RoleIDs); err != nil { |
|
response.Error(ctx, http.StatusInternalServerError, "分配角色失败", err.Error()) |
|
return |
|
} |
|
|
|
response.Success(ctx, "角色分配成功", nil) |
|
} |
|
|
|
// GetUserRoles 获取用户的角色列表 |
|
func (c *RoleController) GetUserRoles(ctx *gin.Context) { |
|
userIDStr := ctx.Param("userId") |
|
userID, err := strconv.ParseUint(userIDStr, 10, 32) |
|
if err != nil { |
|
response.Error(ctx, http.StatusBadRequest, "请求参数错误", "无效的用户ID") |
|
return |
|
} |
|
|
|
roles, err := c.roleService.GetUserRoles(ctx.Request.Context(), uint(userID)) |
|
if err != nil { |
|
response.Error(ctx, http.StatusInternalServerError, "获取用户角色失败", err.Error()) |
|
return |
|
} |
|
|
|
response.Success(ctx, "获取用户角色成功", roles) |
|
} |
|
|
|
// RemoveRolesFromUser 从用户移除角色 |
|
func (c *RoleController) RemoveRolesFromUser(ctx *gin.Context) { |
|
userIDStr := ctx.Param("userId") |
|
userID, err := strconv.ParseUint(userIDStr, 10, 32) |
|
if err != nil { |
|
response.Error(ctx, http.StatusBadRequest, "请求参数错误", "无效的用户ID") |
|
return |
|
} |
|
|
|
var request struct { |
|
RoleIDs []uint `json:"role_ids" binding:"required"` |
|
} |
|
|
|
if err := ctx.ShouldBindJSON(&request); err != nil { |
|
response.Error(ctx, http.StatusBadRequest, "请求参数错误", err.Error()) |
|
return |
|
} |
|
|
|
if err := c.roleService.RemoveRolesFromUser(ctx.Request.Context(), uint(userID), request.RoleIDs); err != nil { |
|
response.Error(ctx, http.StatusInternalServerError, "移除角色失败", err.Error()) |
|
return |
|
} |
|
|
|
response.Success(ctx, "角色移除成功", nil) |
|
}
|
|
|