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

svc_role.go 4.7 KiB

2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. session := db.Db.NewSession()
  65. defer session.Close()
  66. session.Begin()
  67. //1、删除 `role`
  68. roleDb := implement.NewRoleDb(db.DBs[GetMasterId(c)], roleId)
  69. _, err = roleDb.RoleDeleteBySession(session, roleId)
  70. if err != nil {
  71. _ = session.Rollback()
  72. return
  73. }
  74. //2、删除 `role_permission_group`
  75. rolePermissionGroupDb := implement.NewRolePermissionGroupDb(db.DBs[GetMasterId(c)])
  76. _, err = rolePermissionGroupDb.RolePermissionGroupDeleteForRoleBySession(session, roleId)
  77. if err != nil {
  78. _ = session.Rollback()
  79. return
  80. }
  81. //3、删除 `admin_role`
  82. adminRoleDb := implement.NewAdminRoleDb(db.DBs[GetMasterId(c)])
  83. _, err = adminRoleDb.AdminRoleDeleteForRoleBySession(session, roleId)
  84. if err != nil {
  85. _ = session.Rollback()
  86. return
  87. }
  88. return session.Commit()
  89. }
  90. func RoleBindPermissionGroup(c *gin.Context, req md.RoleBindPermissionGroupReq) (err error) {
  91. session := db.Db.NewSession()
  92. defer session.Close()
  93. session.Begin()
  94. //1、查询 `role`
  95. roleDb := implement.NewRoleDb(db.DBs[GetMasterId(c)], req.RoleId)
  96. role, err := roleDb.GetRole()
  97. if err != nil {
  98. return
  99. }
  100. if role == nil {
  101. return errors.New("未查询到相应记录")
  102. }
  103. //1、删除 `role_permission_group`
  104. rolePermissionGroupDb := implement.NewAgentRolePermissionGroupDb(db.DBs[GetMasterId(c)])
  105. _, err = rolePermissionGroupDb.AgentRolePermissionGroupDeleteForRoleBySession(session, req.RoleId)
  106. if err != nil {
  107. _ = session.Rollback()
  108. return
  109. }
  110. //2、新增 `role_permission_group``
  111. var mm []*model.AgentRolePermissionGroup
  112. now := time.Now()
  113. for _, v := range req.PermissionIds {
  114. mm = append(mm, &model.AgentRolePermissionGroup{
  115. RoleId: role.Id,
  116. GroupId: v,
  117. CreateAt: now.Format("2006-01-02 15:04:05"),
  118. UpdateAt: now.Format("2006-01-02 15:04:05"),
  119. })
  120. }
  121. _, err = rolePermissionGroupDb.BatchAddAgentRolePermissionGroupBySession(session, mm)
  122. if err != nil {
  123. _ = session.Rollback()
  124. return
  125. }
  126. return session.Commit()
  127. }
  128. func BindAdminRole(c *gin.Context, req md.BindAdminRoleReq) (err error) {
  129. session := db.Db.NewSession()
  130. defer session.Close()
  131. session.Begin()
  132. //1、查询 `role`
  133. adminDb := implement.NewAgentDb(db.DBs[GetMasterId(c)])
  134. role, err := adminDb.GetAgent(req.Id)
  135. if err != nil {
  136. return
  137. }
  138. if role == nil {
  139. return errors.New("未查询到相应记录")
  140. }
  141. //1、删除 `admin_role`
  142. adminRoleDb := implement.NewAgentWithRoleDb(db.DBs[GetMasterId(c)])
  143. _, err = adminRoleDb.AgentWithRoleDeleteBySession(session, req.Id)
  144. if err != nil {
  145. _ = session.Rollback()
  146. return
  147. }
  148. //2、新增 `删除 `admin_role``
  149. var mm []*model.AgentWithRole
  150. now := time.Now()
  151. for _, v := range req.RoleIds {
  152. mm = append(mm, &model.AgentWithRole{
  153. AgentId: req.Id,
  154. RoleId: v,
  155. State: 1,
  156. CreateAt: now.Format("2006-01-02 15:04:05"),
  157. UpdateAt: now.Format("2006-01-02 15:04:05"),
  158. })
  159. }
  160. _, err = adminRoleDb.BatchAddAgentWithRoleBySession(session, mm)
  161. if err != nil {
  162. _ = session.Rollback()
  163. return
  164. }
  165. return session.Commit()
  166. }