智慧食堂
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

184 Zeilen
4.2 KiB

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