广告平台(站长使用)
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.
 
 
 
 
 

188 lines
4.8 KiB

  1. package svc
  2. import (
  3. "applet/app/md"
  4. "applet/app/utils"
  5. "applet/app/utils/cache"
  6. db "code.fnuoos.com/zhimeng/model.git/src"
  7. "code.fnuoos.com/zhimeng/model.git/src/implement"
  8. "code.fnuoos.com/zhimeng/model.git/src/model"
  9. "encoding/json"
  10. "errors"
  11. "github.com/gin-gonic/gin"
  12. "regexp"
  13. "strings"
  14. "time"
  15. )
  16. func CheckUserRole(c *gin.Context, cacheKey, uri string, admId int) (isHasPermission bool, err error) {
  17. uri = utils.UriFilterExcludeQueryString(uri) // 去除uri中?后的query参数
  18. isHasPermission = false
  19. var rolePermission []string
  20. var rolePermissionString string
  21. rolePermissionString, _ = cache.GetString(cacheKey)
  22. // TODO::判断是否在白名单中
  23. if utils.InArr(uri, md.WhiteUri) {
  24. isHasPermission = true
  25. return
  26. }
  27. if rolePermissionString != "" {
  28. // if false {
  29. if err = json.Unmarshal([]byte(rolePermissionString), &rolePermission); err != nil {
  30. return
  31. }
  32. } else {
  33. adminDb := implement.NewAdminDb(db.DBs[GetMasterId(c)])
  34. list, _, err1 := adminDb.GetAdminRolePermission(admId)
  35. if err1 != nil {
  36. return isHasPermission, err1
  37. }
  38. for _, v := range list {
  39. rolePermission = append(rolePermission, v.Permission.Action)
  40. }
  41. marshal, err1 := json.Marshal(rolePermission)
  42. if err1 != nil {
  43. return isHasPermission, err1
  44. }
  45. rolePermissionString = string(marshal)
  46. _, err = cache.SetEx(cacheKey, rolePermissionString, md.AdminRolePermissionCacheTime)
  47. }
  48. if utils.InArr(uri, rolePermission) {
  49. isHasPermission = true
  50. } else {
  51. // 正则匹配占位符情况
  52. compileRegex := regexp.MustCompile("[0-9]+")
  53. matchArr := compileRegex.FindAllString(uri, -1)
  54. if len(matchArr) > 0 {
  55. uri = strings.Replace(uri, matchArr[len(matchArr)-1], ":id", 1)
  56. if utils.InArr(uri, rolePermission) {
  57. isHasPermission = true
  58. }
  59. }
  60. }
  61. return
  62. }
  63. func DeleteRole(c *gin.Context, roleId int) (err error) {
  64. engine := db.DBs[GetMasterId(c)]
  65. session := engine.NewSession()
  66. defer session.Close()
  67. session.Begin()
  68. // 1、删除 `role`
  69. roleDb := implement.NewRoleDb(engine, roleId)
  70. _, err = roleDb.RoleDeleteBySession(session, roleId)
  71. if err != nil {
  72. _ = session.Rollback()
  73. return
  74. }
  75. // 2、删除 `role_permission_group`
  76. rolePermissionGroupDb := implement.NewRolePermissionGroupDb(db.DBs[GetMasterId(c)])
  77. _, err = rolePermissionGroupDb.RolePermissionGroupDeleteForRoleBySession(session, roleId)
  78. if err != nil {
  79. _ = session.Rollback()
  80. return
  81. }
  82. // 3、删除 `admin_role`
  83. adminRoleDb := implement.NewAdminRoleDb(db.DBs[GetMasterId(c)])
  84. _, err = adminRoleDb.AdminRoleDeleteForRoleBySession(session, roleId)
  85. if err != nil {
  86. _ = session.Rollback()
  87. return
  88. }
  89. return session.Commit()
  90. }
  91. func RoleBindPermissionGroup(c *gin.Context, req md.RoleBindPermissionGroupReq) (err error) {
  92. engine := db.DBs[GetMasterId(c)]
  93. session := engine.NewSession()
  94. defer session.Close()
  95. session.Begin()
  96. // 1、查询 `role`
  97. roleDb := implement.NewRoleDb(db.DBs[GetMasterId(c)], req.RoleId)
  98. role, err := roleDb.GetRole()
  99. if err != nil {
  100. return
  101. }
  102. if role == nil {
  103. return errors.New("未查询到相应记录")
  104. }
  105. // 1、删除 `role_permission_group`
  106. rolePermissionGroupDb := implement.NewRolePermissionGroupDb(db.DBs[GetMasterId(c)])
  107. _, err = rolePermissionGroupDb.RolePermissionGroupDeleteForRoleBySession(session, req.RoleId)
  108. if err != nil {
  109. _ = session.Rollback()
  110. return
  111. }
  112. // 2、新增 `role_permission_group``
  113. var mm []*model.RolePermissionGroup
  114. now := time.Now()
  115. for _, v := range req.PermissionIds {
  116. mm = append(mm, &model.RolePermissionGroup{
  117. RoleId: role.Id,
  118. GroupId: v,
  119. CreateAt: now.Format("2006-01-02 15:04:05"),
  120. UpdateAt: now.Format("2006-01-02 15:04:05"),
  121. })
  122. }
  123. _, err = rolePermissionGroupDb.BatchAddRolePermissionGroupBySession(session, mm)
  124. if err != nil {
  125. _ = session.Rollback()
  126. return
  127. }
  128. return session.Commit()
  129. }
  130. func BindAdminRole(c *gin.Context, req md.BindAdminRoleReq) (err error) {
  131. engine := db.DBs[GetMasterId(c)]
  132. session := engine.NewSession()
  133. defer session.Close()
  134. session.Begin()
  135. // 1、查询 `role`
  136. adminDb := implement.NewAdminDb(db.DBs[GetMasterId(c)])
  137. role, err := adminDb.GetAdmin(req.AdmId)
  138. if err != nil {
  139. return
  140. }
  141. if role == nil {
  142. return errors.New("未查询到相应记录")
  143. }
  144. // 1、删除 `admin_role`
  145. adminRoleDb := implement.NewAdminRoleDb(db.DBs[GetMasterId(c)])
  146. _, err = adminRoleDb.AdminRoleDeleteBySession(session, req.AdmId)
  147. if err != nil {
  148. _ = session.Rollback()
  149. return
  150. }
  151. // 2、新增 `删除 `admin_role``
  152. var mm []*model.AdminRole
  153. now := time.Now()
  154. for _, v := range req.RoleIds {
  155. mm = append(mm, &model.AdminRole{
  156. AdmId: req.AdmId,
  157. RoleId: v,
  158. State: 1,
  159. CreateAt: now.Format("2006-01-02 15:04:05"),
  160. UpdateAt: now.Format("2006-01-02 15:04:05"),
  161. })
  162. }
  163. _, err = adminRoleDb.BatchAddAdminRoleBySession(session, mm)
  164. if err != nil {
  165. _ = session.Rollback()
  166. return
  167. }
  168. return session.Commit()
  169. }