@@ -50,6 +50,39 @@ type User struct {
50
50
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
51
51
}
52
52
53
+ // User respresents a registered user with email/password authentication
54
+ type UserForExport struct {
55
+ InstanceID uuid.UUID `json:"-" db:"instance_id"`
56
+ ID uuid.UUID `json:"id" db:"id"`
57
+
58
+ Aud string `json:"aud" db:"aud"`
59
+ Role string `json:"role" db:"role"`
60
+ Email string `json:"email" db:"email"`
61
+ EncryptedPassword string `json:"encrypted_password" db:"encrypted_password"` // Exposing the encrypted password for an export.
62
+ ConfirmedAt * time.Time `json:"confirmed_at,omitempty" db:"confirmed_at"`
63
+ InvitedAt * time.Time `json:"invited_at,omitempty" db:"invited_at"`
64
+
65
+ ConfirmationToken string `json:"-" db:"confirmation_token"`
66
+ ConfirmationSentAt * time.Time `json:"confirmation_sent_at,omitempty" db:"confirmation_sent_at"`
67
+
68
+ RecoveryToken string `json:"-" db:"recovery_token"`
69
+ RecoverySentAt * time.Time `json:"recovery_sent_at,omitempty" db:"recovery_sent_at"`
70
+
71
+ EmailChangeToken string `json:"-" db:"email_change_token"`
72
+ EmailChange string `json:"new_email,omitempty" db:"email_change"`
73
+ EmailChangeSentAt * time.Time `json:"email_change_sent_at,omitempty" db:"email_change_sent_at"`
74
+
75
+ LastSignInAt * time.Time `json:"last_sign_in_at,omitempty" db:"last_sign_in_at"`
76
+
77
+ AppMetaData JSONMap `json:"app_metadata" db:"raw_app_meta_data"`
78
+ UserMetaData JSONMap `json:"user_metadata" db:"raw_user_meta_data"`
79
+
80
+ IsSuperAdmin bool `json:"-" db:"is_super_admin"`
81
+
82
+ CreatedAt time.Time `json:"created_at" db:"created_at"`
83
+ UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
84
+ }
85
+
53
86
// NewUser initializes a new user from an email, password and user data.
54
87
func NewUser (instanceID uuid.UUID , email , password , aud string , userData map [string ]interface {}) (* User , error ) {
55
88
id , err := uuid .NewV4 ()
@@ -320,6 +353,34 @@ func FindUsersInAudience(tx *storage.Connection, instanceID uuid.UUID, aud strin
320
353
return users , err
321
354
}
322
355
356
+ // FindUsersInAudience finds users with the matching audience.
357
+ func FindUsersForExportInAudience (tx * storage.Connection , instanceID uuid.UUID , aud string , pageParams * Pagination , sortParams * SortParams , filter string ) ([]* UserForExport , error ) {
358
+ users := []* UserForExport {}
359
+ q := tx .Q ().Where ("instance_id = ? and aud = ?" , instanceID , aud )
360
+
361
+ if filter != "" {
362
+ lf := "%" + filter + "%"
363
+ // we must specify the collation in order to get case insensitive search for the JSON column
364
+ q = q .Where ("(email LIKE ? OR raw_user_meta_data->>'$.full_name' COLLATE utf8mb4_unicode_ci LIKE ?)" , lf , lf )
365
+ }
366
+
367
+ if sortParams != nil && len (sortParams .Fields ) > 0 {
368
+ for _ , field := range sortParams .Fields {
369
+ q = q .Order (field .Name + " " + string (field .Dir ))
370
+ }
371
+ }
372
+
373
+ var err error
374
+ if pageParams != nil {
375
+ err = q .Paginate (int (pageParams .Page ), int (pageParams .PerPage )).All (& users )
376
+ pageParams .Count = uint64 (q .Paginator .TotalEntriesSize )
377
+ } else {
378
+ err = q .All (& users )
379
+ }
380
+
381
+ return users , err
382
+ }
383
+
323
384
// IsDuplicatedEmail returns whether a user exists with a matching email and audience.
324
385
func IsDuplicatedEmail (tx * storage.Connection , instanceID uuid.UUID , email , aud string ) (bool , error ) {
325
386
_ , err := FindUserByEmailAndAudience (tx , instanceID , email , aud )
0 commit comments