123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- package service
- import (
- "context"
- "encoding/json"
- "fmt"
- "go-micro.dev/v4/auth"
- "go-micro.dev/v4/metadata"
- "reflect"
- req "sghgogs.com/sghblog/authorization-service/domain/model/request"
- "sghgogs.com/sghblog/authorization-service/domain/repository"
- pb "sghgogs.com/sghblog/authorization-service/proto"
- "sghgogs.com/sghblog/authorization-service/utils/authutil"
- "sghgogs.com/sghblog/common/errorcode"
- "strconv"
- "strings"
- "time"
- )
- type IService interface {
- AdminLogin(string, string) (pb.AdminUser, error)
- GetAdminUser(int64) (*pb.AdminUser, error)
- ListAdminUsers(*pb.ListAdminUsersRequest) ([]*pb.AdminUser, int64, error)
- CreateAdminUser(*pb.CreateAdminUserRequest) error
- DeleteAdminUser(context.Context, int64) error
- UpdateAdminUser(*pb.UpdateAdminUserRequest) error
- ToggleAdminUser(*pb.ToggleAdminUserRequest) error
- RetrieveEnabledUsers() ([]*pb.AdminUser, error)
- // RevokeAdminUserWithRole(context.Context, *pb.RevokeAdminUserWithRoleRequest) error
- // GetUnassignedAdminRoles(int64) ([]*pb.AdminRole, int64, error)
- // GetAdminUserAssociatedRoles(string) ([]*pb.AdminBase, error)
- GetAdminRole(int64) (*pb.AdminRole, error)
- CreateAdminRole(context.Context, *pb.CreateAdminRoleRequest) error
- UpdateAdminRole(*pb.UpdateAdminRoleRequest) error
- ListAdminRoles(*pb.ListAdminRolesRequest) ([]*pb.AdminRole, int64, error)
- RetrieveEnabledRoles() ([]*pb.AdminRole, error)
- ToggleAdminRole(*pb.ToggleAdminRoleRequest) error
- DeleteAdminRole(int64) error
- // AssociateUserWithRole(context.Context, *pb.AssociateAdminUserWithRoleRequest) error
- // UpdateAdminUserProfileInfoRequest(*pb.UpdateAdminUserProfileInfoRequest) error
- }
- func ConvertTimeToInt64(t time.Time) int64 {
- return t.Unix()
- }
- func ConvertInt64ToTime(seconds int64) time.Time {
- return time.Unix(seconds, 0)
- }
- func FindStructDiff(oldData, newData interface{}) (string, string) {
- // var diffs []Diff
- // Convert structs to JSON for easy comparison
- oldJSON, _ := json.Marshal(oldData)
- newJSON, _ := json.Marshal(newData)
- // Unmarshal JSON back to map for easier comparison
- var oldMap, newMap map[string]interface{}
- _ = json.Unmarshal(oldJSON, &oldMap)
- _ = json.Unmarshal(newJSON, &newMap)
- // Compare the fields
- old_data := make(map[string]interface{})
- new_data := make(map[string]interface{})
- for key, oldValue := range oldMap {
- if _, ok := newMap[key]; ok {
- newValue, exists := newMap[key]
- if !exists || !reflect.DeepEqual(oldValue, newValue) {
- old_data[key] = oldValue
- new_data[key] = newValue
- }
- }
- }
- oldStr, _ := json.Marshal(old_data)
- newStr, _ := json.Marshal(new_data)
- fmt.Println(string(oldStr), string(newStr))
- return string(oldStr), string(newStr)
- }
- func ParseMetadata(ctx context.Context) (int64, string, string, string) {
- md, b := metadata.FromContext(ctx)
- if !b {
- return 0, "", "", ""
- }
- authHeader, ok := md["Authorization"]
- if !ok || !strings.HasPrefix(authHeader, auth.BearerScheme) {
- }
- token := strings.TrimPrefix(authHeader, auth.BearerScheme)
- inspect, _ := authutil.JWTAuthService.Inspect(token)
- fmt.Println("inspect", inspect)
- var userId int64
- if id, isOk := inspect.Metadata["user_id"]; isOk {
- ID, _ := strconv.ParseInt(id, 0, 64)
- userId = ID
- }
- createdBy := ""
- if username, isOk := inspect.Metadata["username"]; isOk {
- createdBy = username
- }
- IPAddress := ""
- if len(md["Local"]) > 0 {
- IPAddress = md["Local"]
- } else {
- IPAddress = md["Remote"]
- }
- return userId, createdBy, IPAddress, md["Agent"]
- }
- func NewService(r repository.IRepository) IService {
- return &Service{
- Repository: r,
- }
- }
- type Service struct {
- Repository repository.IRepository
- }
- func (s *Service) CreatedAuditLog(ctx context.Context, action, tableName string, recordID int64, oldData, newData string) (string, error) {
- userId, createdBy, IPAddress, agent := ParseMetadata(ctx)
- log := req.AuditLog{
- UserID: userId,
- Action: action,
- TableName: tableName,
- RecordID: recordID,
- OldData: oldData,
- NewData: newData,
- IPAddress: IPAddress,
- UserAgent: agent,
- CreatedAt: time.Now(),
- }
- if err := s.Repository.CreateAuditLog(&log); err != nil {
- return createdBy, errorcode.New("authorization service", err.Error(), 500)
- }
- return createdBy, nil
- }
|