-
Notifications
You must be signed in to change notification settings - Fork 1
/
catalog_user.rb
183 lines (130 loc) · 5.2 KB
/
catalog_user.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
# a catalog user is a set of permissions for an persona
# the catalog user is always linked to a account user
#
class CatalogUser < ActiveRecord::Base
belongs_to :catalog
belongs_to :user
belongs_to :account
belongs_to :catalog
belongs_to :account_user
after_commit :flush_cache
#before_save :update_uuids
#after_create :attach_to_account_user
#after_destroy :update_catalog_counter_cache
scope :invited, -> { where( role: 'Catalog User').order("email asc") }
scope :account_users, -> { where( role: 'Account User').order("email asc") }
scope :account_owners, -> { where( role: 'Account Owner').order("email asc") }
# catalog users comes in four flavors
# 1: Catalog User, this role is set when the catalog user is invited to a catalog
# 2: Account User, this role is set when a user is invited to an account
ROLE = ['Catalog User', 'Account User', 'Account Owner']
def downgrade
end
#def update_catalog_counter_cache
# #CatalogUserCounterCachWorker.perform_async(self.catalog_id)
#end
#def update_uuids
# #self.uuid = UUIDTools::UUID.timestamp_create().to_s
#end
#
def self.cached_find(id)
Rails.cache.fetch([name, id]) { find(id) }
end
def self.cached_where(catalog_id, user_id)
Rails.cache.fetch([ 'catalog_user', catalog_id, user_id]) { where( catalog_id: catalog_id,
user_id: user_id ).first
}
end
# when a catalog user is created, the account user attached to an account_user
# so the persona can access the hosting account true the account user
def attach_to_account_user
# find or create an account user for all catalog users
account_user = AccountUser.where( account_id: self.account_id,
user_id: self.user_id)
.first_or_create( account_id: self.account_id,
user_id: self.user_id,
role: 'Catalog User',
email: self.user.email)
account_user.save!
self.account_user_id = account_user.id
self.save!
end
# can update catalog_user
def can_update_catalog_user catalog_user
#puts '+++++++++++++++++++++++ can_update_catalog_user ++++++++++++++++++++++++++'
# only if there is permissions to update
return false unless self.update_user
handle_user_permissions_for catalog_user
end
# can delete catalog_user
def can_delete_catalog_user catalog_user
#puts '+++++++++++++++++++++++ can_delete_catalog_user ++++++++++++++++++++++++++'
# only if there is permissions to update
return false unless self.delete_user
handle_user_permissions_for catalog_user
end
# permissions based on catalog_user
def handle_user_permissions_for catalog_user
#puts '+++++++++++++++++++++++ handle_user_permissions_for ++++++++++++++++++++++++++'
#puts catalog_user.user.email
#puts catalog_user.role
# always edit account users
return true if catalog_user.role == 'Account User'
# newer edit the account owner
return false if catalog_user.role == 'Account Owner'
# never edit the administrator
return false if catalog_user.role == 'Administrator'
# never grand catalog users
# access to the account
return true if catalog_user.role == 'Catalog User'
puts '+++++++++++++++++++++++++++++++++++++++++++++++++'
puts 'ERROR: Unable assign edit permmision for catalog user'
puts 'In CatalogUser#has_permisions'
puts '+++++++++++++++++++++++++++++++++++++++++++++++++'
false
end
def can_read_catalog_user( catalog_user )
# supers can se everything
return true if self.user.role == 'Super'
# never update the administrator
return false if account.administrator_id == catalog_user.user_id
# always show catalog users
return true if catalog_user.role == 'Catalog User'
# no permissions
false
end
def access_assets?
return true if self.read_file
return true if self.read_legal_document
return true if self.read_financial_document
return true if self.read_artwork
end
def add_assets?
return true if self.create_recording
return true if self.create_file
return true if self.create_legal_document
return true if self.create_financial_document
end
def grand_all_permissions
Permissions::TYPES.each do |permission|
self[permission] = true
end
self.save!
end
def copy_permissions_from_account_user account_user
Permissions::TYPES.each do |permission|
self[permission] = account_user[permission]
end
self.save!
end
private
#def before_destroy
#
#end
def flush_cache
Rails.cache.delete([self.class.name, id])
Rails.cache.delete(['catalog_user', catalog_id, user_id])
#self.catalog.count_users
#self.catalog.save!
end
end