Skip to content

Commit

Permalink
fix: check for phone identity
Browse files Browse the repository at this point in the history
  • Loading branch information
J0 committed Oct 28, 2024
1 parent 158e473 commit 52d641d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (a *API) UserUpdate(w http.ResponseWriter, r *http.Request) error {
}

if params.Phone != "" && user.GetPhone() != params.Phone {
if exists, err := models.IsDuplicatedPhone(db, params.Phone, aud); err != nil {
if exists, err := models.HasPhoneIdentity(db, params.Phone, aud); err != nil {
return internalServerError("Database error checking phone").WithInternalError(err)
} else if exists {
return unprocessableEntityError(ErrorCodePhoneExists, DuplicatePhoneMsg)
Expand Down
20 changes: 20 additions & 0 deletions internal/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,26 @@ func IsDuplicatedPhone(tx *storage.Connection, phone, aud string) (bool, error)
return true, nil
}

// TODO: Simplify this, check if it is safe to do this in admin create user too
// HasPhoneIdentity checks if the phone number already exists in the identities table
func HasPhoneIdentity(tx *storage.Connection, phone, aud string) (bool, error) {
user, err := FindUserByPhoneAndAudience(tx, phone, aud)
if err != nil {
if IsNotFoundError(err) {
return false, nil
}
return false, err
}
_, err = FindIdentityByIdAndProvider(tx, user.ID.String(), "phone")
if err != nil {
if IsNotFoundError(err) {
return false, nil
}
return false, err
}
return true, nil
}

// Ban a user for a given duration.
func (u *User) Ban(tx *storage.Connection, duration time.Duration) error {
if duration == time.Duration(0) {
Expand Down

0 comments on commit 52d641d

Please sign in to comment.