summaryrefslogtreecommitdiff
path: root/lib/gitlab/auth/result.rb
blob: 69525a281e9d5f6d6653cfc4ffc66fe268af4cc2 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# frozen_string_literal: true

module Gitlab
  module Auth
    class Result
      attr_reader :actor, :project, :type, :authentication_abilities

      def initialize(actor, project, type, authentication_abilities)
        @actor = actor
        @project = project
        @type = type
        @authentication_abilities = authentication_abilities
      end

      EMPTY = self.new(nil, nil, nil, nil).freeze

      def ci?(for_project)
        type == :ci &&
          project &&
          project == for_project
      end

      def lfs_deploy_token?(for_project)
        type == :lfs_deploy_token &&
          actor.try(:has_access_to?, for_project)
      end

      def success?
        actor.present? || type == :ci
      end

      def failed?
        !success?
      end

      def auth_user
        actor.is_a?(User) ? actor : nil
      end
      alias_method :user, :auth_user

      def deploy_token
        actor.is_a?(DeployToken) ? actor : nil
      end

      def can?(action)
        actor&.can?(action)
      end

      def can_perform_action_on_project?(action, given_project)
        Ability.allowed?(actor, action, given_project)
      end

      def authentication_abilities_include?(ability)
        return false if authentication_abilities.blank?

        authentication_abilities.include?(ability)
      end
    end
  end
end

Gitlab::Auth::Result.prepend_mod_with('Gitlab::Auth::Result')