mng_role.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package service
  2. import (
  3. "context"
  4. "sort"
  5. "go-common/library/log"
  6. model "go-common/app/admin/main/macross/model/manager"
  7. )
  8. // Role get role.
  9. func (s *Service) Role(c context.Context, system string) (res []*model.Role) {
  10. for _, role := range s.role[system] {
  11. if role == nil {
  12. continue
  13. }
  14. role.Auths = make(map[string]*model.Auth)
  15. if authIDs, ok := s.authRelation[role.RoleID]; ok {
  16. if auths, ok := s.auth[system]; ok {
  17. for _, authID := range authIDs {
  18. if auth, ok := auths[authID]; ok {
  19. role.Auths[auth.AuthFlag] = auth
  20. }
  21. }
  22. }
  23. }
  24. res = append(res, role)
  25. }
  26. sort.Sort(model.Roles(res))
  27. return
  28. }
  29. // SaveRole save role.
  30. func (s *Service) SaveRole(c context.Context, roleID int64, system, roleName string) (err error) {
  31. var rows int64
  32. if roleID == 0 {
  33. if rows, err = s.dao.AddRole(c, system, roleName); err != nil {
  34. log.Error("s.dao.AddRole(%s, %s) error(%v)", system, roleName, err)
  35. return
  36. }
  37. } else {
  38. if rows, err = s.dao.UpRole(c, roleName, roleID); err != nil {
  39. log.Error("s.dao.UpRole(%s, %d) error(%v)", roleName, roleID, err)
  40. return
  41. }
  42. }
  43. if rows != 0 {
  44. // update cache
  45. s.loadRoleCache()
  46. }
  47. return
  48. }
  49. // DelRole del role.
  50. func (s *Service) DelRole(c context.Context, roleID int64) (err error) {
  51. var rows int64
  52. if rows, err = s.dao.DelRole(c, roleID); err != nil {
  53. log.Error("s.dao.DelRole(%d) error(%s)", roleID, err)
  54. return
  55. } else if rows != 0 {
  56. // update cache
  57. s.loadRoleCache()
  58. if rows, err = s.dao.CleanAuthRelationByRole(c, roleID); err != nil {
  59. log.Error("s.dao.CleanAuthRelationByRole(%d) error(%s)", roleID, err)
  60. return
  61. } else if rows != 0 {
  62. s.loadAuthRelationCache()
  63. }
  64. }
  65. return
  66. }