/
granter.rb
67 lines (54 loc) · 1.25 KB
/
granter.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
module Granted
class Granter
def initialize
end
def to(grantee)
accept(grantee: grantee)
end
def from(grantee)
accept(grantee: grantee)
end
def on(subject)
accept(subject: subject)
end
def rights(*rights)
accept(rights: [rights].flatten)
end
def right(right)
rights(right)
end
def grant
accept(action: :grant)
end
def revoke
accept(action: :revoke)
end
private
def accept(options)
@action ||= options[:action]
@grantee ||= options[:grantee]
@subject ||= options[:subject]
@rights ||= options[:rights]
finalize
end
def finalize
return self unless @grantee and @subject and @rights and @action
@rights.map do |right|
clazz = GrantClassFactory.get(right)
selector = clazz.grantee(@grantee).subject(@subject)
case @action.to_sym
when :grant then give_grant(selector)
when :revoke then revoke_grant(selector)
else raise "Invalid action @action"
end
end
end
def revoke_grant(selector)
return true unless g = selector.first
g.destroy
end
def give_grant(selector)
selector.first_or_create
end
end
end