FIX: Allow staff to reset passwords by username (#15709)

When staff visits the user profile of another user, the `email` field
in the model is empty. In this case, staff cannot send the reset email
password because nothing is passed in the `login` field.

This commit changes the behavior for staff users to allow resetting
password by username instead.
This commit is contained in:
Dan Ungureanu 2022-01-26 10:39:58 +02:00 committed by GitHub
parent f43bba8d59
commit f5b94f152f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 2 deletions

View File

@ -429,7 +429,7 @@ const User = RestModel.extend({
changePassword() { changePassword() {
return ajax("/session/forgot_password", { return ajax("/session/forgot_password", {
dataType: "json", dataType: "json",
data: { login: this.email }, data: { login: this.email || this.username },
type: "POST", type: "POST",
}); });
}, },

View File

@ -434,7 +434,7 @@ class SessionController < ApplicationController
RateLimiter.new(nil, "forgot-password-hr-#{request.remote_ip}", 6, 1.hour).performed! RateLimiter.new(nil, "forgot-password-hr-#{request.remote_ip}", 6, 1.hour).performed!
RateLimiter.new(nil, "forgot-password-min-#{request.remote_ip}", 3, 1.minute).performed! RateLimiter.new(nil, "forgot-password-min-#{request.remote_ip}", 3, 1.minute).performed!
user = if SiteSetting.hide_email_address_taken user = if SiteSetting.hide_email_address_taken && !current_user&.staff?
raise Discourse::InvalidParameters.new(:login) if EmailValidator.email_regex !~ normalized_login_param raise Discourse::InvalidParameters.new(:login) if EmailValidator.email_regex !~ normalized_login_param
User.real.where(staged: false).find_by_email(Email.downcase(normalized_login_param)) User.real.where(staged: false).find_by_email(Email.downcase(normalized_login_param))
else else

View File

@ -2072,6 +2072,16 @@ describe SessionController do
expect(Jobs::CriticalUserEmail.jobs.size).to eq(0) expect(Jobs::CriticalUserEmail.jobs.size).to eq(0)
end end
it 'allows for username when staff' do
sign_in(Fabricate(:admin))
post "/session/forgot_password.json",
params: { login: user.username }
expect(response.status).to eq(200)
expect(Jobs::CriticalUserEmail.jobs.size).to eq(1)
end
it 'allows for email' do it 'allows for email' do
post "/session/forgot_password.json", post "/session/forgot_password.json",
params: { login: user.email } params: { login: user.email }