authority.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package system
  2. import (
  3. "context"
  4. "github.com/pkg/errors"
  5. "gorm.io/gorm"
  6. sysModel "log-server/model/system"
  7. "log-server/service/system"
  8. )
  9. const initOrderAuthority = initOrderCasbin + 1
  10. type initAuthority struct{}
  11. // auto run
  12. func init() {
  13. system.RegisterInit(initOrderAuthority, &initAuthority{})
  14. }
  15. func (i *initAuthority) MigrateTable(ctx context.Context) (context.Context, error) {
  16. db, ok := ctx.Value("db").(*gorm.DB)
  17. if !ok {
  18. return ctx, system.ErrMissingDBContext
  19. }
  20. return ctx, db.AutoMigrate(&sysModel.SysAuthority{})
  21. }
  22. func (i *initAuthority) TableCreated(ctx context.Context) bool {
  23. db, ok := ctx.Value("db").(*gorm.DB)
  24. if !ok {
  25. return false
  26. }
  27. return db.Migrator().HasTable(&sysModel.SysAuthority{})
  28. }
  29. func (i initAuthority) InitializerName() string {
  30. return sysModel.SysAuthority{}.TableName()
  31. }
  32. func (i *initAuthority) InitializeData(ctx context.Context) (context.Context, error) {
  33. db, ok := ctx.Value("db").(*gorm.DB)
  34. if !ok {
  35. return ctx, system.ErrMissingDBContext
  36. }
  37. entities := []sysModel.SysAuthority{
  38. {AuthorityId: 888, AuthorityName: "普通用户", ParentId: 0, DefaultRouter: "dashboard"},
  39. {AuthorityId: 9528, AuthorityName: "测试角色", ParentId: 0, DefaultRouter: "dashboard"},
  40. {AuthorityId: 8881, AuthorityName: "普通用户子角色", ParentId: 888, DefaultRouter: "dashboard"},
  41. }
  42. if err := db.Create(&entities).Error; err != nil {
  43. return ctx, errors.Wrapf(err, "%s表数据初始化失败!", sysModel.SysAuthority{}.TableName())
  44. }
  45. // data authority
  46. if err := db.Model(&entities[0]).Association("DataAuthorityId").Replace(
  47. []*sysModel.SysAuthority{
  48. {AuthorityId: 888},
  49. {AuthorityId: 9528},
  50. {AuthorityId: 8881},
  51. }); err != nil {
  52. return ctx, errors.Wrapf(err, "%s表数据初始化失败!",
  53. db.Model(&entities[0]).Association("DataAuthorityId").Relationship.JoinTable.Name)
  54. }
  55. if err := db.Model(&entities[1]).Association("DataAuthorityId").Replace(
  56. []*sysModel.SysAuthority{
  57. {AuthorityId: 9528},
  58. {AuthorityId: 8881},
  59. }); err != nil {
  60. return ctx, errors.Wrapf(err, "%s表数据初始化失败!",
  61. db.Model(&entities[1]).Association("DataAuthorityId").Relationship.JoinTable.Name)
  62. }
  63. next := context.WithValue(ctx, i.InitializerName(), entities)
  64. return next, nil
  65. }
  66. func (i *initAuthority) DataInserted(ctx context.Context) bool {
  67. db, ok := ctx.Value("db").(*gorm.DB)
  68. if !ok {
  69. return false
  70. }
  71. if errors.Is(db.Where("authority_id = ?", "8881").
  72. First(&sysModel.SysAuthority{}).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据
  73. return false
  74. }
  75. return true
  76. }