-
-
Save neerajsingh0101/405df1c6f6b22ec9bfeb3a5fdb5d95a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
before_action :store_user_location!, if: :storable_location? | |
before_action :authenticate_user! | |
rescue_from Pundit::NotAuthorizedError, with: :authorization_error | |
after_action :verify_authorized | |
protect_from_forgery with: :exception | |
rescue_from ActiveRecord::RecordNotFound, with: :handle_api_exceptions | |
before_action :set_honeybadger_context | |
before_action :ensure_user_onboarded, if: :user_signed_in? | |
private | |
def storable_location? | |
request.path != "/sign_in" && | |
request.path != "/sign_up" && | |
!request.xhr? && | |
request.get? && | |
!user_signed_in? | |
end | |
def store_user_location! | |
store_location_for(:user, request.fullpath) | |
end | |
def authorization_error | |
respond_to do |format| | |
format.html { render "/pages/access_denied", status: 403 } | |
format.json { respond_with_error("Access Denied", 403) } | |
format.csv { respond_with_error("Access Denied", 403) } | |
end | |
end | |
def ensure_current_user_is_superadmin! | |
authenticate_user! | |
unless current_user.super_admin? | |
raise Pundit::NotAuthorizedError | |
end | |
end | |
def handle_validation_error(exception) | |
respond_with_error exception.message, 422 | |
end | |
def handle_api_exceptions(exception) | |
log_exception exception unless Rails.env.test? | |
if (exception.class.name == "Pundit::NotAuthorizedError") | |
respond_with_error("Access Denied", 403) | |
elsif (exception.class.name == "ActiveRecord::RecordNotFound") | |
respond_with_error(exception.message, 404) | |
elsif exception.class.name == "ValidationError" | |
respond_with_error exception.message, 422 | |
else | |
error_message = Rails.env.development? ? exception.message : "Something went wrong. Please try again later." | |
respond_with_error(error_message, 500) | |
end | |
end | |
def respond_with_error(message, status = 500) | |
render json: { error: message }, status: status | |
end | |
def log_exception(exception) | |
Rails.logger.info exception.class.to_s | |
Rails.logger.info exception.to_s | |
Rails.logger.info exception.backtrace.join("\n") | |
end | |
def raise_error(message, status_code) | |
raise ValidationError.new(message, status_code) | |
end | |
end | |
def set_honeybadger_context | |
hash = { uuid: request.uuid } | |
hash.merge!(user_id: current_user.id, user_email: current_user.email) if current_user | |
Honeybadger.context hash | |
end | |
def ensure_terms_of_service_is_accepted | |
unless current_user.terms_of_service_accepted? | |
respond_to do |format| | |
format.html { redirect_to(onboarding_introduction_path) && return } | |
format.json { respond_with_error("Terms of service not accepted", 403) } | |
end | |
end | |
end | |
def ensure_user_onboarded | |
unless current_user.onboarded? | |
respond_to do |format| | |
format.html { redirect_to(root_path) && return } | |
format.json { respond_with_error("Onboarded process not completed", 403) } | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment