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.

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