You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
3.0 KiB

  1. package model
  2. import (
  3. "gin-vue-admin/global"
  4. "github.com/pkg/errors"
  5. "time"
  6. )
  7. type SysAuthority struct {
  8. CreatedAt time.Time
  9. UpdatedAt time.Time
  10. DeletedAt *time.Time `sql:"index"`
  11. AuthorityId string `json:"authorityId" gorm:"not null;unique;primary_key"`
  12. AuthorityName string `json:"authorityName"`
  13. ParentId string `json:"parentId"`
  14. DataAuthorityId []SysAuthority `json:"dataAuthorityId" gorm:"many2many:sys_data_authority_id;association_jointable_foreignkey:data_authority_id"`
  15. Children []SysAuthority `json:"children"`
  16. SysBaseMenus []SysBaseMenu `json:"menus" gorm:"many2many:sys_authority_menus;"`
  17. }
  18. // 创建角色
  19. func (a *SysAuthority) CreateAuthority() (err error, authority *SysAuthority) {
  20. err = global.GVA_DB.Create(a).Error
  21. return err, a
  22. }
  23. // 删除角色
  24. func (a *SysAuthority) DeleteAuthority() (err error) {
  25. err = global.GVA_DB.Where("authority_id = ?", a.AuthorityId).Find(&SysUser{}).Error
  26. if err != nil {
  27. err = global.GVA_DB.Where("parent_id = ?", a.AuthorityId).Find(&SysAuthority{}).Error
  28. if err != nil {
  29. err = global.GVA_DB.Preload("SysBaseMenus").Where("authority_id = ?", a.AuthorityId).First(a).Unscoped().Delete(a).Association("SysBaseMenus").Delete(a.SysBaseMenus).Error
  30. new(CasbinModel).clearCasbin(0, a.AuthorityId)
  31. } else {
  32. err = errors.New("此角色存在子角色不允许删除")
  33. }
  34. } else {
  35. err = errors.New("此角色有用户正在使用禁止删除")
  36. }
  37. return err
  38. }
  39. // 分页获取数据
  40. func (a *SysAuthority) GetInfoList(info PageInfo) (err error, list interface{}, total int) {
  41. limit := info.PageSize
  42. offset := info.PageSize * (info.Page - 1)
  43. db := global.GVA_DB
  44. if err != nil {
  45. return
  46. } else {
  47. var authority []SysAuthority
  48. err = db.Limit(limit).Offset(offset).Preload("DataAuthorityId").Where("parent_id = 0").Find(&authority).Error
  49. if len(authority) > 0 {
  50. for k, _ := range authority {
  51. err = findChildrenAuthority(&authority[k])
  52. }
  53. }
  54. return err, authority, total
  55. }
  56. }
  57. func findChildrenAuthority(authority *SysAuthority) (err error) {
  58. err = global.GVA_DB.Preload("DataAuthorityId").Where("parent_id = ?", authority.AuthorityId).Find(&authority.Children).Error
  59. if len(authority.Children) > 0 {
  60. for k, _ := range authority.Children {
  61. err = findChildrenAuthority(&authority.Children[k])
  62. }
  63. }
  64. return err
  65. }
  66. func (a *SysAuthority) SetDataAuthority() error {
  67. var s SysAuthority
  68. global.GVA_DB.Preload("DataAuthorityId").First(&s, "authority_id = ?", a.AuthorityId)
  69. err := global.GVA_DB.Model(&s).Association("DataAuthorityId").Replace(&a.DataAuthorityId).Error
  70. return err
  71. }
  72. func (a *SysAuthority) SetMuneAuthority() error {
  73. var s SysAuthority
  74. global.GVA_DB.Preload("SysBaseMenus").First(&s, "authority_id = ?", a.AuthorityId)
  75. err := global.GVA_DB.Model(&s).Association("SysBaseMenus").Replace(&a.SysBaseMenus).Error
  76. return err
  77. }
  78. func (a *SysAuthority) GetAuthorityInfo() (err error, sa SysAuthority) {
  79. err = global.GVA_DB.Preload("DataAuthorityId").Where("authority_id = ?", a.AuthorityId).First(&sa).Error
  80. return err, sa
  81. }