|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"gofaster/internal/auth/controller"
|
|
|
|
"gofaster/internal/auth/repository"
|
|
|
|
"gofaster/internal/auth/service"
|
|
|
|
"gofaster/internal/shared/middleware"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func RegisterRoleRoutes(router *gin.RouterGroup, db *gorm.DB, jwtSecret string) {
|
|
|
|
// 初始化依赖
|
|
|
|
roleRepo := repository.NewRoleRepository(db)
|
|
|
|
roleService := service.NewRoleService(roleRepo)
|
|
|
|
roleController := controller.NewRoleController(roleService)
|
|
|
|
|
|
|
|
// 角色管理路由组 - 修复路径配置
|
|
|
|
roleGroup := router.Group("/auth/roles")
|
|
|
|
{
|
|
|
|
// 临时测试路由 - 不需要认证
|
|
|
|
testGroup := roleGroup.Group("/test")
|
|
|
|
{
|
|
|
|
testGroup.GET("", roleController.ListRoles) // 获取角色列表
|
|
|
|
testGroup.POST("", roleController.CreateRole) // 创建角色
|
|
|
|
testGroup.GET("/:id", roleController.GetRole) // 获取角色详情
|
|
|
|
testGroup.PUT("/:id", roleController.UpdateRole) // 更新角色
|
|
|
|
testGroup.DELETE("/:id", roleController.DeleteRole) // 删除角色
|
|
|
|
}
|
|
|
|
|
|
|
|
// 需要权限验证的路由
|
|
|
|
roleGroup.Use(middleware.AuthMiddleware(jwtSecret))
|
|
|
|
{
|
|
|
|
// 角色CRUD操作
|
|
|
|
roleGroup.GET("", roleController.ListRoles) // 获取角色列表
|
|
|
|
roleGroup.POST("", roleController.CreateRole) // 创建角色
|
|
|
|
roleGroup.GET("/:id", roleController.GetRole) // 获取角色详情
|
|
|
|
roleGroup.PUT("/:id", roleController.UpdateRole) // 更新角色
|
|
|
|
roleGroup.DELETE("/:id", roleController.DeleteRole) // 删除角色
|
|
|
|
|
|
|
|
// 角色分配相关
|
|
|
|
roleGroup.POST("/users/:userId/assign", roleController.AssignRolesToUser) // 为用户分配角色
|
|
|
|
roleGroup.GET("/users/:userId", roleController.GetUserRoles) // 获取用户的角色列表
|
|
|
|
roleGroup.DELETE("/users/:userId/remove", roleController.RemoveRolesFromUser) // 从用户移除角色
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|