Skip to content

Commit

Permalink
is: Update user registry
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaspcr committed Oct 3, 2023
1 parent 8e48f26 commit 38ae9d4
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions pkg/identityserver/user_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,12 +430,9 @@ func (is *IdentityServer) updateUser(ctx context.Context, req *ttnpb.UpdateUserR
}

if !updatedByAdmin {
cleanContactInfo(req.User.ContactInfo)
req.User.PrimaryEmailAddressValidatedAt = nil
req.FieldMask.Paths = ttnpb.AddFields(req.FieldMask.GetPaths(), "primary_email_address_validated_at")
if len(req.User.ContactInfo) > 0 {
cleanContactInfo(req.User.ContactInfo)
req.FieldMask.Paths = ttnpb.AddFields(req.FieldMask.GetPaths(), "contact_info")
}
}

if ttnpb.HasAnyField(req.FieldMask.GetPaths(), "state") {
Expand Down Expand Up @@ -474,6 +471,9 @@ func (is *IdentityServer) updateUser(ctx context.Context, req *ttnpb.UpdateUserR
defer func() { is.setFullProfilePictureURL(ctx, usr) }()
}

updatingPrimaryEmailAddress := ttnpb.HasAnyField(req.FieldMask.GetPaths(), "primary_email_address")
updatingContactInfo := ttnpb.HasAnyField(req.FieldMask.GetPaths(), "contact_info")

err = is.store.Transact(ctx, func(ctx context.Context, st store.Store) (err error) {
if ttnpb.HasAnyField(req.FieldMask.GetPaths(), "admin") {
if err := isLastAdmin(ctx, st, req.User.Ids); err != nil {
Expand All @@ -482,16 +482,13 @@ func (is *IdentityServer) updateUser(ctx context.Context, req *ttnpb.UpdateUserR
}
}

updatingPrimaryEmailAddress := ttnpb.HasAnyField(req.FieldMask.GetPaths(), "primary_email_address")
updatingContactInfo := ttnpb.HasAnyField(req.FieldMask.GetPaths(), "contact_info")
var contactInfo []*ttnpb.ContactInfo

if updatingContactInfo {
contactInfo, err = st.SetContactInfo(ctx, req.User.GetIds(), req.User.ContactInfo)
if err != nil {
return err
}
usr.ContactInfo = contactInfo
}

if updatingPrimaryEmailAddress {
Expand All @@ -508,38 +505,34 @@ func (is *IdentityServer) updateUser(ctx context.Context, req *ttnpb.UpdateUserR
}
oldEmailAddress := oldUser.PrimaryEmailAddress

contactInfoChange := false
newEmailInContactInfo := false
foundOldEmailAddress := false
for _, contactInfo := range contactInfo {
if contactInfo.ContactMethod != ttnpb.ContactMethod_CONTACT_METHOD_EMAIL {
continue
}
if contactInfo.Value == req.User.PrimaryEmailAddress {
newEmailInContactInfo = true
}
if contactInfo.Value == oldEmailAddress {
foundOldEmailAddress = true

if updatedByAdmin {
req.User.PrimaryEmailAddressValidatedAt = contactInfo.ValidatedAt
req.FieldMask.Paths = ttnpb.AddFields(
req.FieldMask.GetPaths(), "primary_email_address_validated_at",
)
}

if req.User.PrimaryEmailAddress != contactInfo.Value {
contactInfoChange = true
contactInfo.Value = req.User.PrimaryEmailAddress
contactInfo.ValidatedAt = req.User.PrimaryEmailAddressValidatedAt
}
contactInfo.Value = req.User.PrimaryEmailAddress
contactInfo.ValidatedAt = req.User.PrimaryEmailAddressValidatedAt
break
}
}

if !contactInfoChange && !newEmailInContactInfo {
contactInfo = append(contactInfo, &ttnpb.ContactInfo{
// If the old email was not found on the contact info list it replaces everything with the new email.
if !foundOldEmailAddress {
contactInfo = []*ttnpb.ContactInfo{{
ContactMethod: ttnpb.ContactMethod_CONTACT_METHOD_EMAIL,
Value: req.User.PrimaryEmailAddress,
ValidatedAt: req.User.PrimaryEmailAddressValidatedAt,
})
}}
}
contactInfo, err = st.SetContactInfo(ctx, req.User.GetIds(), contactInfo)
if err != nil {
Expand All @@ -551,6 +544,7 @@ func (is *IdentityServer) updateUser(ctx context.Context, req *ttnpb.UpdateUserR
if err != nil {
return err
}

return nil
})
if err != nil {
Expand All @@ -571,9 +565,10 @@ func (is *IdentityServer) updateUser(ctx context.Context, req *ttnpb.UpdateUserR
})
}

// If primary email address was updated and invalidated, send a new validation email.
if usr.PrimaryEmailAddressValidatedAt == nil &&
ttnpb.HasAnyField(req.FieldMask.Paths, "primary_email_address_validated_at") {
// NOTE: The reason we validate the `updatingPrimaryEmailAddress` is because all changes on the primary email imply
// in a indirect changed to the contact info list. And if not validated the same is reflected on the contact info
// and a new validation should be requested.
if updatingPrimaryEmailAddress && usr.PrimaryEmailAddressValidatedAt == nil {
if _, err := is.requestContactInfoValidation(ctx, req.User.GetIds().GetEntityIdentifiers()); err != nil {
log.FromContext(ctx).WithError(err).Error("Could not send contact info validations")
}
Expand Down

0 comments on commit 38ae9d4

Please sign in to comment.