146 lines
3.6 KiB
Go
146 lines
3.6 KiB
Go
package handlers
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/niangaodev/art-code/models"
|
|
"github.com/niangaodev/art-code/repositories"
|
|
)
|
|
|
|
// 获取角色列表
|
|
func AdminGetRoles(c *gin.Context) {
|
|
roles, err := repositories.GetRoles()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get roles"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, repositories.BuildRolesResponse(roles))
|
|
}
|
|
|
|
// 创建角色
|
|
func AdminCreateRole(c *gin.Context) {
|
|
var role models.Role
|
|
if err := c.ShouldBindJSON(&role); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
|
|
return
|
|
}
|
|
|
|
// 创建角色
|
|
if err := repositories.CreateRole(&role); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create role"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "Role created successfully"})
|
|
}
|
|
|
|
// 更新角色
|
|
func AdminUpdateRole(c *gin.Context) {
|
|
roleID := c.Param("id")
|
|
var role models.Role
|
|
if err := c.ShouldBindJSON(&role); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
|
|
return
|
|
}
|
|
|
|
// 转换角色ID为uint
|
|
var idUint uint
|
|
_, err := fmt.Sscanf(roleID, "%d", &idUint)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid role ID"})
|
|
return
|
|
}
|
|
|
|
// 设置角色ID
|
|
role.ID = idUint
|
|
|
|
// 更新角色
|
|
if err := repositories.UpdateRole(&role); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update role"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "Role updated successfully"})
|
|
}
|
|
|
|
// 删除角色
|
|
func AdminDeleteRole(c *gin.Context) {
|
|
roleID := c.Param("id")
|
|
|
|
// 转换角色ID为uint
|
|
var idUint uint
|
|
_, err := fmt.Sscanf(roleID, "%d", &idUint)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid role ID"})
|
|
return
|
|
}
|
|
|
|
// 删除角色
|
|
if err := repositories.DeleteRole(idUint); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete role"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "Role deleted successfully"})
|
|
}
|
|
|
|
// 获取所有权限列表
|
|
func AdminGetPermissions(c *gin.Context) {
|
|
permissions, err := repositories.GetPermissions()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get permissions"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, repositories.BuildPermissionsResponse(permissions))
|
|
}
|
|
|
|
// 更新角色权限请求结构
|
|
type UpdateRolePermissionsRequest struct {
|
|
PermissionIDs []uint `json:"permissionIds" binding:"required"`
|
|
}
|
|
|
|
// 更新角色的权限
|
|
func AdminUpdateRolePermissions(c *gin.Context) {
|
|
roleIDStr := c.Param("id")
|
|
|
|
// 转换角色ID
|
|
var roleID uint
|
|
id, err := strconv.ParseUint(roleIDStr, 10, 32)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid role ID"})
|
|
return
|
|
}
|
|
roleID = uint(id)
|
|
|
|
// 绑定请求数据
|
|
var req UpdateRolePermissionsRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request format"})
|
|
return
|
|
}
|
|
|
|
// 检查角色是否存在
|
|
role, err := repositories.GetRoleByID(roleID)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to check role existence"})
|
|
return
|
|
}
|
|
if role == nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Role not found"})
|
|
return
|
|
}
|
|
|
|
// 更新权限
|
|
if err := repositories.AssignPermissionsToRole(roleID, req.PermissionIDs); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update role permissions"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "Role permissions updated successfully"})
|
|
}
|