Skip to content

Commit

Permalink
fix: adjust check to use phone identity
Browse files Browse the repository at this point in the history
  • Loading branch information
J0 committed Oct 29, 2024
1 parent 52d641d commit 09af278
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
6 changes: 6 additions & 0 deletions internal/api/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 10 additions & 12 deletions internal/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 09af278

Please sign in to comment.