Module: DeclarativePolicy

Extended by:
PreferredScope
Defined in:
lib/declarative_policy.rb,
lib/declarative_policy/base.rb,
lib/declarative_policy/rule.rb,
lib/declarative_policy/step.rb,
lib/declarative_policy/cache.rb,
lib/declarative_policy/runner.rb,
lib/declarative_policy/rule_dsl.rb,
lib/declarative_policy/condition.rb,
lib/declarative_policy/policy_dsl.rb,
lib/declarative_policy/delegate_dsl.rb,
lib/declarative_policy/preferred_scope.rb

Defined Under Namespace

Modules: Cache, PreferredScope, Rule Classes: Base, Condition, DelegateDsl, ManifestCondition, PolicyDsl, RuleDsl, Runner, Step

Constant Summary collapse

CLASS_CACHE_MUTEX =
Mutex.new
CLASS_CACHE_IVAR =
:@__DeclarativePolicy_CLASS_CACHE

Constants included from PreferredScope

PreferredScope::PREFERRED_SCOPE_KEY

Class Method Summary collapse

Methods included from PreferredScope

preferred_scope, preferred_scope=, subject_scope, user_scope, with_preferred_scope

Class Method Details

.class_for(subject) ⇒ Object


35
36
37
38
39
40
41
42
43
44
45
# File 'lib/declarative_policy.rb', line 35

def class_for(subject)
  return GlobalPolicy if subject == :global
  return NilPolicy if subject.nil?

  subject = find_delegate(subject)

  policy_class = class_for_class(subject.class)
  raise "no policy for #{subject.class.name}" if policy_class.nil?

  policy_class
end

.has_policy?(subject) ⇒ Boolean

Returns:

  • (Boolean)

47
48
49
# File 'lib/declarative_policy.rb', line 47

def has_policy?(subject)
  !class_for_class(subject.class).nil?
end

.policy_for(user, subject, opts = {}) ⇒ Object


22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/declarative_policy.rb', line 22

def policy_for(user, subject, opts = {})
  cache = opts[:cache] || {}
  key = Cache.policy_key(user, subject)

  cache[key] ||=
    # to avoid deadlocks in multi-threaded environment when
    # autoloading is enabled, we allow concurrent loads,
    # https://gitlab.com/gitlab-org/gitlab-foss/issues/48263
    ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
      class_for(subject).new(user, subject, opts)
    end
end