2016-09-24 03:00:46 +00:00
|
|
|
# frozen_string_literal: true
|
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
|
2016-06-16 07:44:08 +00:00
|
|
|
protect_from_forgery(with: :exception)
|
2015-11-03 14:22:53 +00:00
|
|
|
|
2016-09-24 04:27:34 +00:00
|
|
|
before_action :invite_link
|
2016-09-29 10:46:00 +00:00
|
|
|
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
|
|
|
|
2016-03-12 02:20:15 +00:00
|
|
|
def default_serializer_options
|
|
|
|
{ root: false }
|
|
|
|
end
|
|
|
|
|
2014-01-29 03:46:58 +00:00
|
|
|
# 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?
|
2014-05-17 18:57:03 +00:00
|
|
|
helper_method :admin?
|
2015-12-22 18:16:03 +00:00
|
|
|
|
2014-10-07 21:46:09 +00:00
|
|
|
def after_sign_in_path_for(resource)
|
2016-10-17 00:22:00 +00:00
|
|
|
sign_in_url = new_user_session_url
|
|
|
|
sign_up_url = new_user_registration_url
|
|
|
|
stored = stored_location_for(User)
|
2014-10-07 22:38:33 +00:00
|
|
|
|
2016-10-17 00:22:00 +00:00
|
|
|
if stored
|
|
|
|
stored
|
|
|
|
elsif request.referer.include?(sign_in_url) || request.referer.include?(sign_up_url)
|
2014-10-07 21:46:09 +00:00
|
|
|
super
|
|
|
|
else
|
2016-10-17 00:22:00 +00:00
|
|
|
request.referer || root_path
|
2014-01-29 03:46:58 +00:00
|
|
|
end
|
|
|
|
end
|
2015-12-22 18:16:03 +00:00
|
|
|
|
2016-03-11 13:35:48 +00:00
|
|
|
def handle_unauthorized
|
2016-10-17 00:22:00 +00:00
|
|
|
if authenticated? and params[:controller] == 'maps' and params[:action] == 'show'
|
|
|
|
redirect_to request_access_map_path(params[:id])
|
|
|
|
elsif authenticated?
|
2016-10-08 04:26:08 +00:00
|
|
|
redirect_to root_path, notice: "You don't have permission to see that page."
|
2016-04-24 15:50:35 +00:00
|
|
|
else
|
2016-10-17 00:22:00 +00:00
|
|
|
store_location_for(resource, request.fullpath)
|
2016-07-26 00:14:23 +00:00
|
|
|
redirect_to new_user_session_path, notice: 'Try signing in to do that.'
|
2016-04-24 15:50:35 +00:00
|
|
|
end
|
2016-03-11 13:35:48 +00:00
|
|
|
end
|
2016-03-25 04:26:07 +00:00
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
private
|
2012-09-23 02:39:12 +00:00
|
|
|
|
2016-09-24 04:27:34 +00:00
|
|
|
def invite_link
|
2016-07-26 00:14:23 +00:00
|
|
|
@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?
|
|
|
|
redirect_to edit_user_path(user), notice: 'You must be logged out.'
|
|
|
|
return 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?
|
|
|
|
redirect_to new_user_session_path, notice: 'You must be logged in.'
|
|
|
|
return false
|
2012-09-23 02:39:12 +00:00
|
|
|
end
|
2015-12-22 18:16:03 +00:00
|
|
|
|
2014-05-17 18:57:03 +00:00
|
|
|
def require_admin
|
2016-09-24 03:00:46 +00:00
|
|
|
return true if authenticated? && admin?
|
|
|
|
redirect_to root_url, notice: 'You need to be an admin for that.'
|
|
|
|
false
|
2014-05-17 18:57:03 +00:00
|
|
|
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
|
|
|
|
2014-05-17 18:57:03 +00:00
|
|
|
def admin?
|
2015-12-16 13:32:50 +00:00
|
|
|
authenticated? && current_user.admin
|
2014-05-17 18:57:03 +00:00
|
|
|
end
|
2015-11-03 14:22:53 +00:00
|
|
|
|
2016-02-19 01:23:39 +00:00
|
|
|
def allow_embedding
|
2016-07-26 00:14:23 +00:00
|
|
|
# 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
|
2016-09-29 10:46:00 +00:00
|
|
|
|
|
|
|
def prepare_exception_notifier
|
|
|
|
request.env['exception_notifier.exception_data'] = {
|
|
|
|
current_user: current_user
|
|
|
|
}
|
|
|
|
end
|
2012-09-23 02:39:12 +00:00
|
|
|
end
|