helpers.rb 4.4 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
      private_token = (params[PRIVATE_TOKEN_PARAM] || env[PRIVATE_TOKEN_HEADER]).to_s
S
skv 已提交
10
      @current_user ||= User.find_by(authentication_token: private_token)
11 12 13 14 15

      unless @current_user && Gitlab::UserAccess.allowed?(@current_user)
        return nil
      end

16
      identifier = sudo_identifier()
17

18 19 20
      # 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 已提交
21
        @current_user = User.by_username_or_id(identifier)
N
Nihad Abbasov 已提交
22
        not_found!("No user id or username for: #{identifier}") if @current_user.nil?
23
      end
24

25 26 27 28
      @current_user
    end

    def sudo_identifier()
29
      identifier ||= params[SUDO_PARAM] ||= env[SUDO_HEADER]
30

31
      # Regex for integers
32 33 34 35 36
      if (!!(identifier =~ /^[0-9]+$/))
        identifier.to_i
      else
        identifier
      end
N
Nihad Abbasov 已提交
37 38
    end

N
Nihad Abbasov 已提交
39
    def user_project
40
      @project ||= find_project(params[:id])
41 42 43
      @project || not_found!
    end

44
    def find_project(id)
45
      project = Project.find_with_namespace(id) || Project.find_by(id: id)
46 47 48

      if project && can?(current_user, :read_project, project)
        project
N
Nihad Abbasov 已提交
49
      else
50
        nil
N
Nihad Abbasov 已提交
51
      end
N
Nihad Abbasov 已提交
52 53
    end

N
Nihad Abbasov 已提交
54 55 56 57 58 59
    def paginate(relation)
      per_page  = params[:per_page].to_i
      paginated = relation.page(params[:page]).per(per_page)
      add_pagination_headers(paginated, per_page)

      paginated
N
Nihad Abbasov 已提交
60 61
    end

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

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

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

D
Dmitriy Zaporozhets 已提交
76 77 78 79
    def authorize_push_project
      authorize! :push_code, user_project
    end

80 81 82 83
    def authorize_admin_project
      authorize! :admin_project, user_project
    end

84 85 86 87
    def can?(object, action, subject)
      abilities.allowed?(object, action, subject)
    end

88 89 90 91 92 93 94 95 96 97 98
    # 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 已提交
99
    def attributes_for_keys(keys)
A
Alex Denisov 已提交
100
      attrs = {}
101

A
Alex Denisov 已提交
102
      keys.each do |key|
103 104 105
        if params[key].present? or (params.has_key?(key) and params[key] == false)
          attrs[key] = params[key]
        end
A
Alex Denisov 已提交
106
      end
107 108

      ActionController::Parameters.new(attrs).permit!
A
Alex Denisov 已提交
109 110
    end

111 112 113
    # error helpers

    def forbidden!
A
Alex Denisov 已提交
114
      render_api_error!('403 Forbidden', 403)
115 116
    end

117 118 119 120 121 122
    def bad_request!(attribute)
      message = ["400 (Bad request)"]
      message << "\"" + attribute.to_s + "\" not given"
      render_api_error!(message.join(' '), 400)
    end

123 124 125 126
    def not_found!(resource = nil)
      message = ["404"]
      message << resource if resource
      message << "Not Found"
A
Alex Denisov 已提交
127
      render_api_error!(message.join(' '), 404)
128 129 130
    end

    def unauthorized!
A
Alex Denisov 已提交
131
      render_api_error!('401 Unauthorized', 401)
132 133 134
    end

    def not_allowed!
A
Alex Denisov 已提交
135 136 137 138 139
      render_api_error!('Method Not Allowed', 405)
    end

    def render_api_error!(message, status)
      error!({'message' => message}, status)
140 141
    end

142
    private
R
randx 已提交
143

N
Nihad Abbasov 已提交
144 145 146 147 148 149 150 151 152 153 154 155
    def add_pagination_headers(paginated, per_page)
      request_url = request.url.split('?').first

      links = []
      links << %(<#{request_url}?page=#{paginated.current_page - 1}&per_page=#{per_page}>; rel="prev") unless paginated.first_page?
      links << %(<#{request_url}?page=#{paginated.current_page + 1}&per_page=#{per_page}>; rel="next") unless paginated.last_page?
      links << %(<#{request_url}?page=1&per_page=#{per_page}>; rel="first")
      links << %(<#{request_url}?page=#{paginated.total_pages}&per_page=#{per_page}>; rel="last")

      header 'Link', links.join(', ')
    end

R
randx 已提交
156 157
    def abilities
      @abilities ||= begin
158 159 160 161
                       abilities = Six.new
                       abilities << Ability
                       abilities
                     end
R
randx 已提交
162
    end
N
Nihad Abbasov 已提交
163 164
  end
end