helpers.rb 3.5 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 11 12 13 14
      @current_user ||= User.find_by_authentication_token(params[PRIVATE_TOKEN_PARAM] || env[PRIVATE_TOKEN_HEADER])
      identifier = sudo_identifier()
      # 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?
        begin
15
          @current_user = User.by_username_or_id(identifier)
16 17 18
        rescue => ex
          not_found!("No user id or username for: #{identifier}")
        end
19
        not_found!("No user id or username for: #{identifier}") if current_user.nil?
20 21 22 23 24
      end
      @current_user
    end

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

N
Nihad Abbasov 已提交
34
    def user_project
35
      @project ||= find_project(params[:id])
36 37 38
      @project || not_found!
    end

39 40
    def find_project(id)
      project = Project.find_by_id(id) || Project.find_with_namespace(id)
41 42 43

      if project && can?(current_user, :read_project, project)
        project
N
Nihad Abbasov 已提交
44
      else
45
        nil
N
Nihad Abbasov 已提交
46
      end
N
Nihad Abbasov 已提交
47 48
    end

N
Nihad Abbasov 已提交
49 50 51 52
    def paginate(object)
      object.page(params[:page]).per(params[:per_page].to_i)
    end

N
Nihad Abbasov 已提交
53
    def authenticate!
54
      unauthorized! unless current_user
N
Nihad Abbasov 已提交
55
    end
R
randx 已提交
56

57 58 59 60
    def authenticated_as_admin!
      forbidden! unless current_user.is_admin?
    end

R
randx 已提交
61 62
    def authorize! action, subject
      unless abilities.allowed?(current_user, action, subject)
63
        forbidden!
R
randx 已提交
64 65 66
      end
    end

67 68 69 70
    def authorize_admin_project
      authorize! :admin_project, user_project
    end

71 72 73 74
    def can?(object, action, subject)
      abilities.allowed?(object, action, subject)
    end

75 76 77 78 79 80 81 82 83 84 85
    # 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 已提交
86
    def attributes_for_keys(keys)
A
Alex Denisov 已提交
87 88
      attrs = {}
      keys.each do |key|
89
        attrs[key] = params[key] if params[key].present? or (params.has_key?(key) and params[key] == false)
A
Alex Denisov 已提交
90 91 92 93
      end
      attrs
    end

94 95 96
    # error helpers

    def forbidden!
A
Alex Denisov 已提交
97
      render_api_error!('403 Forbidden', 403)
98 99
    end

100 101 102 103 104 105
    def bad_request!(attribute)
      message = ["400 (Bad request)"]
      message << "\"" + attribute.to_s + "\" not given"
      render_api_error!(message.join(' '), 400)
    end

106 107 108 109
    def not_found!(resource = nil)
      message = ["404"]
      message << resource if resource
      message << "Not Found"
A
Alex Denisov 已提交
110
      render_api_error!(message.join(' '), 404)
111 112 113
    end

    def unauthorized!
A
Alex Denisov 已提交
114
      render_api_error!('401 Unauthorized', 401)
115 116 117
    end

    def not_allowed!
A
Alex Denisov 已提交
118 119 120 121 122
      render_api_error!('Method Not Allowed', 405)
    end

    def render_api_error!(message, status)
      error!({'message' => message}, status)
123 124
    end

125
    private
R
randx 已提交
126 127 128

    def abilities
      @abilities ||= begin
129 130 131 132
                       abilities = Six.new
                       abilities << Ability
                       abilities
                     end
R
randx 已提交
133
    end
N
Nihad Abbasov 已提交
134 135
  end
end