forked from External/greenlight
		
	Added recaptcha to reset password if enabled (#2475)
This commit is contained in:
		@@ -23,22 +23,22 @@ class PasswordResetsController < ApplicationController
 | 
				
			|||||||
  before_action :find_user, only: [:edit, :update]
 | 
					  before_action :find_user, only: [:edit, :update]
 | 
				
			||||||
  before_action :check_expiration, only: [:edit, :update]
 | 
					  before_action :check_expiration, only: [:edit, :update]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  # POST /password_resets/new
 | 
					  # GET /password_resets/new
 | 
				
			||||||
  def new
 | 
					  def new
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  # POST /password_resets
 | 
					  # POST /password_resets
 | 
				
			||||||
  def create
 | 
					  def create
 | 
				
			||||||
    begin
 | 
					    return redirect_to new_password_reset_path, flash: { alert: I18n.t("reset_password.captcha") } unless valid_captcha
 | 
				
			||||||
      # Check if user exists and throw an error if he doesn't
 | 
					 | 
				
			||||||
      @user = User.find_by!(email: params[:password_reset][:email].downcase, provider: @user_domain)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
      send_password_reset_email(@user, @user.create_reset_digest)
 | 
					    # Check if user exists and throw an error if he doesn't
 | 
				
			||||||
      redirect_to root_path
 | 
					    @user = User.find_by!(email: params[:password_reset][:email].downcase, provider: @user_domain)
 | 
				
			||||||
    rescue
 | 
					
 | 
				
			||||||
      # User doesn't exist
 | 
					    send_password_reset_email(@user, @user.create_reset_digest)
 | 
				
			||||||
      redirect_to root_path, flash: { success: I18n.t("email_sent", email_type: t("reset_password.subtitle")) }
 | 
					    redirect_to root_path
 | 
				
			||||||
    end
 | 
					  rescue
 | 
				
			||||||
 | 
					    # User doesn't exist
 | 
				
			||||||
 | 
					    redirect_to root_path, flash: { success: I18n.t("email_sent", email_type: t("reset_password.subtitle")) }
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  # GET /password_resets/:id/edit
 | 
					  # GET /password_resets/:id/edit
 | 
				
			||||||
@@ -84,4 +84,10 @@ class PasswordResetsController < ApplicationController
 | 
				
			|||||||
  def disable_password_reset
 | 
					  def disable_password_reset
 | 
				
			||||||
    redirect_to '/404'
 | 
					    redirect_to '/404'
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  # Checks that the captcha passed is valid
 | 
				
			||||||
 | 
					  def valid_captcha
 | 
				
			||||||
 | 
					    return true unless Rails.configuration.recaptcha_enabled
 | 
				
			||||||
 | 
					    verify_recaptcha
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -26,6 +26,12 @@
 | 
				
			|||||||
            <%= f.email_field :email, class: "form-control" %>
 | 
					            <%= f.email_field :email, class: "form-control" %>
 | 
				
			||||||
            <br>
 | 
					            <br>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            <% if recaptcha_enabled? %>
 | 
				
			||||||
 | 
					              <div class="form-group">
 | 
				
			||||||
 | 
					                <%= recaptcha_tags %>
 | 
				
			||||||
 | 
					              </div>
 | 
				
			||||||
 | 
					            <% end %>
 | 
				
			||||||
 | 
					      
 | 
				
			||||||
            <%= f.submit t("forgot_password.submit"), class: "btn btn-primary" %>
 | 
					            <%= f.submit t("forgot_password.submit"), class: "btn btn-primary" %>
 | 
				
			||||||
          <% end %>
 | 
					          <% end %>
 | 
				
			||||||
        </div>
 | 
					        </div>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -526,6 +526,7 @@ en:
 | 
				
			|||||||
  remove: Remove
 | 
					  remove: Remove
 | 
				
			||||||
  rename: Rename
 | 
					  rename: Rename
 | 
				
			||||||
  reset_password:
 | 
					  reset_password:
 | 
				
			||||||
 | 
					    captcha: reCAPTCHA verification failed, please try again.
 | 
				
			||||||
    invalid_token: Password reset token is invalid. Please try resetting your password again.
 | 
					    invalid_token: Password reset token is invalid. Please try resetting your password again.
 | 
				
			||||||
    subtitle: Reset Password
 | 
					    subtitle: Reset Password
 | 
				
			||||||
    password: New Password
 | 
					    password: New Password
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -71,6 +71,43 @@ describe PasswordResetsController, type: :controller do
 | 
				
			|||||||
        expect(response).to redirect_to("/404")
 | 
					        expect(response).to redirect_to("/404")
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    context "reCAPTCHA enabled" do
 | 
				
			||||||
 | 
					      before do
 | 
				
			||||||
 | 
					        allow(Rails.configuration).to receive(:enable_email_verification).and_return(true)
 | 
				
			||||||
 | 
					        allow(Rails.configuration).to receive(:recaptcha_enabled).and_return(true)
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      it "sends a reset email if the recaptcha was passed" do
 | 
				
			||||||
 | 
					        allow(controller).to receive(:valid_captcha).and_return(true)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        user = create(:user, provider: "greenlight")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        params = {
 | 
				
			||||||
 | 
					          password_reset: {
 | 
				
			||||||
 | 
					            email: user.email,
 | 
				
			||||||
 | 
					          },
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        expect { post :create, params: params }.to change { ActionMailer::Base.deliveries.count }.by(1)
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      it "doesn't send an email if the recaptcha was failed" do
 | 
				
			||||||
 | 
					        allow(controller).to receive(:valid_captcha).and_return(false)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        user = create(:user)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        params = {
 | 
				
			||||||
 | 
					          password_reset: {
 | 
				
			||||||
 | 
					            email: user.email,
 | 
				
			||||||
 | 
					          },
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        post :create, params: params
 | 
				
			||||||
 | 
					        expect(response).to redirect_to(new_password_reset_path)
 | 
				
			||||||
 | 
					        expect(flash[:alert]).to be_present
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  describe "PATCH #update" do
 | 
					  describe "PATCH #update" do
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user