metamaps--metamaps/app/controllers/application_controller.rb

84 lines
1.9 KiB
Ruby
Raw Normal View History

2012-09-22 22:39:12 -04:00
class ApplicationController < ActionController::Base
2016-03-24 21:26:07 -07:00
include ApplicationHelper
2016-02-13 17:28:09 +08:00
include Pundit
2016-03-12 09:37:32 +11:00
include PunditExtra
2016-03-11 21:35:48 +08:00
rescue_from Pundit::NotAuthorizedError, with: :handle_unauthorized
2012-09-22 22:39:12 -04:00
protect_from_forgery
2015-11-03 22:22:53 +08:00
2016-03-29 22:34:47 +08:00
before_action :get_invite_link
2016-02-19 09:23:39 +08:00
after_action :allow_embedding
2016-03-24 21:26:07 -07:00
def default_serializer_options
{ root: false }
end
# this is for global login
include ContentHelper
2015-12-22 13:16:03 -05:00
2012-09-22 22:39:12 -04:00
helper_method :user
helper_method :authenticated?
helper_method :admin?
2015-12-22 13:16:03 -05:00
2014-10-07 17:46:09 -04:00
def after_sign_in_path_for(resource)
2016-02-28 16:57:25 +08:00
sign_in_url = url_for(:action => 'new', :controller => 'sessions', :only_path => false, :protocol => 'https')
2014-10-07 17:46:09 -04:00
if request.referer == sign_in_url
super
elsif params[:uv_login] == "1"
"http://support.metamaps.cc/login_success?sso=" + current_sso_token
2014-10-07 17:46:09 -04:00
else
stored_location_for(resource) || request.referer || root_path
end
end
2015-12-22 13:16:03 -05:00
2016-03-11 21:35:48 +08:00
def handle_unauthorized
head :forbidden # TODO make this better
end
2016-03-24 21:26:07 -07:00
2012-09-22 22:39:12 -04:00
private
2016-03-29 22:34:47 +08:00
def get_invite_link
@invite_link = "#{request.base_url}/join" + (current_user ? "?code=#{current_user.code}" : "")
end
2012-09-22 22:39:12 -04:00
def require_no_user
if authenticated?
redirect_to edit_user_path(user), notice: "You must be logged out."
2012-09-22 22:39:12 -04:00
return false
end
end
2015-12-22 13:16:03 -05:00
2012-09-22 22:39:12 -04:00
def require_user
unless authenticated?
redirect_to new_user_session_path, notice: "You must be logged in."
2012-09-22 22:39:12 -04:00
return false
end
end
2015-12-22 13:16:03 -05:00
def require_admin
unless authenticated? && admin?
redirect_to root_url, notice: "You need to be an admin for that."
return false
end
end
2015-12-22 13:16:03 -05:00
2012-09-22 22:39:12 -04:00
def user
current_user
end
2015-12-22 13:16:03 -05:00
2012-09-22 22:39:12 -04:00
def authenticated?
current_user
end
2015-12-22 13:16:03 -05:00
def admin?
authenticated? && current_user.admin
end
2015-11-03 22:22:53 +08:00
2016-02-19 09:23:39 +08:00
def allow_embedding
#allow all
response.headers.except! 'X-Frame-Options'
# or allow a whitelist
# response.headers['X-Frame-Options'] = 'ALLOW-FROM http://blog.metamaps.cc'
2015-11-03 22:22:53 +08:00
end
2012-09-22 22:39:12 -04:00
end