permission.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package model
  2. import "time"
  3. // RolePermission stores menu/feature permissions per role.
  4. type RolePermission struct {
  5. ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
  6. Role string `gorm:"size:20;uniqueIndex;not null" json:"role"` // admin / operator / viewer / custom roles
  7. Menus string `gorm:"type:text" json:"menus"` // comma-separated menu keys: dashboard,merchants,tasks,...
  8. Actions string `gorm:"type:text" json:"actions"` // comma-separated action keys: task_start,merchant_edit,...
  9. CreatedAt time.Time `json:"created_at"`
  10. UpdatedAt time.Time `json:"updated_at"`
  11. }
  12. func (RolePermission) TableName() string { return "role_permissions" }
  13. // AllMenuKeys returns all available menu keys in order.
  14. func AllMenuKeys() []map[string]string {
  15. return []map[string]string{
  16. {"key": "dashboard", "label": "数据看板"},
  17. {"key": "merchants", "label": "商户列表"},
  18. {"key": "merchants-raw", "label": "原始数据"},
  19. {"key": "groups", "label": "群组分析"},
  20. {"key": "channels", "label": "频道管理"},
  21. {"key": "analytics", "label": "数据分析"},
  22. {"key": "tasks", "label": "任务管理"},
  23. {"key": "keywords", "label": "关键词管理"},
  24. {"key": "tg-accounts", "label": "TG账号"},
  25. {"key": "proxies", "label": "代理管理"},
  26. {"key": "settings", "label": "分级设置"},
  27. {"key": "schedules", "label": "定时任务"},
  28. {"key": "notifications", "label": "通知管理"},
  29. {"key": "audit-logs", "label": "审计日志"},
  30. {"key": "users", "label": "用户管理"},
  31. }
  32. }
  33. // AllActionKeys returns all available action permission keys.
  34. func AllActionKeys() []map[string]string {
  35. return []map[string]string{
  36. {"key": "task_start", "label": "启动任务"},
  37. {"key": "task_stop", "label": "停止任务"},
  38. {"key": "merchant_edit", "label": "编辑商户"},
  39. {"key": "merchant_assign", "label": "分配商户"},
  40. {"key": "merchant_delete", "label": "删除商户"},
  41. {"key": "merchant_import", "label": "导入商户"},
  42. {"key": "merchant_export", "label": "导出商户"},
  43. {"key": "keyword_manage", "label": "管理关键词"},
  44. {"key": "schedule_manage", "label": "管理定时任务"},
  45. {"key": "user_manage", "label": "管理用户"},
  46. {"key": "setting_manage", "label": "管理设置"},
  47. }
  48. }
  49. // DefaultPermissions returns the default permission sets per built-in role.
  50. func DefaultPermissions() map[string]struct{ Menus, Actions string } {
  51. return map[string]struct{ Menus, Actions string }{
  52. "admin": {
  53. Menus: "dashboard,merchants,merchants-raw,groups,channels,analytics,tasks,keywords,tg-accounts,proxies,settings,schedules,notifications,audit-logs,users",
  54. Actions: "task_start,task_stop,merchant_edit,merchant_assign,merchant_delete,merchant_import,merchant_export,keyword_manage,schedule_manage,user_manage,setting_manage",
  55. },
  56. "operator": {
  57. Menus: "dashboard,merchants,merchants-raw,groups,channels,analytics,tasks,keywords,tg-accounts,settings",
  58. Actions: "task_start,task_stop,merchant_edit,merchant_assign,merchant_import,merchant_export,keyword_manage",
  59. },
  60. "viewer": {
  61. Menus: "dashboard,merchants,merchants-raw,groups,channels,analytics",
  62. Actions: "merchant_export",
  63. },
  64. }
  65. }