PageRenderTime 32ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/application_controller.rb

https://gitlab.com/comster/gitlab-ci
Ruby | 99 lines | 79 code | 19 blank | 1 comment | 7 complexity | 05e011a5305c4f035be3c94088037fe8 MD5 | raw file
  1. class ApplicationController < ActionController::Base
  2. rescue_from Network::UnauthorizedError, with: :invalid_token
  3. before_filter :default_headers
  4. before_filter :check_config
  5. protect_from_forgery
  6. helper_method :current_user
  7. before_filter :reset_cache
  8. private
  9. def current_user
  10. @current_user ||= session[:current_user]
  11. end
  12. def sign_in(user)
  13. session[:current_user] = user
  14. end
  15. def sign_out
  16. reset_session
  17. end
  18. def authenticate_user!
  19. unless current_user
  20. redirect_to new_user_sessions_path
  21. return
  22. end
  23. end
  24. def authenticate_admin!
  25. unless current_user && current_user.is_admin
  26. redirect_to new_user_sessions_path
  27. return
  28. end
  29. end
  30. def authenticate_token!
  31. unless project.valid_token?(params[:token])
  32. return head(403)
  33. end
  34. end
  35. def authorize_access_project!
  36. unless current_user.can_access_project?(@project.gitlab_id)
  37. return page_404
  38. end
  39. end
  40. def authorize_project_developer!
  41. unless current_user.has_developer_access?(@project.gitlab_id)
  42. return page_404
  43. end
  44. end
  45. def authorize_manage_project!
  46. unless current_user.can_manage_project?(@project.gitlab_id)
  47. return page_404
  48. end
  49. end
  50. def page_404
  51. render file: "#{Rails.root}/public/404.html", status: 404, layout: false
  52. end
  53. # Reset user cache every day for security purposes
  54. def reset_cache
  55. if current_user && current_user.sync_at < (Time.zone.now - 24.hours)
  56. current_user.reset_cache
  57. end
  58. end
  59. def default_headers
  60. headers['X-Frame-Options'] = 'DENY'
  61. headers['X-XSS-Protection'] = '1; mode=block'
  62. end
  63. def check_config
  64. redirect_to oauth2_help_path unless valid_config?
  65. end
  66. def valid_config?
  67. server = GitlabCi.config.gitlab_server
  68. if server.blank? || server.url.blank? || server.app_id.blank? || server.app_secret.blank?
  69. false
  70. else
  71. true
  72. end
  73. rescue Settingslogic::MissingSetting, NoMethodError
  74. false
  75. end
  76. def invalid_token
  77. reset_session
  78. redirect_to :root
  79. end
  80. end