diff --git a/changelogs/unreleased/24606-force-password-reset-on-next-login.yml b/changelogs/unreleased/24606-force-password-reset-on-next-login.yml new file mode 100644 index 0000000000000000000000000000000000000000..fd671d04a9f50b5cc5b4aef20ca0be0972ee68e5 --- /dev/null +++ b/changelogs/unreleased/24606-force-password-reset-on-next-login.yml @@ -0,0 +1,4 @@ +--- +title: Force new password after password reset via API +merge_request: +author: George Andrinopoulos diff --git a/doc/api/users.md b/doc/api/users.md index 28b6c7bd491cb9881b2f091cc5e2c8f575f0072f..fea9bdf9639dd37da8112eb89f7ed95ba7c1741d 100644 --- a/doc/api/users.md +++ b/doc/api/users.md @@ -271,6 +271,7 @@ Parameters: - `can_create_group` (optional) - User can create groups - true or false - `external` (optional) - Flags the user as external - true or false(default) +On password update, user will be forced to change it upon next login. Note, at the moment this method does only return a `404` error, even in cases where a `409` (Conflict) would be more appropriate, e.g. when renaming the email address to some existing one. diff --git a/lib/api/users.rb b/lib/api/users.rb index 11a7368b4c0f4a6872be41764d1724725a59c4c0..0ed468626b7c13bdbe393320bda05f1caf62ef2e 100644 --- a/lib/api/users.rb +++ b/lib/api/users.rb @@ -160,6 +160,8 @@ module API end end + user_params.merge!(password_expires_at: Time.now) if user_params[:password].present? + if user.update_attributes(user_params.except(:extern_uid, :provider)) present user, with: Entities::UserPublic else diff --git a/spec/requests/api/users_spec.rb b/spec/requests/api/users_spec.rb index 5bf5bf0739e4b871c32097239a93a2bf636ff505..8692f9da976eb149ad5757288f367bd323dbcf09 100644 --- a/spec/requests/api/users_spec.rb +++ b/spec/requests/api/users_spec.rb @@ -305,6 +305,13 @@ describe API::Users, api: true do expect(user.reload.bio).to eq('new test bio') end + it "updates user with new password and forces reset on next login" do + put api("/users/#{user.id}", admin), password: '12345678' + + expect(response).to have_http_status(200) + expect(user.reload.password_expires_at).to be <= Time.now + end + it "updates user with organization" do put api("/users/#{user.id}", admin), { organization: 'GitLab' }