helpers.rb 3.6 KB
Newer Older
1
module API
N
Nihad Abbasov 已提交
2
  module APIHelpers
3 4 5 6 7
    PRIVATE_TOKEN_HEADER = "HTTP_PRIVATE_TOKEN"
    PRIVATE_TOKEN_PARAM = :private_token
    SUDO_HEADER ="HTTP_SUDO"
    SUDO_PARAM = :sudo

N
Nihad Abbasov 已提交
8
    def current_user
9 10
      private_token = (params[PRIVATE_TOKEN_PARAM] || env[PRIVATE_TOKEN_HEADER]).to_s
      @current_user ||= User.find_by_authentication_token(private_token)
11
      identifier = sudo_identifier()
12

13 14 15
      # If the sudo is the current user do nothing
      if (identifier && !(@current_user.id == identifier || @current_user.username == identifier))
        render_api_error!('403 Forbidden: Must be admin to use sudo', 403) unless @current_user.is_admin?
N
Nihad Abbasov 已提交
16
        @current_user = User.by_username_or_id(identifier)
N
Nihad Abbasov 已提交
17
        not_found!("No user id or username for: #{identifier}") if @current_user.nil?
18
      end
19

20 21 22 23
      @current_user
    end

    def sudo_identifier()
24
      identifier ||= params[SUDO_PARAM] ||= env[SUDO_HEADER]
25

26
      # Regex for integers
27 28 29 30 31
      if (!!(identifier =~ /^[0-9]+$/))
        identifier.to_i
      else
        identifier
      end
N
Nihad Abbasov 已提交
32 33
    end

34 35
    def set_current_user_for_thread
      Thread.current[:current_user] = current_user
36

37 38 39 40 41 42 43
      begin
        yield
      ensure
        Thread.current[:current_user] = nil
      end
    end

N
Nihad Abbasov 已提交
44
    def user_project
45
      @project ||= find_project(params[:id])
46 47 48
      @project || not_found!
    end

49 50
    def find_project(id)
      project = Project.find_by_id(id) || Project.find_with_namespace(id)
51 52 53

      if project && can?(current_user, :read_project, project)
        project
N
Nihad Abbasov 已提交
54
      else
55
        nil
N
Nihad Abbasov 已提交
56
      end
N
Nihad Abbasov 已提交
57 58
    end

N
Nihad Abbasov 已提交
59 60 61 62
    def paginate(object)
      object.page(params[:page]).per(params[:per_page].to_i)
    end

N
Nihad Abbasov 已提交
63
    def authenticate!
64
      unauthorized! unless current_user
N
Nihad Abbasov 已提交
65
    end
R
randx 已提交
66

67 68 69 70
    def authenticated_as_admin!
      forbidden! unless current_user.is_admin?
    end

R
randx 已提交
71 72
    def authorize! action, subject
      unless abilities.allowed?(current_user, action, subject)
73
        forbidden!
R
randx 已提交
74 75 76
      end
    end

77 78 79 80
    def authorize_admin_project
      authorize! :admin_project, user_project
    end

81 82 83 84
    def can?(object, action, subject)
      abilities.allowed?(object, action, subject)
    end

85 86 87 88 89 90 91 92 93 94 95
    # Checks the occurrences of required attributes, each attribute must be present in the params hash
    # or a Bad Request error is invoked.
    #
    # Parameters:
    #   keys (required) - A hash consisting of keys that must be present
    def required_attributes!(keys)
      keys.each do |key|
        bad_request!(key) unless params[key].present?
      end
    end

A
Alex Denisov 已提交
96
    def attributes_for_keys(keys)
A
Alex Denisov 已提交
97 98
      attrs = {}
      keys.each do |key|
99
        attrs[key] = params[key] if params[key].present? or (params.has_key?(key) and params[key] == false)
A
Alex Denisov 已提交
100 101 102 103
      end
      attrs
    end

104 105 106
    # error helpers

    def forbidden!
A
Alex Denisov 已提交
107
      render_api_error!('403 Forbidden', 403)
108 109
    end

110 111 112 113 114 115
    def bad_request!(attribute)
      message = ["400 (Bad request)"]
      message << "\"" + attribute.to_s + "\" not given"
      render_api_error!(message.join(' '), 400)
    end

116 117 118 119
    def not_found!(resource = nil)
      message = ["404"]
      message << resource if resource
      message << "Not Found"
A
Alex Denisov 已提交
120
      render_api_error!(message.join(' '), 404)
121 122 123
    end

    def unauthorized!
A
Alex Denisov 已提交
124
      render_api_error!('401 Unauthorized', 401)
125 126 127
    end

    def not_allowed!
A
Alex Denisov 已提交
128 129 130 131 132
      render_api_error!('Method Not Allowed', 405)
    end

    def render_api_error!(message, status)
      error!({'message' => message}, status)
133 134
    end

135
    private
R
randx 已提交
136 137 138

    def abilities
      @abilities ||= begin
139 140 141 142
                       abilities = Six.new
                       abilities << Ability
                       abilities
                     end
R
randx 已提交
143
    end
N
Nihad Abbasov 已提交
144 145
  end
end