Add admin API endpoint to delete a user's phone number

What does this MR do and why?

Adds an admin-only REST endpoint, DELETE /api/v4/users/:id/phone_number, that deletes the phone number a user provided for identity verification.

Until now, admins could only remove a phone number through the Rails console.

  • Only EE identity verification creates these records, so the endpoint lives in EE (ee/lib/ee/api/users.rb). It follows the existing DELETE /users/:id/identities/:provider endpoint.
  • It's available only where identity verification is available (Gitlab::Saas.feature_available?(:identity_verification)). Everywhere else it returns 404.
  • It requires an administrator in admin mode. It returns 204 on success, 404 when the user doesn't exist or has no phone number, and 412 on a stale If-Unmodified-Since header.
  • It records a phone_number_validation_deleted audit event against the user. Deleting the record removes anti-abuse signals: it resets SMS send throttling, removes the number from the banned-user match, and can require the user to verify a phone number again.
  • It adds a delete_phone_number_validation permission for fine-grained tokens. The token can be scoped to the instance, and only admins on GitLab.com can be assigned it.

Database

The endpoint loads the user's phone number validation and deletes it. Both statements are primary key lookups on user_phone_number_validations. The users lookup uses the existing find_users_by_id function.

1. Load the user's phone number validation

SELECT "user_phone_number_validations"."user_id", "user_phone_number_validations"."validated_at", "user_phone_number_validations"."created_at", "user_phone_number_validations"."updated_at", "user_phone_number_validations"."international_dial_code", "user_phone_number_validations"."risk_score", "user_phone_number_validations"."country", "user_phone_number_validations"."phone_number", "user_phone_number_validations"."telesign_reference_xid", "user_phone_number_validations"."sms_sent_at", "user_phone_number_validations"."sms_send_count" FROM "user_phone_number_validations" WHERE "user_phone_number_validations"."user_id" = <user_id> LIMIT 1

Plan: https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/109338/commands/214084 (Index Scan on user_phone_number_validations_pkey, 1 row, 3.5 ms cold, buffers: 6 hit / 1 read)

2. Delete it

DELETE FROM "user_phone_number_validations" WHERE "user_phone_number_validations"."user_id" = <user_id>

Plan: https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/109338/commands/214085 (Index Scan on user_phone_number_validations_pkey, 1 row, 0.7 ms warm, buffers: 9 hit / 0 read)

References

Screenshots or screen recordings

Not applicable. This is an API-only change.

How to set up and validate locally

  1. Simulate GitLab.com: set GITLAB_SIMULATE_SAAS=1 in env.runit and restart GDK.
  2. In the Rails console (gdk rails console), give a user a phone number record:
    user = User.find_by_username('root')
    Users::PhoneNumberValidation.create!(user: user, country: 'US', international_dial_code: 1, phone_number: '5555555555')
  3. Using an admin personal access token with the api scope, delete the user's phone number:
    curl --request DELETE \
      --header "PRIVATE-TOKEN: <admin_token>" \
      --url "http://gdk.test:3000/api/v4/users/<user_id>/phone_number"
    It returns 204 No Content.
  4. In the Rails console, check that user.reload.phone_number_validation is nil.
  5. Run the same request again. It returns 404 Phone number Not Found.
  6. In Admin area > Monitoring > Audit events, check for a Deleted phone number used for identity verification event on the user.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

🤖 Generated with Claude Code

Edited by Jay

Merge request reports

Loading
Loading