summaryrefslogtreecommitdiff
path: root/lib/api/helpers.rb
blob: c0ba874790a311d84c66bbb859e958ef32ba4cd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module Gitlab
  module APIHelpers
    def current_user
      @current_user ||= User.find_by_authentication_token(params[:private_token])
    end

    def user_project
      if @project ||= current_user.projects.find_by_id(params[:id]) ||
                      current_user.projects.find_by_code(params[:id])
      else
        error!({'message' => '404 Not found'}, 404)
      end

      @project
    end

    def paginate(object)
      object.page(params[:page]).per(params[:per_page].to_i)
    end

    def authenticate!
      error!({'message' => '401 Unauthorized'}, 401) unless current_user
    end

    def authorize! action, subject
      unless abilities.allowed?(current_user, action, subject)
        error!({'message' => '403 Forbidden'}, 403)
      end
    end

    private 

    def abilities
      @abilities ||= begin
                       abilities = Six.new
                       abilities << Ability
                       abilities
                     end
    end
  end
end