-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapplication_controller.rb
145 lines (127 loc) · 4.65 KB
/
application_controller.rb
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
# frozen_string_literal: true
# Base controller class.
class ApplicationController < ActionController::Base
include Pundit
before_action :authenticate_user!, unless: :unauthenticated?
before_action :authorize_active, unless: :unauthenticated?
before_action :authorize!, unless: :unauthenticated?
before_action :set_current_college, unless: :unauthenticated?
before_action :authorize_current_college, unless: :unauthenticated?
before_action :verify_tos_accepted, unless: :unauthenticated?
before_action :set_active_draws, if: :user_signed_in?
before_action :notify_masquerading, unless: :unauthenticated?
before_action :assign_referrer
after_action :verify_authorized, unless: :unauthenticated?
rescue_from Pundit::NotAuthorizedError do |exception|
Honeybadger.notify(exception)
flash[:error] = "Sorry, you don't have permission to do that."
redirect_to request.referer.present? ? request.referer : root_path
end
rescue_from ActiveRecord::RecordNotFound do |exception|
Honeybadger.notify(exception)
flash[:error] = 'Sorry, that record could not be found.'
redirect_to request.referer.present? ? request.referer : root_path
end
private
def unauthenticated?
devise_controller? || self.class == HighVoltage::PagesController
end
# Abstract method to handle object CRUD. Handles success, failure,
# and setting the flash appropriately.
#
# @abstract
# @param [ApplicationRecord] object The object key from the service object
# results
# @param [Hash{Symbol=>String}] msg The msg key from the service object
# results
# @param [String] action The action to render when no object passed.
# (Creation / update failure, destruction success)
# @param [String] path The path to redirect to when no object passed.
def handle_action(redirect_object:, msg:, action: nil, path: nil, **_)
msg.each { |flash_type, msg_str| flash[flash_type] = msg_str }
redirect_to(redirect_object) && return if redirect_object
complete_request(action: action, path: path)
end
def complete_request(action: nil, path: nil)
if path
redirect_to path
elsif action
render action: action
flash.discard
else
redirect_to root_path
end
end
# Abstract method to handle file export actions. Handles success, failure,
# and setting the flash appropriately.
#
# @abstract
# @param file [Object] the file to be exported.
# @param filename [String] the file name.
# @param type [String] the type of the file (ex: 'text/csv').
# @param errors [String] the errors incurred during file creation, if any.
def handle_file_action(file:, filename:, type:, errors: nil)
if errors
flash[:error] = errors
redirect_to request.referer
else
send_data(file, filename: filename, type: type)
end
end
# Abstract method to enforce permissions authorization in all controllers.
# Must be overridden in all controllers.
#
# @abstract
def authorize!
raise NoMethodError
end
def set_current_college
@current_college ||= College.current
rescue ActiveRecord::RecordNotFound
flash[:error] = 'Please select a valid college to proceed.'
redirect_to colleges_path
end
def authorize_current_college
return if policy(@current_college).access?
flash[:error] = 'You do not have permission to access this college.'
if current_user.college.present?
redirect_to root_url(host: current_user.college.host)
else
redirect_to colleges_url(host: ENV.fetch('APPLICATION_HOST'))
end
end
def authorize_active
return unless current_user.role == 'graduated'
sign_out
msg = 'Your account has been marked as inactive. '\
'If you believe this is an error, please contact an Administrator.'
flash[:error] = msg
redirect_to home_path
end
def verify_tos_accepted
return if current_user.admin? || current_user.tos_accepted
flash[:error] = 'You must accept the Terms of Service to proceed.'
redirect_to terms_of_service_path
end
def set_active_draws
@active_draws ||= Draw.where(active: true)
end
def notify_masquerading
return unless masquerading?
user = User.find(session['warden.user.user.key'][0][0])
msg = "Masquerading as #{user.full_name}. INFO: To end the masquerade \
session, click the 'Stop Masquerading' button on the upper right of \
the nav bar."
flash[:success] = msg
end
def masquerading?
session[:admin_id].present?
end
helper_method :masquerading?
# Determines redirect for back/cancel button
#
# @param referrer [String] the referring resource
def assign_referrer
@referrer = params[:referrer] || 'javascript:history.back()'
end
end