|
1
|
|
|
# frozen_string_literal: true |
|
2
|
|
|
|
|
3
|
|
|
require 'ama-entity-mapper' |
|
4
|
|
|
|
|
5
|
|
|
require_relative 'privilege' |
|
6
|
|
|
require_relative 'partition/filter' |
|
7
|
|
|
require_relative 'partition/policy_group' |
|
8
|
|
|
|
|
9
|
|
|
module AMA |
|
10
|
|
|
module Chef |
|
11
|
|
|
module User |
|
12
|
|
|
module Model |
|
13
|
|
|
# Represents definition of client partition: how clients will be matched |
|
14
|
|
|
# and what would be done next |
|
15
|
|
|
class Partition |
|
16
|
|
|
include Mixin::Entity |
|
17
|
|
|
|
|
18
|
|
|
attribute :id, Symbol |
|
19
|
|
|
attribute :privileges, [Hash, K: Symbol, V: Privilege], default: {} |
|
20
|
|
|
attribute :filters, [Enumerable, T: Filter], default: [] |
|
21
|
|
|
attribute :policy, PolicyGroup, default: PolicyGroup::DEFAULT |
|
22
|
|
|
attribute :impersonation, [Hash, K: Symbol, V: [:*, NilClass]] |
|
23
|
|
|
|
|
24
|
|
|
denormalizer_block do |input, type, context, &block| |
|
25
|
|
|
input = [context.path.current.name] if input.nil? |
|
26
|
|
|
if input.is_a?(Enumerable) && !input.is_a?(Hash) |
|
27
|
|
|
input = { filters: input } |
|
28
|
|
|
end |
|
29
|
|
|
if input.is_a?(Hash) && ['id', :id].none? { |key| input.key?(key) } |
|
30
|
|
|
input[:id] = context.path.current.name |
|
31
|
|
|
end |
|
32
|
|
|
block.call(input, type, context) |
|
33
|
|
|
end |
|
34
|
|
|
|
|
35
|
|
|
# @param [AMA::Chef::User::Model::Client] client |
|
36
|
|
|
def applies_to(client) |
|
37
|
|
|
filters.any? { |filter| filter.apply(client) } |
|
38
|
|
|
end |
|
39
|
|
|
|
|
40
|
|
|
def to_s |
|
41
|
|
|
"Partition #{id}" |
|
42
|
|
|
end |
|
43
|
|
|
end |
|
44
|
|
|
end |
|
45
|
|
|
end |
|
46
|
|
|
end |
|
47
|
|
|
end |
|
48
|
|
|
|