2023-08-30 09:28:21 +08:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2023-10-12 10:10:23 +08:00
|
|
|
"log/slog"
|
2023-08-30 09:28:21 +08:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
jwt "github.com/appleboy/gin-jwt/v2"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"joylink.club/bj-rtsts-server/dto"
|
|
|
|
"joylink.club/bj-rtsts-server/middleware"
|
|
|
|
"joylink.club/bj-rtsts-server/service"
|
|
|
|
)
|
|
|
|
|
|
|
|
func InitAuthRouter(api *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
|
|
|
|
authed := api.Group("/v1/auth").Use(authMiddleware.MiddlewareFunc(), middleware.PermissMiddleware)
|
|
|
|
authed.GET("/role/paging", pageQueryRole)
|
|
|
|
authed.GET("/role/list", listQueryRole)
|
|
|
|
authed.POST("/role", createRole)
|
|
|
|
authed.GET("/role/:id", queryRoleInfo)
|
|
|
|
authed.PUT("/role/:id", updateRoleInfo)
|
|
|
|
authed.DELETE("/role/:id", deleteRoleInfo)
|
|
|
|
authed.GET("/path/paging", pageQueryApiPath)
|
|
|
|
authed.GET("/path/list", listQueryApiPath)
|
|
|
|
authed.POST("/path", createApiPath)
|
|
|
|
authed.GET("/path/:id", queryApiPathInfo)
|
|
|
|
authed.PUT("/path/:id", updateApiPathInfo)
|
|
|
|
authed.DELETE("/path/:id", deleteApiPathInfo)
|
|
|
|
authed.POST("/userLinkRole", assignRoleToUser)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 分页查询角色信息
|
|
|
|
//
|
|
|
|
// @Summary 分页查询角色信息
|
|
|
|
//
|
|
|
|
// @Security JwtAuth
|
|
|
|
//
|
|
|
|
// @Description 分页查询角色信息
|
|
|
|
// @Tags 权限Api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param PageQueryDto query dto.PageQueryDto true "分页信息"
|
|
|
|
// @Success 200 {object} dto.PageDto
|
|
|
|
// @Failure 401 {object} dto.ErrorDto
|
|
|
|
// @Failure 404 {object} dto.ErrorDto
|
|
|
|
// @Failure 500 {object} dto.ErrorDto
|
|
|
|
// @Router /api/v1/auth/role/paging [get]
|
|
|
|
func pageQueryRole(c *gin.Context) {
|
|
|
|
req := dto.PageQueryDto{}
|
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: err.Error()})
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
|
|
|
page, err := service.PageAuthRoleQuery(&req)
|
|
|
|
if err != nil {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err.Error()})
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, page)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询角色列表
|
|
|
|
//
|
|
|
|
// @Summary 查询角色列表
|
|
|
|
//
|
|
|
|
// @Security JwtAuth
|
|
|
|
//
|
|
|
|
// @Description 查询角色列表
|
|
|
|
// @Tags 权限Api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} dto.AuthRoleRspDto
|
|
|
|
// @Failure 401 {object} dto.ErrorDto
|
|
|
|
// @Failure 404 {object} dto.ErrorDto
|
|
|
|
// @Failure 500 {object} dto.ErrorDto
|
|
|
|
// @Router /api/v1/auth/role/list [get]
|
|
|
|
func listQueryRole(c *gin.Context) {
|
|
|
|
page := service.ListAuthRoleQuery()
|
|
|
|
c.JSON(http.StatusOK, page)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 创建角色
|
|
|
|
//
|
|
|
|
// @Summary 创建角色
|
|
|
|
//
|
|
|
|
// @Security JwtAuth
|
|
|
|
//
|
|
|
|
// @Description 创建角色
|
|
|
|
// @Tags 权限Api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param AuthRoleReqDto query dto.AuthRoleReqDto true "创建角色"
|
|
|
|
// @Success 200 {object} bool
|
|
|
|
// @Failure 401 {object} dto.ErrorDto
|
|
|
|
// @Failure 404 {object} dto.ErrorDto
|
|
|
|
// @Failure 500 {object} dto.ErrorDto
|
2023-08-31 11:07:22 +08:00
|
|
|
// @Router /api/v1/auth/role [post]
|
2023-08-30 09:28:21 +08:00
|
|
|
func createRole(c *gin.Context) {
|
|
|
|
req := dto.AuthRoleReqDto{}
|
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: err.Error()})
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
2023-10-12 10:10:23 +08:00
|
|
|
slog.Debug("保存数据", req)
|
2023-08-30 09:28:21 +08:00
|
|
|
c.JSON(http.StatusOK, service.CreateAuthRole(&req))
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询角色详情
|
|
|
|
//
|
|
|
|
// @Summary 查询角色详情
|
|
|
|
//
|
|
|
|
// @Security JwtAuth
|
|
|
|
//
|
|
|
|
// @Description 查询角色详情
|
|
|
|
// @Tags 权限Api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path int true "角色ID"
|
|
|
|
// @Success 200 {object} dto.AuthRoleDetailRspDto
|
|
|
|
// @Failure 401 {object} dto.ErrorDto
|
|
|
|
// @Failure 404 {object} dto.ErrorDto
|
|
|
|
// @Failure 500 {object} dto.ErrorDto
|
2023-08-31 11:07:22 +08:00
|
|
|
// @Router /api/v1/auth/role/{id} [get]
|
2023-08-30 09:28:21 +08:00
|
|
|
func queryRoleInfo(c *gin.Context) {
|
|
|
|
id, exist := c.Params.Get("id")
|
|
|
|
if !exist {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: "必要参数id不存在"})
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
2023-10-12 10:10:23 +08:00
|
|
|
slog.Debug("传入参数id为" + id)
|
2023-08-30 09:28:21 +08:00
|
|
|
int64Id, _ := strconv.ParseInt(id, 10, 64)
|
|
|
|
c.JSON(http.StatusOK, service.QueryAuthRole(int32(int64Id)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// 修改角色信息
|
|
|
|
//
|
|
|
|
// @Summary 修改角色信息
|
|
|
|
//
|
|
|
|
// @Security JwtAuth
|
|
|
|
//
|
|
|
|
// @Description 修改角色信息
|
|
|
|
// @Tags 权限Api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path int true "角色信息ID"
|
|
|
|
// @Param authRoleReqDto query dto.AuthRoleReqDto true "修改的角色信息"
|
|
|
|
// @Success 200 {object} bool
|
|
|
|
// @Failure 401 {object} dto.ErrorDto
|
|
|
|
// @Failure 404 {object} dto.ErrorDto
|
|
|
|
// @Failure 500 {object} dto.ErrorDto
|
2023-08-31 11:07:22 +08:00
|
|
|
// @Router /api/v1/auth/role/{id} [put]
|
2023-08-30 09:28:21 +08:00
|
|
|
func updateRoleInfo(c *gin.Context) {
|
|
|
|
id, exist := c.Params.Get("id")
|
|
|
|
if !exist {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: "必要参数id不存在"})
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
2023-10-12 10:10:23 +08:00
|
|
|
slog.Debug("传入参数id为" + id)
|
2023-08-30 09:28:21 +08:00
|
|
|
req := dto.AuthRoleReqDto{}
|
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: err.Error()})
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
|
|
|
int64Id, _ := strconv.ParseInt(id, 10, 64)
|
2023-08-30 13:25:57 +08:00
|
|
|
rid := int32(int64Id)
|
|
|
|
result := service.UpdateAuthRole(rid, &req)
|
|
|
|
if result {
|
|
|
|
middleware.ClearUserPermissionByRid(rid)
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, result)
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 删除角色信息
|
|
|
|
//
|
|
|
|
// @Summary 删除角色信息
|
|
|
|
//
|
|
|
|
// @Security JwtAuth
|
|
|
|
//
|
|
|
|
// @Description 删除角色信息
|
|
|
|
// @Tags 权限Api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path int true "角色ID"
|
|
|
|
// @Success 200 {object} bool
|
|
|
|
// @Failure 401 {object} dto.ErrorDto
|
|
|
|
// @Failure 404 {object} dto.ErrorDto
|
|
|
|
// @Failure 500 {object} dto.ErrorDto
|
2023-08-31 11:07:22 +08:00
|
|
|
// @Router /api/v1/auth/role/{id} [delete]
|
2023-08-30 09:28:21 +08:00
|
|
|
func deleteRoleInfo(c *gin.Context) {
|
|
|
|
id, exist := c.Params.Get("id")
|
|
|
|
if !exist {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: "必要参数id不存在"})
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
2023-10-12 10:10:23 +08:00
|
|
|
slog.Debug("传入参数id为" + id)
|
2023-08-30 09:28:21 +08:00
|
|
|
int64Id, _ := strconv.ParseInt(id, 10, 64)
|
2023-08-30 13:25:57 +08:00
|
|
|
rid := int32(int64Id)
|
|
|
|
result := service.DeleteAuthRole(rid)
|
|
|
|
if result {
|
|
|
|
middleware.ClearUserPermissionByRid(rid)
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, result)
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 分页查询接口路径信息
|
|
|
|
//
|
|
|
|
// @Summary 分页查询接口路径信息
|
|
|
|
//
|
|
|
|
// @Security JwtAuth
|
|
|
|
//
|
|
|
|
// @Description 分页查询接口路径信息
|
|
|
|
// @Tags 权限Api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param authApiPathPageReqDto query dto.AuthApiPathPageReqDto true "分页信息"
|
|
|
|
// @Success 200 {object} dto.PageDto
|
|
|
|
// @Failure 401 {object} dto.ErrorDto
|
|
|
|
// @Failure 404 {object} dto.ErrorDto
|
|
|
|
// @Failure 500 {object} dto.ErrorDto
|
|
|
|
// @Router /api/v1/auth/path/paging [get]
|
|
|
|
func pageQueryApiPath(c *gin.Context) {
|
|
|
|
req := dto.AuthApiPathPageReqDto{}
|
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: err.Error()})
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
|
|
|
page, err := service.PageAuthApiPathQuery(&req)
|
|
|
|
if err != nil {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err.Error()})
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, page)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询接口路径列表
|
|
|
|
//
|
|
|
|
// @Summary 查询接口路径列表
|
|
|
|
//
|
|
|
|
// @Security JwtAuth
|
|
|
|
//
|
|
|
|
// @Description 查询接口路径列表
|
|
|
|
// @Tags 权限Api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} model.AuthAPIPath
|
|
|
|
// @Failure 401 {object} dto.ErrorDto
|
|
|
|
// @Failure 404 {object} dto.ErrorDto
|
|
|
|
// @Failure 500 {object} dto.ErrorDto
|
|
|
|
// @Router /api/v1/auth/path/list [get]
|
|
|
|
func listQueryApiPath(c *gin.Context) {
|
|
|
|
c.JSON(http.StatusOK, service.ListAuthApiPathQuery())
|
|
|
|
}
|
|
|
|
|
|
|
|
// 创建接口路径
|
|
|
|
//
|
|
|
|
// @Summary 创建接口路径
|
|
|
|
//
|
|
|
|
// @Security JwtAuth
|
|
|
|
//
|
|
|
|
// @Description 创建接口路径
|
|
|
|
// @Tags 权限Api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param authApiPathReqDto query dto.AuthApiPathReqDto true "创建路径信息"
|
|
|
|
// @Success 200 {object} bool
|
|
|
|
// @Failure 401 {object} dto.ErrorDto
|
|
|
|
// @Failure 404 {object} dto.ErrorDto
|
|
|
|
// @Failure 500 {object} dto.ErrorDto
|
|
|
|
// @Router /api/v1/auth/path [post]
|
|
|
|
func createApiPath(c *gin.Context) {
|
|
|
|
req := dto.AuthApiPathReqDto{}
|
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: err.Error()})
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
2023-10-12 10:10:23 +08:00
|
|
|
slog.Debug("保存数据", req)
|
2023-08-30 09:28:21 +08:00
|
|
|
c.JSON(http.StatusOK, service.CreateAuthApiPath(&req))
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询接口路径
|
|
|
|
//
|
|
|
|
// @Summary 查询接口路径
|
|
|
|
//
|
|
|
|
// @Security JwtAuth
|
|
|
|
//
|
|
|
|
// @Description 查询接口路径
|
|
|
|
// @Tags 权限Api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path int true "接口路径ID"
|
|
|
|
// @Success 200 {object} model.AuthAPIPath
|
|
|
|
// @Failure 401 {object} dto.ErrorDto
|
|
|
|
// @Failure 404 {object} dto.ErrorDto
|
|
|
|
// @Failure 500 {object} dto.ErrorDto
|
|
|
|
// @Router /api/v1/auth/path/{id} [get]
|
|
|
|
func queryApiPathInfo(c *gin.Context) {
|
|
|
|
id, exist := c.Params.Get("id")
|
|
|
|
if !exist {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: "必要参数id不存在"})
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
2023-10-12 10:10:23 +08:00
|
|
|
slog.Debug("传入参数id为" + id)
|
2023-08-30 09:28:21 +08:00
|
|
|
int64Id, _ := strconv.ParseInt(id, 10, 64)
|
|
|
|
c.JSON(http.StatusOK, service.QueryAuthApiPath(int32(int64Id)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// 修改接口路径
|
|
|
|
//
|
|
|
|
// @Summary 修改接口路径
|
|
|
|
//
|
|
|
|
// @Security JwtAuth
|
|
|
|
//
|
|
|
|
// @Description 修改接口路径
|
|
|
|
// @Tags 权限Api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path int true "接口路径ID"
|
|
|
|
// @Param authApiPathReqDto query dto.AuthApiPathReqDto true "修改的接口路径"
|
|
|
|
// @Success 200 {object} bool
|
|
|
|
// @Failure 401 {object} dto.ErrorDto
|
|
|
|
// @Failure 404 {object} dto.ErrorDto
|
|
|
|
// @Failure 500 {object} dto.ErrorDto
|
|
|
|
// @Router /api/v1/auth/path/{id} [put]
|
|
|
|
func updateApiPathInfo(c *gin.Context) {
|
|
|
|
id, exist := c.Params.Get("id")
|
|
|
|
if !exist {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: "必要参数id不存在"})
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
|
|
|
req := dto.AuthApiPathReqDto{}
|
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: err.Error()})
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
|
|
|
int64Id, _ := strconv.ParseInt(id, 10, 64)
|
|
|
|
c.JSON(http.StatusOK, service.UpdateAuthApiPath(int32(int64Id), &req))
|
|
|
|
}
|
|
|
|
|
|
|
|
// 删除接口路径
|
|
|
|
//
|
|
|
|
// @Summary 删除接口路径
|
|
|
|
//
|
|
|
|
// @Security JwtAuth
|
|
|
|
//
|
|
|
|
// @Description 删除接口路径
|
|
|
|
// @Tags 权限Api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path int true "接口路径ID"
|
|
|
|
// @Success 200 {object} bool
|
|
|
|
// @Failure 401 {object} dto.ErrorDto
|
|
|
|
// @Failure 404 {object} dto.ErrorDto
|
|
|
|
// @Failure 500 {object} dto.ErrorDto
|
|
|
|
// @Router /api/v1/auth/path/{id} [delete]
|
|
|
|
func deleteApiPathInfo(c *gin.Context) {
|
|
|
|
id, exist := c.Params.Get("id")
|
|
|
|
if !exist {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: "必要参数id不存在"})
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
2023-10-12 10:10:23 +08:00
|
|
|
slog.Debug("传入参数id为" + id)
|
2023-08-30 09:28:21 +08:00
|
|
|
int64Id, _ := strconv.ParseInt(id, 10, 64)
|
|
|
|
c.JSON(http.StatusOK, service.DeleteAuthApiPath(int32(int64Id)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// 用户关联角色
|
|
|
|
//
|
|
|
|
// @Summary 用户关联角色
|
|
|
|
//
|
|
|
|
// @Security JwtAuth
|
|
|
|
//
|
|
|
|
// @Description 用户关联角色
|
|
|
|
// @Tags 权限Api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param authRoleUserReqDto query dto.AuthRoleUserReqDto true "关联角色信息"
|
|
|
|
// @Success 200 {object} bool
|
|
|
|
// @Failure 401 {object} dto.ErrorDto
|
|
|
|
// @Failure 404 {object} dto.ErrorDto
|
|
|
|
// @Failure 500 {object} dto.ErrorDto
|
|
|
|
// @Router /api/v1/auth/userLinkRole [post]
|
|
|
|
func assignRoleToUser(c *gin.Context) {
|
|
|
|
req := dto.AuthRoleUserReqDto{}
|
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: err.Error()})
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|
2023-08-30 13:25:57 +08:00
|
|
|
result := service.UserLinkRole(&req)
|
|
|
|
if result {
|
|
|
|
middleware.ClearUserPermission(req.Uid)
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, result)
|
2023-08-30 09:28:21 +08:00
|
|
|
}
|