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

77 lines
1.4 KiB
Ruby
Raw Normal View History

2016-09-24 03:00:46 +00:00
# frozen_string_literal: true
2017-11-25 19:23:47 +00:00
2012-09-23 02:39:12 +00:00
class ApplicationController < ActionController::Base
2016-03-25 04:26:07 +00:00
include ApplicationHelper
2016-02-13 09:28:09 +00:00
include Pundit
2016-03-11 22:37:32 +00:00
include PunditExtra
2016-03-11 13:35:48 +00:00
rescue_from Pundit::NotAuthorizedError, with: :handle_unauthorized
2015-11-03 14:22:53 +00:00
2016-09-24 04:27:34 +00:00
before_action :invite_link
before_action :prepare_exception_notifier
2016-02-19 01:23:39 +00:00
after_action :allow_embedding
2016-03-25 04:26:07 +00:00
def default_serializer_options
{ root: false }
end
# this is for global login
include ContentHelper
2015-12-22 18:16:03 +00:00
2012-09-23 02:39:12 +00:00
helper_method :user
helper_method :authenticated?
helper_method :admin?
2015-12-22 18:16:03 +00:00
2016-03-11 13:35:48 +00:00
def handle_unauthorized
head :forbidden
2016-03-11 13:35:48 +00:00
end
2016-03-25 04:26:07 +00:00
private
2012-09-23 02:39:12 +00:00
2016-09-24 04:27:34 +00:00
def invite_link
@invite_link = "#{request.base_url}/join" + (current_user ? "?code=#{current_user.code}" : '')
2016-03-29 14:34:47 +00:00
end
2012-09-23 02:39:12 +00:00
def require_no_user
2016-09-24 04:27:34 +00:00
return true unless authenticated?
head :forbidden
2016-12-13 03:28:10 +00:00
false
2012-09-23 02:39:12 +00:00
end
2015-12-22 18:16:03 +00:00
2012-09-23 02:39:12 +00:00
def require_user
2016-09-24 04:27:34 +00:00
return true if authenticated?
head :forbidden
2016-12-13 03:28:10 +00:00
false
2012-09-23 02:39:12 +00:00
end
2015-12-22 18:16:03 +00:00
def require_admin
2016-09-24 03:00:46 +00:00
return true if authenticated? && admin?
head :forbidden
2016-09-24 03:00:46 +00:00
false
end
2015-12-22 18:16:03 +00:00
2012-09-23 02:39:12 +00:00
def user
current_user
end
2015-12-22 18:16:03 +00:00
2012-09-23 02:39:12 +00:00
def authenticated?
current_user
end
2015-12-22 18:16:03 +00:00
def admin?
authenticated? && current_user.admin
end
2015-11-03 14:22:53 +00:00
2016-02-19 01:23:39 +00:00
def allow_embedding
# allow all
2016-02-19 01:23:39 +00:00
response.headers.except! 'X-Frame-Options'
# or allow a whitelist
# response.headers['X-Frame-Options'] = 'ALLOW-FROM http://blog.metamaps.cc'
2015-11-03 14:22:53 +00:00
end
def prepare_exception_notifier
request.env['exception_notifier.exception_data'] = {
current_user: current_user
}
end
2012-09-23 02:39:12 +00:00
end