service.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "go-micro.dev/v4/auth"
  7. "go-micro.dev/v4/metadata"
  8. "reflect"
  9. req "sghgogs.com/sghblog/authorization-service/domain/model/request"
  10. "sghgogs.com/sghblog/authorization-service/domain/repository"
  11. pb "sghgogs.com/sghblog/authorization-service/proto"
  12. "sghgogs.com/sghblog/authorization-service/utils/authutil"
  13. "sghgogs.com/sghblog/common/errorcode"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. type IService interface {
  19. AdminLogin(string, string) (pb.AdminUser, error)
  20. GetAdminUser(int64) (*pb.AdminUser, error)
  21. ListAdminUsers(*pb.ListAdminUsersRequest) ([]*pb.AdminUser, int64, error)
  22. CreateAdminUser(*pb.CreateAdminUserRequest) error
  23. DeleteAdminUser(context.Context, int64) error
  24. UpdateAdminUser(*pb.UpdateAdminUserRequest) error
  25. ToggleAdminUser(*pb.ToggleAdminUserRequest) error
  26. RetrieveEnabledUsers() ([]*pb.AdminUser, error)
  27. // RevokeAdminUserWithRole(context.Context, *pb.RevokeAdminUserWithRoleRequest) error
  28. // GetUnassignedAdminRoles(int64) ([]*pb.AdminRole, int64, error)
  29. // GetAdminUserAssociatedRoles(string) ([]*pb.AdminBase, error)
  30. GetAdminRole(int64) (*pb.AdminRole, error)
  31. CreateAdminRole(context.Context, *pb.CreateAdminRoleRequest) error
  32. UpdateAdminRole(*pb.UpdateAdminRoleRequest) error
  33. ListAdminRoles(*pb.ListAdminRolesRequest) ([]*pb.AdminRole, int64, error)
  34. RetrieveEnabledRoles() ([]*pb.AdminRole, error)
  35. ToggleAdminRole(*pb.ToggleAdminRoleRequest) error
  36. DeleteAdminRole(int64) error
  37. // AssociateUserWithRole(context.Context, *pb.AssociateAdminUserWithRoleRequest) error
  38. // UpdateAdminUserProfileInfoRequest(*pb.UpdateAdminUserProfileInfoRequest) error
  39. }
  40. func ConvertTimeToInt64(t time.Time) int64 {
  41. return t.Unix()
  42. }
  43. func ConvertInt64ToTime(seconds int64) time.Time {
  44. return time.Unix(seconds, 0)
  45. }
  46. func FindStructDiff(oldData, newData interface{}) (string, string) {
  47. // var diffs []Diff
  48. // Convert structs to JSON for easy comparison
  49. oldJSON, _ := json.Marshal(oldData)
  50. newJSON, _ := json.Marshal(newData)
  51. // Unmarshal JSON back to map for easier comparison
  52. var oldMap, newMap map[string]interface{}
  53. _ = json.Unmarshal(oldJSON, &oldMap)
  54. _ = json.Unmarshal(newJSON, &newMap)
  55. // Compare the fields
  56. old_data := make(map[string]interface{})
  57. new_data := make(map[string]interface{})
  58. for key, oldValue := range oldMap {
  59. if _, ok := newMap[key]; ok {
  60. newValue, exists := newMap[key]
  61. if !exists || !reflect.DeepEqual(oldValue, newValue) {
  62. old_data[key] = oldValue
  63. new_data[key] = newValue
  64. }
  65. }
  66. }
  67. oldStr, _ := json.Marshal(old_data)
  68. newStr, _ := json.Marshal(new_data)
  69. fmt.Println(string(oldStr), string(newStr))
  70. return string(oldStr), string(newStr)
  71. }
  72. func ParseMetadata(ctx context.Context) (int64, string, string, string) {
  73. md, b := metadata.FromContext(ctx)
  74. if !b {
  75. return 0, "", "", ""
  76. }
  77. authHeader, ok := md["Authorization"]
  78. if !ok || !strings.HasPrefix(authHeader, auth.BearerScheme) {
  79. }
  80. token := strings.TrimPrefix(authHeader, auth.BearerScheme)
  81. inspect, _ := authutil.JWTAuthService.Inspect(token)
  82. fmt.Println("inspect", inspect)
  83. var userId int64
  84. if id, isOk := inspect.Metadata["user_id"]; isOk {
  85. ID, _ := strconv.ParseInt(id, 0, 64)
  86. userId = ID
  87. }
  88. createdBy := ""
  89. if username, isOk := inspect.Metadata["username"]; isOk {
  90. createdBy = username
  91. }
  92. IPAddress := ""
  93. if len(md["Local"]) > 0 {
  94. IPAddress = md["Local"]
  95. } else {
  96. IPAddress = md["Remote"]
  97. }
  98. return userId, createdBy, IPAddress, md["Agent"]
  99. }
  100. func NewService(r repository.IRepository) IService {
  101. return &Service{
  102. Repository: r,
  103. }
  104. }
  105. type Service struct {
  106. Repository repository.IRepository
  107. }
  108. func (s *Service) CreatedAuditLog(ctx context.Context, action, tableName string, recordID int64, oldData, newData string) (string, error) {
  109. userId, createdBy, IPAddress, agent := ParseMetadata(ctx)
  110. log := req.AuditLog{
  111. UserID: userId,
  112. Action: action,
  113. TableName: tableName,
  114. RecordID: recordID,
  115. OldData: oldData,
  116. NewData: newData,
  117. IPAddress: IPAddress,
  118. UserAgent: agent,
  119. CreatedAt: time.Now(),
  120. }
  121. if err := s.Repository.CreateAuditLog(&log); err != nil {
  122. return createdBy, errorcode.New("authorization service", err.Error(), 500)
  123. }
  124. return createdBy, nil
  125. }