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.
198 lines
5.4 KiB
198 lines
5.4 KiB
1 week ago
|
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 创建角色
|
||
|
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 更新角色
|
||
|
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 删除角色
|
||
|
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 获取角色详情
|
||
|
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 获取角色列表
|
||
|
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)
|
||
|
}
|