permission.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package handler
  2. import (
  3. "strings"
  4. "spider/internal/model"
  5. "spider/internal/store"
  6. "github.com/gin-gonic/gin"
  7. )
  8. // PermissionHandler handles role permission configuration.
  9. type PermissionHandler struct {
  10. store *store.Store
  11. }
  12. // ListAll handles GET /permissions — returns all role permissions + available keys
  13. func (h *PermissionHandler) ListAll(c *gin.Context) {
  14. var perms []model.RolePermission
  15. h.store.DB.Order("role ASC").Find(&perms)
  16. OK(c, gin.H{
  17. "roles": perms,
  18. "all_menus": model.AllMenuKeys(),
  19. "all_actions": model.AllActionKeys(),
  20. })
  21. }
  22. // Update handles PUT /permissions/:role — update permissions for a role
  23. func (h *PermissionHandler) Update(c *gin.Context) {
  24. role := c.Param("role")
  25. if role == "" {
  26. Fail(c, 400, "role is required")
  27. return
  28. }
  29. var body struct {
  30. Menus []string `json:"menus"`
  31. Actions []string `json:"actions"`
  32. }
  33. if err := c.ShouldBindJSON(&body); err != nil {
  34. Fail(c, 400, err.Error())
  35. return
  36. }
  37. menusStr := strings.Join(body.Menus, ",")
  38. actionsStr := strings.Join(body.Actions, ",")
  39. var perm model.RolePermission
  40. result := h.store.DB.Where("role = ?", role).First(&perm)
  41. if result.Error != nil {
  42. // Create new
  43. perm = model.RolePermission{
  44. Role: role,
  45. Menus: menusStr,
  46. Actions: actionsStr,
  47. }
  48. h.store.DB.Create(&perm)
  49. } else {
  50. h.store.DB.Model(&perm).Updates(map[string]any{
  51. "menus": menusStr,
  52. "actions": actionsStr,
  53. })
  54. h.store.DB.First(&perm, perm.ID)
  55. }
  56. LogAudit(h.store, c, "update", "permission", role, gin.H{"menus": body.Menus, "actions": body.Actions})
  57. OK(c, perm)
  58. }
  59. // Reset handles POST /permissions/reset — restore default permissions for all built-in roles
  60. func (h *PermissionHandler) Reset(c *gin.Context) {
  61. defaults := model.DefaultPermissions()
  62. for role, perm := range defaults {
  63. var existing model.RolePermission
  64. result := h.store.DB.Where("role = ?", role).First(&existing)
  65. if result.Error != nil {
  66. h.store.DB.Create(&model.RolePermission{
  67. Role: role,
  68. Menus: perm.Menus,
  69. Actions: perm.Actions,
  70. })
  71. } else {
  72. h.store.DB.Model(&existing).Updates(map[string]any{
  73. "menus": perm.Menus,
  74. "actions": perm.Actions,
  75. })
  76. }
  77. }
  78. LogAudit(h.store, c, "update", "permission", "all", gin.H{"action": "reset"})
  79. var perms []model.RolePermission
  80. h.store.DB.Order("role ASC").Find(&perms)
  81. OK(c, perms)
  82. }
  83. // GetMyPermissions handles GET /auth/permissions — returns current user's permissions
  84. func (h *PermissionHandler) GetMyPermissions(c *gin.Context) {
  85. role := c.GetString("role")
  86. var perm model.RolePermission
  87. if err := h.store.DB.Where("role = ?", role).First(&perm).Error; err != nil {
  88. // Fallback to defaults
  89. defaults := model.DefaultPermissions()
  90. if d, ok := defaults[role]; ok {
  91. OK(c, gin.H{
  92. "role": role,
  93. "menus": strings.Split(d.Menus, ","),
  94. "actions": strings.Split(d.Actions, ","),
  95. })
  96. return
  97. }
  98. // Unknown role, return empty
  99. OK(c, gin.H{"role": role, "menus": []string{}, "actions": []string{}})
  100. return
  101. }
  102. menus := []string{}
  103. if perm.Menus != "" {
  104. menus = strings.Split(perm.Menus, ",")
  105. }
  106. actions := []string{}
  107. if perm.Actions != "" {
  108. actions = strings.Split(perm.Actions, ",")
  109. }
  110. OK(c, gin.H{
  111. "role": role,
  112. "menus": menus,
  113. "actions": actions,
  114. })
  115. }