summaryrefslogtreecommitdiff
path: root/app/policies/ci/build_policy.rb
blob: d4af4490608622bf64494f489249d5f21bb858ee (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
module Ci
  class BuildPolicy < CommitStatusPolicy
    alias_method :build, :subject

    def rules
      super

      # If we can't read build we should also not have that
      # ability when looking at this in context of commit_status
      %w[read create update admin].each do |rule|
        cannot! :"#{rule}_commit_status" unless can? :"#{rule}_build"
      end

      if can?(:update_build) && protected_action?
        cannot! :update_build
      end
    end

    private

    def protected_action?
      return false unless build.action?

      !::Gitlab::UserAccess
        .new(user, project: build.project)
        .can_push_to_branch?(build.ref)
    end
  end
end