-
-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathapplication_controller.rb
More file actions
179 lines (141 loc) Β· 4.06 KB
/
application_controller.rb
File metadata and controls
179 lines (141 loc) Β· 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include Pundit::Authorization
include Pagy::Method
if Rails.env.production?
rescue_from Exception do |ex|
Rollbar.error(ex)
Rails.logger.fatal(ex)
respond_to do |format|
format.html { render 'errors/error', layout: false, status: :internal_server_error }
format.all { head :internal_server_error }
end
end
end
rescue_from ActionController::RoutingError, with: :render_not_found
rescue_from ActiveRecord::RecordNotFound, with: :render_not_found
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
rescue_from Pundit::AuthorizationNotPerformedError, with: :user_not_authorized
helper_method :logged_in?
helper_method :current_user
helper_method :current_service
before_action :set_locale
before_action :accept_terms, if: :logged_in?
def render_not_found
respond_to do |format|
format.html { render template: 'errors/not_found', layout: false, status: :not_found }
format.all { head :not_found }
end
end
protected
def current_user
if session.key?(:member_id)
@current_member ||= Member.find(session[:member_id])
end
rescue ActiveRecord::RecordNotFound
session[:member_id] = nil
end
def current_service
if session.key?(:service_id)
@current_service ||= Service.find_by(member_id: session[:member_id],
id: session[:service_id])
end
rescue ActiveRecord::RecordNotFound
session[:service_id] = nil
end
def current_user?
!!current_user
end
def logged_in?
current_user?
end
def accept_terms
store_path
redirect_to terms_and_conditions_path if current_user.accepted_toc_at.blank?
end
def authenticate_member!
if current_user
finish_registration
else
redirect_to redirect_path
end
end
def store_path
session[:previous_request_url] = request.url
end
def previous_path
session[:previous_request_url]
end
def finish_registration
if current_user.requires_additional_details? && !providing_additional_details?
redirect_to edit_member_details_path
end
end
def providing_additional_details?
[edit_member_path, edit_member_details_path].include? request.path
end
def logout!
@current_member = nil
reset_session
end
helper_method :redirect_path
def redirect_path
'/auth/github'
end
def authenticate_admin!
redirect_to root_path, notice: "You can't be here" unless logged_in? && current_user.is_admin?
end
def authenticate_admin_or_organiser!
redirect_to root_path, notice: "You can't be here" unless manager?
end
def manager?
logged_in? && current_user.manager?
end
helper_method :manager?
helper_method :admin_namespace?
def is_logged_in?
unless logged_in?
flash[:notice] = t('notifications.not_logged_in')
redirect_to root_path
end
end
def has_access?
is_logged_in?
end
def admin_namespace?
controller_path.start_with?('admin/', 'super_admin/')
end
private
def set_locale
store_locale_to_cookie(params[:locale]) if locale
I18n.locale = locale_value
end
def locale_value
return I18n.default_locale unless cookies[:locale].present?
return I18n.default_locale unless I18n.available_locales.include?(cookies[:locale].to_sym)
cookies[:locale]
end
def user_not_authorized
redirect_to(user_path, notice: 'You are not authorized to perform this action.')
end
def user_path
request.referer || root_path
end
def chapters
@chapters ||= Chapter.all
end
def redirect_back(fallback_location:, **args)
if referer = request.headers['Referer']
redirect_to referer, **args
else
redirect_to fallback_location, **args
end
end
def store_locale_to_cookie(locale)
cookies[:locale] = { value: locale,
expires: Time.zone.now + 36_000 }
end
def locale
params[:locale] if params[:locale] && I18n.available_locales.include?(params[:locale].to_sym)
end
end