diff --git a/internal/api/user_test.go b/internal/api/user_test.go index ed6c585af..310979d80 100644 --- a/internal/api/user_test.go +++ b/internal/api/user_test.go @@ -205,6 +205,12 @@ func (ts *UserTestSuite) TestUserUpdatePhoneAutoconfirmEnabled() { require.NoError(ts.T(), err) require.NoError(ts.T(), ts.API.db.Create(existingUser)) + idPhone, err := models.NewIdentity(existingUser, "phone", map[string]interface{}{ + "sub": "+29382983298", + }) + require.NoError(ts.T(), err) + require.NoError(ts.T(), ts.API.db.Create(idPhone)) + cases := []struct { desc string userData map[string]string diff --git a/internal/models/user.go b/internal/models/user.go index 0b3df4589..5fb9a9171 100644 --- a/internal/models/user.go +++ b/internal/models/user.go @@ -762,21 +762,19 @@ func IsDuplicatedPhone(tx *storage.Connection, phone, aud string) (bool, error) // 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) + query := `SELECT 1 FROM users + JOIN identities ON users.id = identities.user_id + WHERE users.phone = ? AND users.aud = ? AND identities.provider = ? + LIMIT 1` + + var exists bool + q := tx.RawQuery(query, phone, aud, "phone") + exists, err := q.Exists(&exists) 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 + + return exists, nil } // Ban a user for a given duration.