summaryrefslogtreecommitdiff
path: root/app/controllers/application_controller.rb
blob: c412a456b1113a42d23c74ecc6a0249a50f5179f (plain)
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
class ApplicationController < ActionController::Base
  rescue_from Network::UnauthorizedError, with: :invalid_token
  before_filter :default_headers
  before_filter :check_config

  protect_from_forgery

  helper_method :current_user
  before_filter :reset_cache

  private

  def current_user
    @current_user ||= session[:current_user]
  end

  def sign_in(user)
    session[:current_user] = user
  end

  def sign_out
    reset_session
  end

  def authenticate_user!
    unless current_user
      redirect_to new_user_sessions_path
      return
    end
  end

  def authenticate_admin!
    unless current_user && current_user.is_admin
      redirect_to new_user_sessions_path
      return
    end
  end

  def authenticate_token!
    unless project.valid_token?(params[:token])
      return head(403)
    end
  end

  def authorize_access_project!
    unless current_user.can_access_project?(@project.gitlab_id)
      return page_404
    end
  end

  def authorize_project_developer!
    unless current_user.has_developer_access?(@project.gitlab_id)
      return page_404
    end
  end

  def authorize_manage_project!
    unless current_user.can_manage_project?(@project.gitlab_id)
      return page_404
    end
  end

  def page_404
    render file: "#{Rails.root}/public/404.html", status: 404, layout: false
  end

  # Reset user cache every day for security purposes
  def reset_cache
    if current_user && current_user.sync_at < (Time.zone.now - 24.hours)
      current_user.reset_cache
    end
  end

  def default_headers
    headers['X-Frame-Options'] = 'DENY'
    headers['X-XSS-Protection'] = '1; mode=block'
  end

  def check_config
    redirect_to oauth2_help_path unless valid_config?
  end

  def valid_config?
    server = GitlabCi.config.gitlab_server

    if server.blank? || server.url.blank? || server.app_id.blank? || server.app_secret.blank?
      false
    else
      true
    end
  rescue Settingslogic::MissingSetting, NoMethodError
    false
  end

  def invalid_token
    reset_session
    redirect_to :root
  end
end