A suspended user returns 403 instead of 500 (#16408)

This commit is contained in:
Jamie Gaskins 2022-02-04 09:42:23 -05:00 committed by GitHub
parent 52e4e571b7
commit a9cdb2bae2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 26 additions and 14 deletions

View file

@ -131,8 +131,11 @@ class ApplicationController < ActionController::Base
onboarding_path
end
def raise_suspended
raise SuspendedError if current_user&.suspended?
def check_suspended
return unless current_user&.suspended?
response.status = :forbidden
render "pages/forbidden"
end
def internal_navigation?

View file

@ -3,7 +3,7 @@ class ArticlesController < ApplicationController
before_action :authenticate_user!, except: %i[feed new]
before_action :set_article, only: %i[edit manage update destroy stats admin_unpublish]
before_action :raise_suspended, only: %i[new create update]
before_action :check_suspended, only: %i[new create update]
before_action :set_cache_control_headers, only: %i[feed]
after_action :verify_authorized

View file

@ -29,7 +29,7 @@ class ListingsController < ApplicationController
# rubocop:disable Rails/LexicallyScopedActionFilter
before_action :set_listing, only: %i[edit update destroy]
before_action :set_cache_control_headers, only: %i[index]
before_action :raise_suspended, only: %i[new create update]
before_action :check_suspended, only: %i[new create update]
before_action :authenticate_user!, only: %i[edit update new dashboard]
after_action :verify_authorized, only: %i[edit update]
# rubocop:enable Rails/LexicallyScopedActionFilter

View file

@ -1,6 +1,6 @@
module Users
class NotificationSettingsController < ApplicationController
before_action :raise_suspended
before_action :check_suspended
before_action :authenticate_user!
after_action :verify_authorized

View file

@ -1,6 +1,6 @@
module Users
class SettingsController < ApplicationController
before_action :raise_suspended
before_action :check_suspended
before_action :authenticate_user!
after_action :verify_authorized

View file

@ -1,6 +1,6 @@
class UsersController < ApplicationController
before_action :set_no_cache_header
before_action :raise_suspended, only: %i[update update_password]
before_action :check_suspended, only: %i[update update_password]
before_action :set_user,
only: %i[update update_password request_destroy full_delete remove_identity]
# rubocop:disable Layout/LineLength

View file

@ -1 +0,0 @@
class SuspendedError < StandardError; end

View file

@ -0,0 +1,8 @@
<h2>Forbidden</h2>
<p>
Your account has been suspended and has limited access.
Your ability to post and comment may be limited.
</p>
<p>For more information, you may send a message to <%= ForemInstance.email %>.</p>

View file

@ -4,7 +4,6 @@
# rubocop:disable Metrics/BlockLength
Rails.application.reloader.to_prepare do
message_fingerprints = {
"SuspendedError" => "banned",
"Rack::Timeout::RequestTimeoutException" => "rack_timeout",
"Rack::Timeout::RequestTimeoutError" => "rack_timeout",
"PG::QueryCanceled" => "pg_query_canceled"

View file

@ -2,7 +2,6 @@ require "rails_helper"
describe Honeybadger do
{
"SuspendedError" => "banned",
"Rack::Timeout::RequestTimeoutException" => "rack_timeout",
"Rack::Timeout::RequestTimeoutError" => "rack_timeout",
"PG::QueryCanceled" => "pg_query_canceled"

View file

@ -333,9 +333,10 @@ RSpec.describe "/listings", type: :request do
context "when user is suspended" do
it "raises error" do
user.add_role(:suspended)
expect do
post "/listings", params: listing_params
end.to raise_error(SuspendedError)
post "/listings", params: listing_params
expect(response).to have_http_status :forbidden
end
end

View file

@ -5,6 +5,9 @@ RSpec.describe "Suspended user", type: :system do
it "tries to create an article" do
sign_in suspended_user
expect { visit "/new" }.to raise_error(SuspendedError)
visit "/new"
expect(page.status_code).to eq 403
end
end