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 existingDELETE /users/:id/identities/:providerendpoint. - It's available only where identity verification is available (
Gitlab::Saas.feature_available?(:identity_verification)). Everywhere else it returns404. - It requires an administrator in admin mode. It returns
204on success,404when the user doesn't exist or has no phone number, and412on a staleIf-Unmodified-Sinceheader. - It records a
phone_number_validation_deletedaudit 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_validationpermission 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 1Plan: 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
- Simulate GitLab.com: set
GITLAB_SIMULATE_SAAS=1inenv.runitand restart GDK. - 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') - Using an admin personal access token with the
apiscope, delete the user's phone number:It returnscurl --request DELETE \ --header "PRIVATE-TOKEN: <admin_token>" \ --url "http://gdk.test:3000/api/v4/users/<user_id>/phone_number"204 No Content. - In the Rails console, check that
user.reload.phone_number_validationisnil. - Run the same request again. It returns
404 Phone number Not Found. - In Admin area > Monitoring > Audit events, check for a
Deleted phone number used for identity verificationevent 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.