* Adds landing_page checkbox for Landing Page * Adds landing_page to page_params in Admin::Pages::Controller * Adds landing_page to admin_manages_pages and admin_creates_pages specs * Adjusts copy for Landing Page in pages/_form.html.erb * Places #post_a_job before #privacy in Pages::Controller - Ensures that all page-related methods are properly alphabetized * Alphabetizes remaining out-of-order page-related methods - Ensures that all page-related methods within the Pages::Controller are in alphabetical order, following the order of the other methods * Adds landing_page check to ApplicationController#verify_private_forem * Adds a single_landing_page validation to the Page model * Adds a lock.svg to /images and uses it in the pages index * Adjusts buttons and checkboxes to use crayons class in pages partial * Ensures that pages/_form.html.erb destroy button uses crayons * Adds a landing_page validation-related test to page_spec.rb * Updates #single_landing_page validation and error message * WIP: Adds JS modal upon landing_page error in pages/_form.html.erb * Adds a working modal upon error in pages/_form.html.erb - Removes redundant #single_landing_page validation from Page - Adds #has_a_landing_page? in place of #single_landing_page - Conditionally renders the landing_page checkbox in _form.html.erb - Adds styles to locked screen/landing page modal - Adds copy to locked screen/landing page modal - Beings adding buttons to overwrite current landing_page * Updates the data-modal-selectors and ids in pages/_form.html.erb * Uses #landing_page_path within pages/_form.html.erb modal - Adds a link_to to link to the current landing page in the modal - Adds styles to buttons and link_to in modal * Adds back single_landing_page validation to Page.rb * Reverts changes and updates methods within Page.rb - Reverts changes to #single_landing_page - Refactors #has_a_landing_page? and #landing_page_path - Updates div id in pages/_form.html.erb * Adjusts #single_landing_page error message slightly * Adds a test for landing_pages in stories_index_spec.rb * Adds an overwrite attribute to Pages for the modal in _form - Fixes anonymous spelling * Refactors pages/_form.html.erb and cleans up spelling * Adds a safe operator to landing_page.path in Page - Adjusts the placement of the hidden value in pages/_form.html.erb * Updates styling for Current Locked Screen link * Adds the ability to overwrite an active landing page on click - Adds Admin::PagesController#update_and_overwrite and refactors AdminPages::Controller#udpate to work with new method - Adds a remote attribute, overwrite, to Admin::PagesController via a hidden_field in pages/_form.html.erb and an attr_accessor in the Page model - Adds a toggleOverwrite() function to pages/_form.html.erb to toggle the (soon to be hidden) overwrite checkbox in the form -- the checkbox is toggled on button click * Adds a guard clause to #landing_page_path in Page.rb * Adds a safe naviagtor to Admin::PagesController#update_and_overwrite_landing_page * Adds a type and return to Overwrite button to prevent submit * Adds a hidden class to the overwrite checkbox field in _form.html.erb - Hides the overwrite checkbox since it is only used to overwrite the landing_page boolean on click of the "Overwrite current locked screen" button * Adds Admin::PagesController#create_and_overwrite and removes hidden class * Adds landing page-related tests to admin_manages_pages_spec.rb * Remove messy commented out code and place it in GH comment instead * PR Review Feedback: Renames remote attr, defines class methods, updates views - Renames remote attribute to overwrite_landing_page - Updates all necessary views, methods, etc. w/ new attr - Defines two class methods to make code more extensible - Updates all necessary views, methods, etc. w/ new methods * Remove unnecessary comments and revert changes to ternary in _form.html.erb * Adjustments for accessibility within _form and index - Adds a descriptive id to <p> in _form.html.erb - Adds an ariadescribedby field to _form.html.erb - Adjusts title within index.html.erb * Refactors landing_page-related code per PR review requests - Adds a landing_page_modal_controller.js that handles and further customizes the dismiss and overwrite button, and handles opening the modal - Adds a _landing_page_modal partial to extract modal-specific html outside of the _form.html.erb partial - Refactors _form.html.erb to account for the _landing_page_modal partial and the landing_page_modal_controller.js - Refactors the overwrite checkbox to be a hidden_field rather than a hidden checkbox and removes existing hidden_field to accomodate it - Loads the @landing_page within Admin::PagesController#edit for use by the _landing_page_modal.html.erb partial - Refactors Page#single_landing_page to be more readable - Removes useless JS from _form.html.erb * Removes #landing_page_exists? class method since it isnt used now * Uses the #landing_page class method within ApplicationController * Removes pending status from test in admin_manages_pages_spec.rb * Adds an e2e test for landing pages and adds to seeds_e2e - Adds a landingPage.spec.js to test landing pages - Adds a Page to seeds_e2e.rb for landingPage.spec.js * Removes references to articles from e2e test and adds clickable link * Adds another Page to seeds_e2e.rb and updates link names in test * Adds the Current Locked Screen title to modal and opens in new link * Renames Dismiss to Cancel and updates all necessary code * Adjusts landingPage.spec.js * check for existing landing page before showing modal * Update Cypress tests and fix bug in LandingPageModalController * Apply suggestions from code review Co-authored-by: Suzanne Aitchison <suzanne@forem.com> * Orders Pages by created_at desc in /admin/pages/index.html.erb for consistency * Targets specific edit button in landingPage.spec.js Co-authored-by: Suzanne Aitchison <suzanne@forem.com> Co-authored-by: rhymes <github@rhymes.dev>
219 lines
6.2 KiB
Ruby
219 lines
6.2 KiB
Ruby
class ApplicationController < ActionController::Base
|
|
before_action :configure_permitted_parameters, if: :devise_controller?
|
|
skip_before_action :track_ahoy_visit
|
|
before_action :verify_private_forem
|
|
protect_from_forgery with: :exception, prepend: true
|
|
before_action :remember_cookie_sync
|
|
before_action :forward_to_app_config_domain
|
|
|
|
include SessionCurrentUser
|
|
include ValidRequest
|
|
include Pundit
|
|
include CachingHeaders
|
|
include ImageUploads
|
|
include VerifySetupCompleted
|
|
include DevelopmentDependencyChecks if Rails.env.development?
|
|
include EdgeCacheSafetyCheck unless Rails.env.production?
|
|
include Devise::Controllers::Rememberable
|
|
|
|
rescue_from ActionView::MissingTemplate, with: :routing_error
|
|
|
|
rescue_from RateLimitChecker::LimitReached do |exc|
|
|
error_too_many_requests(exc)
|
|
end
|
|
|
|
rescue_from ActionController::InvalidAuthenticityToken do
|
|
ForemStatsClient.increment(
|
|
"users.invalid_authenticity_token",
|
|
tags: ["controller_name:#{controller_name}", "path:#{request.fullpath}"],
|
|
)
|
|
end
|
|
|
|
PUBLIC_CONTROLLERS = %w[async_info
|
|
confirmations
|
|
deep_links
|
|
ga_events
|
|
health_checks
|
|
invitations
|
|
omniauth_callbacks
|
|
passwords
|
|
registrations
|
|
service_worker
|
|
shell].freeze
|
|
private_constant :PUBLIC_CONTROLLERS
|
|
|
|
CONTENT_CHANGE_PATHS = [
|
|
"/tags/onboarding", # Needs to change when suggested_tags is edited.
|
|
"/onboarding", # Page is cached at edge.
|
|
"/", # Page is cached at edge.
|
|
].freeze
|
|
private_constant :CONTENT_CHANGE_PATHS
|
|
|
|
def verify_private_forem
|
|
return if controller_name.in?(PUBLIC_CONTROLLERS)
|
|
return if self.class.module_parent.to_s == "Admin"
|
|
return if user_signed_in? || Settings::UserExperience.public
|
|
|
|
@page = Page.landing_page
|
|
|
|
if api_action?
|
|
authenticate!
|
|
elsif @page
|
|
render template: "pages/show"
|
|
else
|
|
@user ||= User.new
|
|
render template: "devise/registrations/new"
|
|
end
|
|
end
|
|
|
|
def not_found
|
|
raise ActiveRecord::RecordNotFound, "Not Found"
|
|
end
|
|
|
|
def routing_error
|
|
raise ActionController::RoutingError, "Routing Error"
|
|
end
|
|
|
|
def not_authorized
|
|
render json: "Error: not authorized", status: :unauthorized
|
|
raise NotAuthorizedError, "Unauthorized"
|
|
end
|
|
|
|
def bad_request
|
|
render json: "Error: Bad Request", status: :bad_request
|
|
end
|
|
|
|
def error_too_many_requests(exc)
|
|
response.headers["Retry-After"] = exc.retry_after
|
|
render json: { error: exc.message, status: 429 }, status: :too_many_requests
|
|
end
|
|
|
|
def authenticate_user!
|
|
if current_user
|
|
Honeycomb.add_field("current_user_id", current_user.id)
|
|
return
|
|
end
|
|
|
|
respond_to do |format|
|
|
format.html { redirect_to sign_up_path }
|
|
format.json { render json: { error: "Please sign in" }, status: :unauthorized }
|
|
end
|
|
end
|
|
|
|
def redirect_permanently_to(location)
|
|
redirect_to location + internal_nav_param, status: :moved_permanently
|
|
end
|
|
|
|
def customize_params
|
|
params[:signed_in] = user_signed_in?.to_s
|
|
end
|
|
|
|
# This method is used by Devise to decide which is the path to redirect
|
|
# the user to after a successful log in
|
|
def after_sign_in_path_for(resource)
|
|
if current_user.saw_onboarding
|
|
path = stored_location_for(resource) || request.env["omniauth.origin"] || root_path(signin: "true")
|
|
signin_param = { "signin" => "true" } # the "signin" param is used by the service worker
|
|
|
|
uri = Addressable::URI.parse(path)
|
|
uri.query_values = if uri.query_values
|
|
uri.query_values.merge(signin_param)
|
|
else
|
|
signin_param
|
|
end
|
|
|
|
uri.to_s
|
|
else
|
|
referrer = request.env["omniauth.origin"] || "none"
|
|
onboarding_path(referrer: referrer)
|
|
end
|
|
end
|
|
|
|
def after_accept_path_for(_resource)
|
|
onboarding_path
|
|
end
|
|
|
|
def raise_suspended
|
|
raise SuspendedError if current_user&.suspended?
|
|
end
|
|
|
|
def internal_navigation?
|
|
params[:i] == "i"
|
|
end
|
|
helper_method :internal_navigation?
|
|
|
|
def feed_style_preference
|
|
# TODO: Future functionality will let current_user override this value with UX preferences
|
|
# if current_user exists and has a different preference.
|
|
Settings::UserExperience.feed_style
|
|
end
|
|
helper_method :feed_style_preference
|
|
|
|
def set_no_cache_header
|
|
response.headers["Cache-Control"] = "no-cache, no-store"
|
|
response.headers["Pragma"] = "no-cache"
|
|
response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
|
|
end
|
|
|
|
def rate_limit!(action)
|
|
rate_limiter.check_limit!(action)
|
|
end
|
|
|
|
def rate_limiter
|
|
(current_user || anonymous_user).rate_limiter
|
|
end
|
|
|
|
def anonymous_user
|
|
User.new(ip_address: request.env["HTTP_FASTLY_CLIENT_IP"])
|
|
end
|
|
|
|
def api_action?
|
|
self.class.to_s.start_with?("Api::")
|
|
end
|
|
|
|
def initialize_stripe
|
|
Stripe.api_key = Settings::General.stripe_api_key
|
|
|
|
return unless Rails.env.development? && Stripe.api_key.present?
|
|
|
|
Stripe.log_level = Stripe::LEVEL_INFO
|
|
end
|
|
|
|
def remember_cookie_sync
|
|
# Set remember cookie token in case not properly set.
|
|
if user_signed_in? &&
|
|
cookies[:remember_user_token].blank?
|
|
current_user.remember_me = true
|
|
current_user.remember_me!
|
|
remember_me(current_user)
|
|
end
|
|
end
|
|
|
|
def forward_to_app_config_domain
|
|
# Let's only redirect get requests for this purpose.
|
|
return unless request.get? &&
|
|
# If the request equals the original set domain, e.g. forem-x.forem.cloud.
|
|
request.host == ENV["APP_DOMAIN"] &&
|
|
# If the app domain config has now been set, let's go there instead.
|
|
ENV["APP_DOMAIN"] != Settings::General.app_domain
|
|
|
|
redirect_to URL.url(request.fullpath)
|
|
end
|
|
|
|
def bust_content_change_caches
|
|
EdgeCache::Bust.call(CONTENT_CHANGE_PATHS)
|
|
Settings::General.admin_action_taken_at = Time.current # Used as cache key
|
|
end
|
|
|
|
protected
|
|
|
|
def configure_permitted_parameters
|
|
devise_parameter_sanitizer.permit(:sign_up, keys: %i[username name profile_image profile_image_url])
|
|
end
|
|
|
|
def internal_nav_param
|
|
return "" unless params[:i] == "i"
|
|
|
|
"?i=i"
|
|
end
|
|
end
|