-
Notifications
You must be signed in to change notification settings - Fork 158
/
membership.rb
160 lines (142 loc) · 4.2 KB
/
membership.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
class Membership < ActiveRecord::Base
# Membership levels
LEVELS = {
# A denied member is not a member of the group.
:denied => 0,
# A pending member has not yet been accepted into a group.
# We need to update authorization to reflect this.
:pending => 1,
# A regular member is a member of the group with full read permission.
:regular => 2,
# A leader controls the group.
:leader => 9,
}
# Membership Permissions
PERMISSIONS = {
:regular => {
:screen => {
:none => 0, # No screen write privledges
:subscriptions => 3, # Can update subscriptions
:all => 9, # Full write privledges
},
:feed => {
:none => 0, # No feed write privledges
:submissions => 3, # Can update submissions (moderate)
:all => 9 # Full write privledges
},
}
}
after_initialize :expand_permissions
before_save :compact_permissions
belongs_to :user
belongs_to :group
# Validations
validates :user, :presence => true, :associated => true
validates :group, :presence => true, :associated => true
validates_uniqueness_of :user_id, :scope => :group_id
# Scoping shortcuts for leaders/regular
scope :leader, where(:level => Membership::LEVELS[:leader])
scope :regular, where(:level => Membership::LEVELS[:regular])
# Scoping shortcuts for workflow (approved/pending/denied)
scope :approved, where("level > #{Membership::LEVELS[:pending]}")
scope :pending, where(:level => Membership::LEVELS[:pending])
scope :denied, where(:level => Membership::LEVELS[:denied])
attr_accessor :perms
def expand_permissions
self.perms = {}
level_sym = level_name.to_sym
if PERMISSIONS.include?(level_sym) && !permissions.nil?
local_perms = PERMISSIONS[level_sym]
local_perms.each_with_index do |(key, value), index|
if index == 0
p_value = permissions % 10
else
p_value = permissions / (10**index)
p_value = p_value % 10
end
type = (local_perms[key].respond_to?(:key) ? local_perms[key].key(p_value) : local_perms[key].index(p_value))
if !type.nil?
self.perms[key] = type
else
self.perms[key] = nil
end
end
end
self.perms
end
def compact_permissions
level_sym = level_name.to_sym
if PERMISSIONS.include?(level_sym)
local_perms = PERMISSIONS[level_sym]
new_permissions = 0
local_perms.each_with_index do |(key, value), index|
p_value = 0
if perms.include?(key)
p_sym = perms[key].to_sym
p_value = local_perms[key][p_sym]
end
if index == 0
if !p_value.nil?
new_permissions = p_value
else
new_permissions = 0
end
else
new_permissions += p_value * (10**index)
end
end
self.permissions = new_permissions
end
end
# Get level name of a membership
def level_name
name = (Membership::LEVELS.respond_to?(:key) ? Membership::LEVELS.key(level) : Membership::LEVELS.index(level)).to_s
end
# Test if the membership has been approved.
def is_approved?
level > Membership::LEVELS[:pending]
end
# Test if the membership has been denied.
def is_denied?
level == Membership::LEVELS[:denied]
end
# Test if the membership is pending.
def is_pending?
level == Membership::LEVELS[:pending]
end
# A shortcut to test if a membership represents a leader
def is_leader?
level == Membership::LEVELS[:leader]
end
# Approve a user in group
def approve()
if self.can_resign_leadership? && update_attributes({:level => Membership::LEVELS[:regular]})
true
else
reload
false
end
end
# Make a regular member a group leader
def promote_to_leader
if update_attributes({:level => Membership::LEVELS[:leader]})
true
else
reload
false
end
end
# Deny a user in group
def deny()
if update_attributes({:level => Membership::LEVELS[:denied]})
true
else
reload
false
end
end
# Can a group leader resign his commission?
def can_resign_leadership?
return self.group.leaders.count > 1 || !self.is_leader?
end
end