面包店
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.

пре 8 месеци
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package svc
  2. import (
  3. "applet/app/admin/md"
  4. "applet/app/db"
  5. "applet/app/db/model"
  6. md2 "applet/app/md"
  7. "applet/app/utils"
  8. "applet/app/utils/cache"
  9. "encoding/json"
  10. "errors"
  11. "regexp"
  12. "strings"
  13. "time"
  14. )
  15. func CheckUserRole(cacheKey, uri string, admId int) (isHasPermission bool, err error) {
  16. uri = utils.UriFilterExcludeQueryString(uri) //去除uri中?后的query参数
  17. isHasPermission = false
  18. var rolePermission []string
  19. var rolePermissionString string
  20. rolePermissionString, _ = cache.GetString(cacheKey)
  21. //TODO::判断是否在白名单中
  22. if utils.InArr(uri, md2.WhiteUri) {
  23. isHasPermission = true
  24. return
  25. }
  26. //if rolePermissionString != "" {
  27. if false {
  28. if err = json.Unmarshal([]byte(rolePermissionString), &rolePermission); err != nil {
  29. return
  30. }
  31. } else {
  32. adminDb := db.AdminDb{}
  33. adminDb.Set()
  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(roleId int) (err error) {
  64. session := db.Db.NewSession()
  65. defer session.Close()
  66. session.Begin()
  67. //1、删除 `role`
  68. roleDb := db.RoleDb{}
  69. roleDb.Set(roleId)
  70. _, err = roleDb.RoleDeleteBySession(session, roleId)
  71. if err != nil {
  72. _ = session.Rollback()
  73. return
  74. }
  75. //2、删除 `role_permission_group`
  76. rolePermissionGroupDb := db.RolePermissionGroupDb{}
  77. rolePermissionGroupDb.Set()
  78. _, err = rolePermissionGroupDb.RolePermissionGroupDeleteForRoleBySession(session, roleId)
  79. if err != nil {
  80. _ = session.Rollback()
  81. return
  82. }
  83. //3、删除 `admin_role`
  84. adminRoleDb := db.AdminRoleDb{}
  85. adminRoleDb.Set()
  86. _, err = adminRoleDb.AdminRoleDeleteForRoleBySession(session, roleId)
  87. if err != nil {
  88. _ = session.Rollback()
  89. return
  90. }
  91. return session.Commit()
  92. }
  93. func RoleBindPermissionGroup(req md.RoleBindPermissionGroupReq) (err error) {
  94. session := db.Db.NewSession()
  95. defer session.Close()
  96. session.Begin()
  97. //1、查询 `role`
  98. roleDb := db.RoleDb{}
  99. roleDb.Set(req.RoleId)
  100. role, err := roleDb.GetRole()
  101. if err != nil {
  102. return
  103. }
  104. if role == nil {
  105. return errors.New("未查询到相应记录")
  106. }
  107. //1、删除 `role_permission_group`
  108. rolePermissionGroupDb := db.RolePermissionGroupDb{}
  109. rolePermissionGroupDb.Set()
  110. _, err = rolePermissionGroupDb.RolePermissionGroupDeleteForRoleBySession(session, req.RoleId)
  111. if err != nil {
  112. _ = session.Rollback()
  113. return
  114. }
  115. //2、新增 `role_permission_group``
  116. var mm []*model.RolePermissionGroup
  117. now := time.Now()
  118. for _, v := range req.PermissionIds {
  119. mm = append(mm, &model.RolePermissionGroup{
  120. RoleId: role.Id,
  121. GroupId: v,
  122. CreateAt: now.Format("2006-01-02 15:04:05"),
  123. UpdateAt: now.Format("2006-01-02 15:04:05"),
  124. })
  125. }
  126. _, err = rolePermissionGroupDb.BatchAddRolePermissionGroupBySession(session, mm)
  127. if err != nil {
  128. _ = session.Rollback()
  129. return
  130. }
  131. return session.Commit()
  132. }
  133. func BindAdminRole(req md.BindAdminRoleReq) (err error) {
  134. session := db.Db.NewSession()
  135. defer session.Close()
  136. session.Begin()
  137. //1、查询 `role`
  138. adminDb := db.AdminDb{}
  139. adminDb.Set()
  140. role, err := adminDb.GetAdmin(req.AdmId)
  141. if err != nil {
  142. return
  143. }
  144. if role == nil {
  145. return errors.New("未查询到相应记录")
  146. }
  147. //1、删除 `admin_role`
  148. adminRoleDb := db.AdminRoleDb{}
  149. adminRoleDb.Set()
  150. _, err = adminRoleDb.AdminRoleDeleteBySession(session, req.AdmId)
  151. if err != nil {
  152. _ = session.Rollback()
  153. return
  154. }
  155. //2、新增 `删除 `admin_role``
  156. var mm []*model.AdminRole
  157. now := time.Now()
  158. for _, v := range req.RoleIds {
  159. mm = append(mm, &model.AdminRole{
  160. AdmId: req.AdmId,
  161. RoleId: v,
  162. State: 1,
  163. CreateAt: now.Format("2006-01-02 15:04:05"),
  164. UpdateAt: now.Format("2006-01-02 15:04:05"),
  165. })
  166. }
  167. _, err = adminRoleDb.BatchAddAdminRoleBySession(session, mm)
  168. if err != nil {
  169. _ = session.Rollback()
  170. return
  171. }
  172. return session.Commit()
  173. }