/
webui_controller.rb
385 lines (335 loc) · 11.9 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# 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 'frontend_compat'
module Webui
class WebuiController < ActionController::Base
Rails.cache.set_domain if Rails.cache.respond_to?('set_domain');
#before_filter :check_mobile_views
before_filter :instantiate_controller_and_action_names
before_filter :set_return_to, :reset_activexml, :authenticate
before_filter :check_user
before_filter :require_configuration
after_filter :validate_xhtml
after_filter :clean_cache
if Rails.env.test?
prepend_before_filter :start_test_api
end
# :notice and :alert are default, we add :success and :error
add_flash_types :success, :error
# 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|
if exception.code == 'unregistered_ichain_user'
render template: 'user/request_ichain' and return
elsif exception.code == 'unregistered_user'
render file: Rails.root.join('public/403'), formats: [:html], status: 402, layout: false and return
elsif exception.code == '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
class ValidationError < Exception
attr_reader :xml, :errors
def message
errors
end
def initialize( _xml, _errors )
@xml = _xml
@errors = _errors
end
end
class MissingParameterError < Exception; 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
protected
def set_return_to
if params['return_to_host']
@return_to_host = params['return_to_host']
else
# we have a proxy in front of us
@return_to_host = CONFIG['external_webui_protocol'] || 'http'
@return_to_host += '://'
@return_to_host += CONFIG['external_webui_host'] || request.host
end
@return_to_path = params['return_to_path'] || request.env['ORIGINAL_FULLPATH']
logger.debug "Setting return_to: \"#{@return_to_path}\""
end
def require_login
if User.current.is_nobody?
render :text => 'Please login' and return false if request.xhr?
flash[:error] = 'Please login to access the requested page.'
mode = :off
mode = CONFIG['proxy_auth_mode'] unless CONFIG['proxy_auth_mode'].blank?
if (mode == :off)
redirect_to :controller => :user, :action => :login, :return_to_host => @return_to_host, :return_to_path => @return_to_path
else
redirect_to :controller => :main, :return_to_host => @return_to_host, :return_to_path => @return_to_path
end
return false
end
return true
end
# sets session[:login] if the user is authenticated
def authenticate
mode = :off
mode = CONFIG['proxy_auth_mode'] unless CONFIG['proxy_auth_mode'].blank?
logger.debug "Authenticating with iChain mode: #{mode}"
if mode == :on || mode == :simulate
authenticate_proxy
else
authenticate_form_auth
end
if session[:login]
logger.info "Authenticated request to \"#{@return_to_path}\" from #{session[:login]}"
else
logger.info "Anonymous request to #{@return_to_path}"
end
end
def authenticate_proxy
Rails.logger.debug "PROXY!!!"
mode = :off
mode = CONFIG['proxy_auth_host'] unless CONFIG['proxy_auth_host'].blank?
proxy_user = request.env['HTTP_X_USERNAME']
proxy_user = CONFIG['proxy_test_user'] if mode == :simulate and CONFIG['proxy_test_user']
proxy_email = request.env['HTTP_X_EMAIL']
proxy_email = ICHAIN_TEST_EMAIL if mode == :simulate and ICHAIN_TEST_EMAIL
if proxy_user
session[:login] = proxy_user
session[:email] = proxy_email
# Set the headers for direct connection to the api, TODO: is this thread safe?
ActiveXML::api.set_additional_header( 'X-Username', proxy_user )
ActiveXML::api.set_additional_header( 'X-Email', proxy_email ) if proxy_email
else
session[:login] = nil
session[:email] = nil
end
end
def authenticate_form_auth
if session[:login] and session[:password]
# pass credentials to transport plugin, TODO: is this thread safe?
ActiveXML::api.login session[:login], session[:password]
end
end
def frontend
FrontendCompat.new
end
def valid_project_name? name
name =~ /^[[:alnum:]][-+\w.:]+$/
end
def valid_package_name_read? name
return true if name == '_project'
return true if name == '_product'
return true if name == '_deltas'
return true if name =~ /^_product:[-+\w\.:]*$/
return true if name =~ /^_patchinfo:[-+\w\.:]*$/
name =~ /^[[:alnum:]][-+\w\.:]*$/
end
def valid_package_name_write? name
return true if name =~ /^_project$/
return true if name =~ /^_product$/
name =~ /^[[:alnum:]][-+\w\.]*$/
end
def valid_file_name? name
name =~ /^[-\w+~ ][-\w\.+~ ]*$/
end
def valid_role_name? name
name =~ /^[\w\-\.+]+$/
end
def valid_target_name? name
name =~ /^\w[-\.\w&]*$/
end
def valid_user_name? name
name =~ /^[\w\-\.+]+$/
end
def valid_group_name? name
name =~ /^[\w\-\.+]+$/
end
def reset_activexml
transport = ActiveXML::api
transport.delete_additional_header 'X-Username'
transport.delete_additional_header 'X-Email'
transport.delete_additional_header 'Authorization'
end
def required_parameters(*parameters)
parameters.each do |parameter|
unless params.include? parameter.to_s
raise MissingParameterError.new "Required Parameter #{parameter} missing in #{request.url}"
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'
return !request.xhr?
end
def find_hashed(classname, *args)
ret = classname.find( *args )
return Xmlhash::XMLHash.new({}) unless ret
ret.to_hash
end
def instantiate_controller_and_action_names
@current_action = action_name
@current_controller = controller_name
end
def check_spiders
@spider_bot = false
if defined? TREAT_USER_LIKE_BOT or request.env.has_key? 'HTTP_OBS_SPIDER'
@spider_bot = true
end
end
private :check_spiders
def lockout_spiders
check_spiders
if @spider_bot
render :nothing => true
return true
end
return false
end
def check_user
check_spiders
if session[:login]
User.current = User.find_by_login session[:login]
@nr_requests_that_need_work = 0
unless request.xhr?
User.current.request_ids_by_class.each { |key,array| @nr_requests_that_need_work += array.size }
end
else
# TODO: rebase on application_controller and use load_nobdy
User.current = User.find_by_login('_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
private
def put_body_to_tempfile(xmlbody)
file = Tempfile.new('xml').path
file = File.open(file + '.xml', 'w')
file.write(xmlbody)
file.close
return file.path
end
private :put_body_to_tempfile
def validate_xhtml
return if request.xhr?
return unless (response.status.to_i == 200 && response.content_type =~ /text\/html/i)
return if Rails.env.production? or Rails.env.stage?
errors = []
xmlbody = String.new response.body
xmlbody.gsub!(/[\n\r]/, "\n")
xmlbody.gsub!(/&[^;]*sp;/, '')
# now to something fancy - patch HTML5 to look like xhtml 1.1
xmlbody.gsub!(%r{ data-\S+=\"[^\"]*\"}, ' ')
xmlbody.gsub!(%r{ autocomplete=\"[^\"]*\"}, ' ')
xmlbody.gsub!(%r{ placeholder=\"[^\"]*\"}, ' ')
xmlbody.gsub!(%r{ required=\"[^\"]*\"}, ' ')
xmlbody.gsub!(%r{ <tester .*}, ' ')
xmlbody.gsub!('</tester>', ' ')
xmlbody.gsub!(%r{ type=\"range\"}, ' type="text"')
xmlbody.gsub!(%r{ min=\"[^\"]*\"}, ' ')
xmlbody.gsub!(%r{ max=\"[^\"]*\"}, ' ')
xmlbody.gsub!(%r{(<script src="[^\"]*\")>}, '\1 type="application/javascript">')
xmlbody.gsub!('<script>', '<script type="application/javascript">')
xmlbody.gsub!(%r{<mark>(.*)</mark>}, '<b>\1</b>')
xmlbody.gsub!('<!DOCTYPE html>', '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">')
xmlbody.gsub!('<html>', '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">')
begin
document = Nokogiri::XML::Document.parse(xmlbody, nil, nil, Nokogiri::XML::ParseOptions::STRICT)
rescue Nokogiri::XML::SyntaxError => e
errors << ('[%s:%s]' % [e.line, e.column]) + e.inspect
errors << put_body_to_tempfile(xmlbody)
end
if document
ses = XHTML_XSD.validate(document)
unless ses.empty?
document = nil
errors << put_body_to_tempfile(xmlbody)
ses.each do |err|
errors << ('[%s:%s]' % [err.line, err.column]) + err.inspect
end
end
end
unless document
self.instance_variable_set(:@_response_body, nil)
logger.debug "XML Errors #{errors.inspect} #{xmlbody}"
render :template => 'webui/xml_errors', :locals => { :oldbody => xmlbody, :errors => errors }, :status => 400
end
end
def start_test_api
Webui::IntegrationTest.start_test_api
end
def require_configuration
@configuration = ::Configuration.first
end
# Before filter to check if current user is administrator
def require_admin
unless User.current.is_admin?
flash[:error] = 'Requires admin privileges'
redirect_back_or_to :controller => 'main', :action => 'index' and return
end
end
# After filter to clean up caches
def clean_cache
end
def require_available_architectures
@available_architectures = Webui::Architecture.find(:available)
unless @available_architectures
flash[:error] = 'Available architectures not found'
redirect_to :controller => 'project', :action => 'list_public', :nextstatus => 404 and return
end
end
def mobile_request?
if params.has_key? :force_view
# check if it's a reset
if session[:force_view].to_s != 'mobile' && params[:force_view].to_s == 'mobile'
session.delete :force_view
else
session[:force_view] = params[:force_view]
end
end
if session.has_key? :force_view
if session[:force_view].to_s == 'mobile'
request.env['mobile_device_type'] = :mobile
else
request.env['mobile_device_type'] = :forced_desktop
end
end
unless request.env.has_key? 'mobile_device_type'
if request.user_agent.nil? || request.env['HTTP_ACCEPT'].nil?
request.env['mobile_device_type'] = :desktop
else
mobileesp = MobileESPConverted::UserAgentInfo.new(request.user_agent, request.env['HTTP_ACCEPT'])
if mobileesp.is_tier_generic_mobile || mobileesp.is_tier_iphone || mobileesp.is_tier_rich_css || mobileesp.is_tier_tablet
request.env['mobile_device_type'] = :mobile
else
request.env['mobile_device_type'] = :desktop
end
end
end
return request.env['mobile_device_type'] == :mobile
end
def check_mobile_views
#prepend_view_path(Rails.root.join('app', 'mobile_views')) if mobile_request?
end
def check_ajax
raise ActionController::RoutingError.new('Expected AJAX call') unless request.xhr?
end
end
end