Module: Sequel::Privacy::PolicyDSL

Extended by:
T::Helpers, T::Sig
Defined in:
lib/sequel/privacy/policy_dsl.rb

Overview

DSL for defining custom policies. Extend your policy module with this to get the ‘policy` method.

Example:

module P
  extend Sequel::Privacy::PolicyDSL

  AlwaysDeny = Sequel::Privacy::BuiltInPolicies::AlwaysDeny

  policy :AllowAdmins, ->(actor) {
    allow if actor.is_role?(:admin)
  }, 'Allow admin users', cacheable: true
end

Instance Method Summary collapse

Instance Method Details

#policy(name, lam, comment = nil, cacheable: true, single_match: false, cache_by: nil, allow_anonymous: false) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sequel/privacy/policy_dsl.rb', line 41

def policy(name, lam, comment = nil, cacheable: true, single_match: false, cache_by: nil,
           allow_anonymous: false)
  p = Policy.new(&lam).setup(
    policy_name: name,
    comment: comment,
    cacheable: cacheable,
    single_match: single_match,
    cache_by: cache_by,
    allow_anonymous: allow_anonymous
  )
  const_set(name, p)
end

#policy_factory(name, factory, comment = nil, cacheable: true, single_match: false, cache_by: nil, allow_anonymous: false) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sequel/privacy/policy_dsl.rb', line 65

def policy_factory(name, factory, comment = nil, cacheable: true, single_match: false, cache_by: nil,
                   allow_anonymous: false)
  policy_factory = PolicyFactory.new(
    name,
    factory,
    comment: comment,
    cacheable: cacheable,
    single_match: single_match,
    cache_by: cache_by,
    allow_anonymous: allow_anonymous
  )

  const_set(name, policy_factory)
  define_singleton_method(name) do |*args|
    policy_factory.call(*args)
  end
end