docbrown/app/controllers/application_controller.rb
Edem Attikese 0ad8cd9eab Edem/improvements/pundit coverage (#498)
* added organization policy + spec

* user specs for is_org_admin?

* added authroize to organization controller

* admin policy + specs

* deleted enforce admin due to pundit policy redundancy

* applied admin policy to entire admin namespace

* refactoring analytics controller WIP - wanna test codeship

* Add protection against reactions to unpublished articles (#473)

* Add chat channel policy and spec (#474)

* Add comment policy and specs (#475)

* Fix edge case with apostrophes

* Add comment policy and specs

* Add login for deleting comment spec

* Change test to raise pundit error instead of 404

* Clean up comment destroy request specs

* Remove redundant raise

* Whitelist columns on to_json call (#477)

* Add pundit policy for several controllers (#476)

* Add pundit policy for several controllers

* Adjust video spec

* Fix tag request specs

* Add proper twilio tokens request specs

* Remove puts statements

* Add a couple basic request specs (#478)

* Add a few tests and fix user tag color bug (#482)

* Refactor handle_tag_index in stories_controller (#481)

* Modify valid_request_origin? (#483)

* Add misc specs and remove banned attribute from user model (#484)

*  Fix missing Cloudinary tags and misc specs (#486)

* removing current_user_is_admin? to use .is_admin? method

* added missing org policy routes

* Add comment for all public controllers

* Fix edge case for test

* Authorize mod controller and add specs

* Refactor methods via inheritance and use only super_admin role

* Create policy method for analytics via article_policy and refactor

* Capitalize all buttons in dashboard page

* Fix org tests and remove old admin test

* Use only happy path for analytics

* Fix tests to use Pundit error

* Update org_policy spec
2018-06-28 09:38:20 -04:00

75 lines
2.3 KiB
Ruby

class ApplicationController < ActionController::Base
protect_from_forgery with: :exception, prepend: true
include Pundit
def require_http_auth
authenticate_or_request_with_http_basic do |username, password|
username == ENV["APP_NAME"] && password == ENV["APP_PASSWORD"]
end
end
def not_found
raise ActionController::RoutingError.new("Not Found")
end
def efficient_current_user_id
if session["warden.user.user.key"].present?
session["warden.user.user.key"].flatten[0]
end
end
def authenticate_user!
unless current_user
respond_to do |format|
format.html { redirect_to "/enter" }
format.json { render json: { error: "Please sign in" }, status: 401 }
end
end
end
def customize_params
params[:signed_in] = user_signed_in?.to_s
end
def after_sign_in_path_for(resource)
location = request.env["omniauth.origin"] || stored_location_for(resource) || "/dashboard"
context_param = resource.created_at > 40.seconds.ago ? "?newly-registered-user=true" : "?returning-user=true"
location + context_param
end
def raise_banned
raise "BANNED" if current_user && current_user.banned
end
def is_internal_navigation?
params[:i] == "i"
end
helper_method :is_internal_navigation?
def valid_request_origin?
# This manually does what it was supposed to do on its own.
# We were getting this issue:
# HTTP Origin header (https://dev.to) didn't match request.base_url (http://dev.to)
# Not sure why, but once we work it out, we can delete this method.
# We are at least secure for now.
return if Rails.env.test?
if request.referer.present?
request.referer.start_with?(ENV["APP_PROTOCOL"].to_s + ENV["APP_DOMAIN"].to_s)
else
logger.info "**REQUEST ORIGIN CHECK** #{request.origin}"
raise InvalidAuthenticityToken, NULL_ORIGIN_MESSAGE if request.origin == "null"
request.origin.nil? || request.origin.gsub("https", "http") == request.base_url.gsub("https", "http")
end
end
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 touch_current_user
current_user.touch
end
end