-
Notifications
You must be signed in to change notification settings - Fork 434
/
webui_controller.rb
309 lines (267 loc) · 9.79 KB
/
webui_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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# Filters added to this controller will be run for all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
require_dependency 'authenticator'
class Webui::WebuiController < ActionController::Base
helper_method :valid_xml_id
Rails.cache.set_domain if Rails.cache.respond_to?('set_domain')
include Pundit
protect_from_forgery
before_action :setup_view_path
before_action :instantiate_controller_and_action_names
before_action :check_user
before_action :check_anonymous
before_action :require_configuration
after_action :clean_cache
# We execute both strategies here. The default rails strategy (resetting the session)
# and throwing an exception if the session is handled elswhere (e.g. proxy_auth_mode: :on)
def handle_unverified_request
super
raise ActionController::InvalidAuthenticityToken
end
# :notice and :alert are default, we add :success and :error
add_flash_types :success, :error
rescue_from Pundit::NotAuthorizedError do |exception|
pundit_action = case exception.try(:query).to_s
when "index?" then "list"
when "show?" then "view"
when "create?" then "create"
when "new?" then "create"
when "update?" then "update"
when "edit?" then "edit"
when "destroy?" then "delete"
when "branch?" then "branch"
else exception.try(:query)
end
if pundit_action && exception.record
message = "Sorry, you are not authorized to #{pundit_action} this #{exception.record.class}."
else
message = "Sorry, you are not authorized to perform this action."
end
if request.xhr?
render json: { error: message }, status: 400
else
flash[:error] = message
redirect_back(fallback_location: root_path)
end
end
# FIXME: This belongs into the user controller my dear.
# Also it would be better, but also more complicated, to just raise
# HTTPPaymentRequired, UnauthorizedError or Forbidden
# here so the exception handler catches it but what the heck...
rescue_from ActiveXML::Transport::ForbiddenError do |exception|
case exception.code
when "unregistered_ichain_user"
render template: "user/request_ichain"
when "unregistered_user"
render file: Rails.root.join('public/403'), formats: [:html], status: 402, layout: false
when "unconfirmed_user"
render file: Rails.root.join('public/402'), formats: [:html], status: 402, layout: false
else
if User.current.is_nobody?
render file: Rails.root.join('public/401'), formats: [:html], status: :unauthorized, layout: false
else
render file: Rails.root.join('public/403'), formats: [:html], status: :forbidden, layout: false
end
end
end
rescue_from ActionController::RedirectBackError do
redirect_to root_path
end
# FIXME: This is more than stupid. Why do we tell the user that something isn't found
# just because there is some data missing to compute the request? Someone needs to read
# http://guides.rubyonrails.org/active_record_validations.html
class MissingParameterError < RuntimeError; end
rescue_from MissingParameterError do |exception|
logger.debug "#{exception.class.name} #{exception.message} #{exception.backtrace.join('\n')}"
render file: Rails.root.join('public/404'), status: 404, layout: false, formats: [:html]
end
def set_project
@project = Project.find_by(name: params[:project])
raise ActiveRecord::RecordNotFound unless @project
end
def set_project_by_id
@project = Project.find(params[:id])
end
def valid_xml_id(rawid)
rawid = "_#{rawid}" if rawid !~ /^[A-Za-z_]/ # xs:ID elements have to start with character or '_'
CGI.escapeHTML(rawid.gsub(/[+&: .\/\~\(\)@#]/, '_'))
end
protected
def require_login
if CONFIG['kerberos_mode']
kerberos_auth
else
if User.current.nil? || User.current.is_nobody?
render(text: 'Please login') && (return false) if request.xhr?
flash[:error] = 'Please login to access the requested page.'
mode = CONFIG['proxy_auth_mode'] || :off
if mode == :off
redirect_to controller: :user, action: :login
else
redirect_to controller: :main
end
return false
end
true
end
end
def required_parameters(*parameters)
parameters.each do |parameter|
unless params.include? parameter.to_s
raise MissingParameterError.new "Required Parameter #{parameter} missing"
end
end
end
def discard_cache?
cc = request.headers['HTTP_CACHE_CONTROL']
return false if cc.blank?
return true if cc == 'max-age=0'
return false unless cc == 'no-cache'
!request.xhr?
end
def instantiate_controller_and_action_names
@current_action = action_name
@current_controller = controller_name
end
# Needed to hide/render some views to well known spider bots
# FIXME: We should get rid of it
def check_spiders
@spider_bot = request.bot?
end
private :check_spiders
def lockout_spiders
check_spiders
if @spider_bot
head :ok
return true
end
false
end
def authenticator
@authenticator ||= Authenticator.new(request, session, response)
end
def kerberos_auth
return true unless CONFIG['kerberos_mode'] && (User.current.nil? || User.current.is_nobody?)
authorization = authenticator.authorization_infos || []
if authorization[0].to_s != "Negotiate"
# Demand kerberos negotiation
response.headers["WWW-Authenticate"] = 'Negotiate'
render :login, status: 401
return
else
begin
authenticator.extract_user
rescue Authenticator::AuthenticationRequiredError => e
logger.info "Authentication via kerberos failed '#{e.message}'"
flash[:error] = "Authentication failed: '#{e.message}'"
redirect_back(fallback_location: root_path)
return
end
if User.current
logger.info "User '#{User.current}' has logged in via kerberos"
session[:login] = User.current.login
redirect_back(fallback_location: root_path)
return true
end
end
end
def check_user
check_spiders
User.current = nil # reset old users hanging around
if CONFIG['proxy_auth_mode'] == :on
logger.debug "Authenticating with proxy auth mode"
user_login = request.env['HTTP_X_USERNAME']
if user_login.blank?
User.current = User.find_nobody!
return
end
# The user does not exist in our database, create her.
unless User.where(login: user_login).exists?
logger.debug "Creating user #{user_login}"
User.create_user_with_fake_pw!(login: user_login,
email: request.env['HTTP_X_EMAIL'],
state: User.default_user_state,
realname: "#{request.env['HTTP_X_FIRSTNAME']} #{request.env['HTTP_X_LASTNAME']}".strip)
end
# The user exists, check if shes active and update the info
User.current = User.find_by(login: user_login)
unless User.current.is_active?
session[:login] = nil
User.current = User.find_nobody!
redirect_to(CONFIG['proxy_auth_logout_page'], error: "Your account is disabled. Please contact the adminsitrator for details.")
return
end
User.current.update_user_info_from_proxy_env(request.env)
end
User.current = User.find_by_login(session[:login]) if session[:login]
User.current ||= User.find_nobody!
end
def check_display_user
if params['user'].present?
begin
@displayed_user = User.find_by_login!(params['user'])
rescue NotFoundError
# admins can see deleted users
@displayed_user = User.find_by_login(params['user']) if User.current && User.current.is_admin?
redirect_back(fallback_location: root_path, error: "User not found #{params['user']}") unless @displayed_user
end
else
@displayed_user = User.current
@displayed_user ||= User.find_nobody!
end
end
def map_to_workers(arch)
case arch
when 'i586' then 'x86_64'
when 'ppc' then 'ppc64'
when 's390' then 's390x'
else arch
end
end
def peek_enabled?
User.current && (User.current.is_admin? || User.current.is_staff?)
end
private
def require_configuration
@configuration = ::Configuration.first
end
# Before filter to check if current user is administrator
def require_admin
return unless User.current.nil? || !User.current.is_admin?
flash[:error] = 'Requires admin privileges'
redirect_back(fallback_location: { controller: 'main', action: 'index' })
end
# before filter to only show the frontpage to anonymous users
def check_anonymous
if User.current && User.current.is_nobody?
unless ::Configuration.anonymous
flash[:error] = "No anonymous access. Please log in!"
redirect_back(fallback_location: root_path)
end
else
false
end
end
# After filter to clean up caches
def clean_cache
end
def setup_view_path
return unless CONFIG['theme']
theme_path = Rails.root.join('app', 'views', 'webui', 'theme', CONFIG['theme'])
prepend_view_path(theme_path)
end
def check_ajax
raise ActionController::RoutingError.new('Expected AJAX call') unless request.xhr?
end
def pundit_user
return if User.current.is_nobody?
return User.current
end
# dialog_init is a function name called before dialog is shown
def render_dialog(dialog_init = nil)
check_ajax
@dialog_html = ActionController::Base.helpers.escape_javascript(render_to_string(partial: @current_action.to_s))
@dialog_init = dialog_init
render partial: 'dialog', content_type: 'application/javascript'
end
end