helpers.rb 2.4 KB
Newer Older
1
module API
N
Nihad Abbasov 已提交
2 3
  module APIHelpers
    def current_user
V
Valeriy Sizov 已提交
4
      @current_user ||= User.find_by_authentication_token(params[:private_token] || env["HTTP_PRIVATE_TOKEN"])
N
Nihad Abbasov 已提交
5 6
    end

N
Nihad Abbasov 已提交
7
    def user_project
8
      @project ||= find_project(params[:id])
9 10 11
      @project || not_found!
    end

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

      if project && can?(current_user, :read_project, project)
        project
N
Nihad Abbasov 已提交
17
      else
18
        nil
N
Nihad Abbasov 已提交
19
      end
N
Nihad Abbasov 已提交
20 21
    end

N
Nihad Abbasov 已提交
22 23 24 25
    def paginate(object)
      object.page(params[:page]).per(params[:per_page].to_i)
    end

N
Nihad Abbasov 已提交
26
    def authenticate!
27
      unauthorized! unless current_user
N
Nihad Abbasov 已提交
28
    end
R
randx 已提交
29

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

R
randx 已提交
34 35
    def authorize! action, subject
      unless abilities.allowed?(current_user, action, subject)
36
        forbidden!
R
randx 已提交
37 38 39
      end
    end

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

44 45 46 47 48 49 50 51 52 53 54
    # 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 已提交
55
    def attributes_for_keys(keys)
A
Alex Denisov 已提交
56 57 58 59 60 61 62
      attrs = {}
      keys.each do |key|
        attrs[key] = params[key] if params[key].present?
      end
      attrs
    end

63 64 65
    # error helpers

    def forbidden!
A
Alex Denisov 已提交
66
      render_api_error!('403 Forbidden', 403)
67 68
    end

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

75 76 77 78
    def not_found!(resource = nil)
      message = ["404"]
      message << resource if resource
      message << "Not Found"
A
Alex Denisov 已提交
79
      render_api_error!(message.join(' '), 404)
80 81 82
    end

    def unauthorized!
A
Alex Denisov 已提交
83
      render_api_error!('401 Unauthorized', 401)
84 85 86
    end

    def not_allowed!
A
Alex Denisov 已提交
87 88 89 90 91
      render_api_error!('Method Not Allowed', 405)
    end

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

94
    private
R
randx 已提交
95 96 97 98 99 100 101 102

    def abilities
      @abilities ||= begin
                       abilities = Six.new
                       abilities << Ability
                       abilities
                     end
    end
N
Nihad Abbasov 已提交
103 104
  end
end