helpers.rb 2.45 KB
Newer Older
1
module API
Nihad Abbasov's avatar
Nihad Abbasov committed
2 3
  module APIHelpers
    def current_user
4 5
      private_token = (params[:private_token] || env["HTTP_PRIVATE_TOKEN"]).to_s
      @current_user ||= User.find_by_authentication_token(private_token)
Nihad Abbasov's avatar
Nihad Abbasov committed
6 7
    end

Nihad Abbasov's avatar
Nihad Abbasov committed
8
    def user_project
9
      @project ||= find_project(params[:id])
10 11 12
      @project || not_found!
    end

13 14
    def find_project(id)
      project = Project.find_by_id(id) || Project.find_with_namespace(id)
15 16 17

      if project && can?(current_user, :read_project, project)
        project
18
      else
19
        nil
20
      end
Nihad Abbasov's avatar
Nihad Abbasov committed
21 22
    end

Nihad Abbasov's avatar
Nihad Abbasov committed
23 24 25 26
    def paginate(object)
      object.page(params[:page]).per(params[:per_page].to_i)
    end

Nihad Abbasov's avatar
Nihad Abbasov committed
27
    def authenticate!
28
      unauthorized! unless current_user
Nihad Abbasov's avatar
Nihad Abbasov committed
29
    end
randx's avatar
randx committed
30

31 32 33 34
    def authenticated_as_admin!
      forbidden! unless current_user.is_admin?
    end

randx's avatar
randx committed
35 36
    def authorize! action, subject
      unless abilities.allowed?(current_user, action, subject)
37
        forbidden!
randx's avatar
randx committed
38 39 40
      end
    end

41 42 43 44
    def can?(object, action, subject)
      abilities.allowed?(object, action, subject)
    end

45 46 47 48 49 50 51 52 53 54 55
    # 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

Alex Denisov's avatar
Alex Denisov committed
56
    def attributes_for_keys(keys)
Alex Denisov's avatar
Alex Denisov committed
57 58 59 60 61 62 63
      attrs = {}
      keys.each do |key|
        attrs[key] = params[key] if params[key].present?
      end
      attrs
    end

64 65 66
    # error helpers

    def forbidden!
Alex Denisov's avatar
Alex Denisov committed
67
      render_api_error!('403 Forbidden', 403)
68 69
    end

70 71 72 73 74 75
    def bad_request!(attribute)
      message = ["400 (Bad request)"]
      message << "\"" + attribute.to_s + "\" not given"
      render_api_error!(message.join(' '), 400)
    end

76 77 78 79
    def not_found!(resource = nil)
      message = ["404"]
      message << resource if resource
      message << "Not Found"
Alex Denisov's avatar
Alex Denisov committed
80
      render_api_error!(message.join(' '), 404)
81 82 83
    end

    def unauthorized!
Alex Denisov's avatar
Alex Denisov committed
84
      render_api_error!('401 Unauthorized', 401)
85 86 87
    end

    def not_allowed!
Alex Denisov's avatar
Alex Denisov committed
88 89 90 91 92
      render_api_error!('Method Not Allowed', 405)
    end

    def render_api_error!(message, status)
      error!({'message' => message}, status)
93 94
    end

95
    private
randx's avatar
randx committed
96 97 98 99 100 101 102 103

    def abilities
      @abilities ||= begin
                       abilities = Six.new
                       abilities << Ability
                       abilities
                     end
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
104 105
  end
end