summaryrefslogtreecommitdiff
path: root/lib/gitlab/cross_project_access/check_info.rb
blob: 2a9eacad680dfade16a9e6d0c2d323704de8f7ca (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
63
64
65
66
67
68
# frozen_string_literal: true

module Gitlab
  class CrossProjectAccess
    class CheckInfo
      attr_accessor :actions, :positive_condition, :negative_condition, :skip

      def initialize(actions, positive_condition, negative_condition, skip)
        @actions = actions
        @positive_condition = positive_condition
        @negative_condition = negative_condition
        @skip = skip
      end

      def should_skip?(object)
        return !should_run?(object) unless @skip

        skip_for_action = @actions[current_action(object)]
        skip_for_action = false if @actions[current_action(object)].nil?

        # We need to do the opposite of what was defined in the following cases:
        # - skip_cross_project_access_check index: true, if: -> { false }
        # - skip_cross_project_access_check index: true, unless: -> { true }
        if positive_condition_is_false?(object)
          skip_for_action = !skip_for_action
        end

        if negative_condition_is_true?(object)
          skip_for_action = !skip_for_action
        end

        skip_for_action
      end

      def should_run?(object)
        return !should_skip?(object) if @skip

        run_for_action = @actions[current_action(object)]
        run_for_action = true if @actions[current_action(object)].nil?

        # We need to do the opposite of what was defined in the following cases:
        # - requires_cross_project_access index: true, if: -> { false }
        # - requires_cross_project_access index: true, unless: -> { true }
        if positive_condition_is_false?(object)
          run_for_action = !run_for_action
        end

        if negative_condition_is_true?(object)
          run_for_action = !run_for_action
        end

        run_for_action
      end

      def positive_condition_is_false?(object)
        @positive_condition && !object.instance_exec(&@positive_condition)
      end

      def negative_condition_is_true?(object)
        @negative_condition && object.instance_exec(&@negative_condition)
      end

      def current_action(object)
        object.respond_to?(:action_name) ? object.action_name.to_sym : nil
      end
    end
  end
end