Guest User

Untitled

a guest
Apr 27th, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. # Filters added to this controller apply to all controllers in the application.
  2. # Likewise, all the methods added will be available for all controllers.
  3.  
  4. class ApplicationController < ActionController::Base
  5. include Ssl
  6. include Service::Helper
  7. helper :all
  8. protect_from_forgery
  9. filter_parameter_logging :password
  10. layout :admin_or_home
  11. before_filter :basic_auth
  12. before_filter :basic_auth_if_https
  13. before_filter :set_redhat_user,:require_ssl_only_if_is_logged_in#,:registered_vendor
  14. before_filter :require_admin_or_vendor
  15. before_filter :set_origin
  16. before_filter :redirect_to_public_page_if_http_protocol
  17.  
  18. def admin_or_home
  19. request.path =~ /admin/ ? 'admin': 'public'
  20. end
  21.  
  22. protected
  23. def ecosystems
  24. @ecosystems||=Ecosystem.all(:select=>'id,name,path')#,:order=>"level>0,level=0,level<0,updated_At asc")
  25. end
  26.  
  27. def ecosystem
  28. @ecosystem ||= ecosystems.detect{|e| e.path==params[:ecosystem]} if params[:ecosystem]
  29. end
  30.  
  31. def ecosystem_tags
  32. ecosystem ? ecosystem.versions.all(:joins=>[:tags],:select=>'versions.id,tags.id,tags.name') : []
  33. end
  34.  
  35. def promotion
  36. @promotion||=ecosystem.promotions.first
  37. end
  38.  
  39. def require_ssl_only_if_is_logged_in
  40. ssl_required if service.logged_in?(cookies)
  41. end
  42.  
  43. def set_redhat_user
  44. set_rh_user if logged_in?
  45. end
  46.  
  47. def require_admin_or_vendor
  48. if vendor?
  49. redirect_to root_url,:notice=>'You are\'t registered as Market Place ISV. Please request to Red Hat.' unless current_vendor
  50. elsif !admin?
  51. redirect_to root_path,:status=>:forbidden
  52. end
  53. end
  54.  
  55. def current_vendor
  56. @current_vendor ||= Vendor.find_by_red_hat_id(authenticate_and_roles['username'],:select=>'id,red_hat_id,name')
  57. end
  58.  
  59. def check_if_version_is_submitted(version)
  60. unless version.new?
  61. unless admin?
  62. raise Version::UnAuthorized
  63. end
  64. end
  65. end
  66.  
  67. def set_origin
  68. cookies[:origin] = params[:origin] if params[:origin]
  69. end
  70.  
  71. rescue_from Version::UnAuthorized do |exception|
  72. redirect_to admin_root_url,:notice=>"This Version is submitted"
  73. end
  74.  
  75. rescue_from Vendor::NoVendorRegistered do |exception|
  76. redirect_to root_url,:notice=>'You are\'t registered as Market Place ISV. Please request to Red Hat.'
  77. end
  78.  
  79. def redirect_to_public_page_if_http_protocol
  80. redirect_to eco_url(:rhev) unless request.ssl?
  81. end
  82.  
  83. def basic_auth
  84. authenticate_or_request_with_http_basic do |username, password|
  85. @success = username == "redhat" && password == "Luavvekma"
  86. end
  87. end
  88. def basic_auth_if_https
  89. basic_auth if request.ssl? && !@success
  90. end
  91.  
  92.  
  93. helper_method :ecosystems,:ecosystem,:promotion,:ecosystem_tags,:logged_in?,
  94. :ecosystem_sidebar,:admin?,:vendor?,:current_vendor,:admin_or_vendor?,:require_admin_or_vendor
  95.  
  96. end
Add Comment
Please, Sign In to add comment