permissions.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package e21_presents
  2. import (
  3. "net/http"
  4. "github.com/qor5/admin/presets"
  5. . "github.com/qor5/ui/vuetify"
  6. "github.com/qor5/web"
  7. "github.com/qor5/x/perm"
  8. h "github.com/theplant/htmlgo"
  9. "gorm.io/gorm"
  10. )
  11. // @snippet_begin(PresetsPermissionsSample)
  12. type User struct {
  13. ID uint
  14. Username string
  15. }
  16. type Group struct {
  17. ID uint
  18. Name string
  19. }
  20. func PresetsPermissions(b *presets.Builder) (
  21. cust *presets.ModelBuilder,
  22. cl *presets.ListingBuilder,
  23. ce *presets.EditingBuilder,
  24. dp *presets.DetailingBuilder,
  25. db *gorm.DB,
  26. ) {
  27. cust, cl, ce, dp, db = PresetsDetailPageCards(b)
  28. b.URIPrefix(PresetsPermissionsPath)
  29. b.ProfileFunc(func(ctx *web.EventContext) h.HTMLComponent {
  30. return VMenu(
  31. web.Slot(
  32. VBtn("").
  33. Icon(true).
  34. Attr("v-bind", "attrs", "v-on", "on").
  35. Children(
  36. VIcon("person"),
  37. ).Class("ml-2"),
  38. ).Name("activator").Scope("{ on, attrs }"),
  39. VList(
  40. VListItem(
  41. VListItemTitle(h.Text("Logout")),
  42. ),
  43. ),
  44. )
  45. })
  46. perm.Verbose = true
  47. b.Permission(perm.New().
  48. Policies(
  49. perm.PolicyFor("editor").WhoAre(perm.Allowed).ToDo(perm.Anything).On(perm.Anything),
  50. perm.PolicyFor("editor").WhoAre(perm.Denied).ToDo(presets.PermRead...).On("*user_management*"),
  51. perm.PolicyFor("editor").WhoAre(perm.Denied).
  52. ToDo(presets.PermCreate, presets.PermDelete).On("*customers*"),
  53. perm.PolicyFor("editor").WhoAre(perm.Denied).
  54. ToDo(presets.PermCreate, presets.PermUpdate).On("*companies*"),
  55. perm.PolicyFor("editor").WhoAre(perm.Denied).
  56. ToDo(presets.PermUpdate).On("*customers:*:company_id*"),
  57. perm.PolicyFor("editor").WhoAre(perm.Denied).
  58. ToDo("*bulk_actions:delete").On("*:customers*"),
  59. ).
  60. SubjectsFunc(func(r *http.Request) []string {
  61. return []string{"editor"}
  62. }))
  63. err := db.AutoMigrate(&User{}, &Group{})
  64. if err != nil {
  65. panic(err)
  66. }
  67. b.MenuGroup("User Management").SubItems("user", "group")
  68. b.Model(&User{})
  69. b.Model(&Group{})
  70. return
  71. }
  72. const PresetsPermissionsPath = "/samples/presets-permissions"
  73. // @snippet_end