sys_authority.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. // @title CreateAuthority
  19. // @description create authority, 创建一个权限
  20. // @auth (2020/04/05 20:22 )
  21. // @param FileMd5 string
  22. // @param FileName string
  23. // @param FilePath string
  24. // @return error
  25. func (a *SysAuthority) CreateAuthority() (err error, authority *SysAuthority) {
  26. err = global.GVA_DB.Create(a).Error
  27. return err, a
  28. }
  29. // @title DeleteAuthority
  30. // @description 删除文件切片记录
  31. // @auth (2020/04/05 20:22 )
  32. // @param FileMd5 string
  33. // @param FileName string
  34. // @param FilePath string
  35. // @return error
  36. // 删除角色
  37. func (a *SysAuthority) DeleteAuthority() (err error) {
  38. err = global.GVA_DB.Where("authority_id = ?", a.AuthorityId).Find(&SysUser{}).Error
  39. if err != nil {
  40. err = global.GVA_DB.Where("parent_id = ?", a.AuthorityId).Find(&SysAuthority{}).Error
  41. if err != nil {
  42. err = global.GVA_DB.Where("authority_id = ?", a.AuthorityId).First(a).Unscoped().Delete(a).Error
  43. new(CasbinModel).ClearCasbin(0, a.AuthorityId)
  44. } else {
  45. err = errors.New("此角色存在子角色不允许删除")
  46. }
  47. } else {
  48. err = errors.New("此角色有用户正在使用禁止删除")
  49. }
  50. return err
  51. }
  52. // @title GetInfoList
  53. // @description 删除文件切片记录
  54. // @auth (2020/04/05 20:22 )
  55. // @param FileMd5 string
  56. // @param FileName string
  57. // @param FilePath string
  58. // @return error
  59. // 分页获取数据
  60. func (a *SysAuthority) GetInfoList(info PageInfo) (err error, list interface{}, total int) {
  61. limit := info.PageSize
  62. offset := info.PageSize * (info.Page - 1)
  63. db := global.GVA_DB
  64. if err != nil {
  65. return
  66. } else {
  67. var authority []SysAuthority
  68. err = db.Limit(limit).Offset(offset).Preload("DataAuthorityId").Where("parent_id = 0").Find(&authority).Error
  69. if len(authority) > 0 {
  70. for k, _ := range authority {
  71. err = findChildrenAuthority(&authority[k])
  72. }
  73. }
  74. return err, authority, total
  75. }
  76. }
  77. // @title findChildrenAuthority
  78. // @description 删除文件切片记录
  79. // @auth (2020/04/05 20:22 )
  80. // @param FileMd5 string
  81. // @param FileName string
  82. // @param FilePath string
  83. // @return error
  84. func findChildrenAuthority(authority *SysAuthority) (err error) {
  85. err = global.GVA_DB.Preload("DataAuthorityId").Where("parent_id = ?", authority.AuthorityId).Find(&authority.Children).Error
  86. if len(authority.Children) > 0 {
  87. for k, _ := range authority.Children {
  88. err = findChildrenAuthority(&authority.Children[k])
  89. }
  90. }
  91. return err
  92. }
  93. // @title SetDataAuthority
  94. // @description 删除文件切片记录
  95. // @auth (2020/04/05 20:22 )
  96. // @param FileMd5 string
  97. // @param FileName string
  98. // @param FilePath string
  99. // @return error
  100. func (a *SysAuthority) SetDataAuthority() error {
  101. var s SysAuthority
  102. global.GVA_DB.Preload("DataAuthorityId").First(&s, "authority_id = ?", a.AuthorityId)
  103. err := global.GVA_DB.Model(&s).Association("DataAuthorityId").Replace(&a.DataAuthorityId).Error
  104. return err
  105. }
  106. // @title SetMuneAuthority
  107. // @description 删除文件切片记录
  108. // @auth (2020/04/05 20:22 )
  109. // @param FileMd5 string
  110. // @param FileName string
  111. // @param FilePath string
  112. // @return error
  113. func (a *SysAuthority) SetMuneAuthority() error {
  114. var s SysAuthority
  115. global.GVA_DB.Preload("SysBaseMenus").First(&s, "authority_id = ?", a.AuthorityId)
  116. err := global.GVA_DB.Model(&s).Association("SysBaseMenus").Replace(&a.SysBaseMenus).Error
  117. return err
  118. }
  119. // @title GetAuthorityInfo
  120. // @description 删除文件切片记录
  121. // @auth (2020/04/05 20:22 )
  122. // @param FileMd5 string
  123. // @param FileName string
  124. // @param FilePath string
  125. // @return error
  126. func (a *SysAuthority) GetAuthorityInfo() (err error, sa SysAuthority) {
  127. err = global.GVA_DB.Preload("DataAuthorityId").Where("authority_id = ?", a.AuthorityId).First(&sa).Error
  128. return err, sa
  129. }