-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPermissionCluster.rb
195 lines (180 loc) · 5.27 KB
/
PermissionCluster.rb
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
require File.join(File.expand_path(File.dirname(__FILE__)), 'Permission')
require File.join(File.expand_path(File.dirname(__FILE__)), 'PermissionGroup')
require File.join(File.expand_path(File.dirname(__FILE__)), 'PermissionUtils')
require File.join(File.expand_path(File.dirname(__FILE__)), 'Permissible')
# Adds the ability for child permissions.
# Warning, all child permissions share the same @negated.
class PermissionCluster
include Enumerable
# @!attribute [r] permission
# The permission to be identified as
# @!attribute [r] children
# Child permissions
# @!attribute [r] negated
# true if negated, false otherwise.
attr_reader :permission, :children, :negated
def initialize(string, children)
case string
when String
fail("Invalid permission string.") if string.end_with?(?*) || string.include?('..')
@negated = PermissionUtils.negated?(string)
@permission = @negated ? string.sub('-', '') : string
@children = []
add_child(children)
when PermissionCluster
@permission = String.new(string.permission)
@negated = string.negated
@children = Array.new(string.children)
add_child(children)
else
fail(TypeError, "Unknown type.")
end
end
# @param children [String, Permission, PermissionGroup, PermissionCluster, Permissible, Array<String, Permission, PermissionGroup, PermissionCluster, Permissible>]
# @param clear [Boolean] Reset @children before adding.
# @return [Boolean]
def add_child(children, clear = false)
case children
when String
begin
p = PermissionUtils.create(children)
p.negate!(@negated)
@children << p
true
rescue RuntimeError
false
end
when Permission || PermissionGroup
children.negate!(@negated)
@children << children
true
when PermissionCluster
children.negate!(@negated)
if clear
@children = children.children
else
@children << children.children
end
true
when Permissible || Array
# WHY SUPPORT THIS BULK OF PERMISSIONS ._.
@children.clear if clear
children.each do |x|
add_child(x)
end
end
end
# @param children [String, Permission, PermissionGroup, PermissionCluster, Permissible, Array<String, Permission, PermissionGroup, PermissionCluster, Permissible>]
def remove_child(children)
case children
when String
@children.delete_if { |x| x.permission = children }
when Permission || PermissionGroup
remove_child(children.permission)
when PermissionCluster || Array || Permissible
children.each { |x| remove_child(x.permission) }
end
end
# @param child [String, Permission, PermissionGroup]
# @return [Boolean]
def has_child?(child)
case child
when String
@children.any? do |x|
case x
when Permission
x == child
when PermissionGroup
x == child || x.include?(child)
end
end
when Permission || PermissionGroup
has_child?(child.permission)
else
false
end
end
def get_child(child)
case child
when String
@children.select do |x|
case x
when Permission
x == child
when PermissionGroup
x == child || x.include?(child)
end
end
when Permission || PermissionGroup
get_child(child.permission)
else
false
end
end
# Creates a new PermissionCluster object with the same permission
# and children with @negated as true/false
#
# @param negate [Boolean] boolean to set @negated
# @return [PermissionCluster] A new PermissionCluster object.
def negate(negate = true)
case negate
when true
return self if @negated
PermissionCluster.new("-#{@permission}", @children)
when false
return self unless @negated
PermissionCluster.new(@permission, @children)
end
end
# Set @negated for self & children
#
# @param negate [Boolean] boolean to set @negated
# @return [PermissionCluster] A new PermissionCluster object.
def negate!(negate = true)
@negate = negate
@children.each { |x| x.negate!(@negate) }
self
end
# Calls block for each node in permission.
# @yield [String] a node in permission.
# @return [Enumerator] if block wasn't given.
def each(&block)
if block_given?
@children.each do |node|
block.call node
end
else
@children.to_enum
end
end
# @return [Boolean]
def empty?
@children.empty?
end
def inspect
"#<PermissionCluster:0x#{'%x' % (self.object_id << 1)} Permission=#{@permission.inspect} negated=#{@negated.inspect} Children=#{@children.length}>"
end
# @return [PermissionCluster] A new PermissionCluster object with !@negated
def !
@negated ? PermissionCluster.new(@permission, @children) : Permission.new("-#{@permission}", @children)
end
alias_method :inverse, :'!'
def ==(object)
case object
when String
@permission == object
when PermissionCluster
(@negated == object.negated) && (@permission == object.permission) && (@children == object.children)
else
nil
end
end
# Inverts @negated.
# @return [self]
def inverse!
negate!(!@negated)
end
def to_str
@negated ? "-#{@permission}" : @permission
end
end