summaryrefslogtreecommitdiff
path: root/app/policies/application_setting/term_policy.rb
blob: 17f00f33d35251b3b1743b3d1782364c2ccabcb9 (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
# frozen_string_literal: true

class ApplicationSetting
  class TermPolicy < BasePolicy
    include Gitlab::Utils::StrongMemoize

    condition(:current_terms, scope: :subject) do
      Gitlab::CurrentSettings.current_application_settings.latest_terms == @subject
    end

    condition(:terms_accepted, score: 1) do
      agreement&.accepted
    end

    rule { ~anonymous & current_terms }.policy do
      enable :accept_terms
      enable :decline_terms
    end

    rule { terms_accepted }.prevent :accept_terms

    def agreement
      strong_memoize(:agreement) do
        next nil if @user.nil? || @subject.nil?

        @user.term_agreements.find_by(term: @subject)
      end
    end
  end
end