forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccount_user.rb
More file actions
169 lines (140 loc) · 4.95 KB
/
Copy pathaccount_user.rb
File metadata and controls
169 lines (140 loc) · 4.95 KB
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
#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
class AccountUser < ActiveRecord::Base
belongs_to :account
belongs_to :user
belongs_to :role
include Role::AssociationHelper
has_many :role_overrides, :as => :context, :inverse_of => :context
has_a_broadcast_policy
before_validation :infer_defaults
after_save :touch_user
after_destroy :touch_user
after_save :update_account_associations_if_changed
after_destroy :update_account_associations_later
validate :valid_role?
validates_presence_of :account_id, :user_id, :role_id
alias_method :context, :account
def update_account_associations_if_changed
if (self.account_id_changed? || self.user_id_changed?)
if self.new_record?
return if %w{creation_pending deleted}.include?(self.user.workflow_state)
account_chain = self.account.account_chain
associations = {}
account_chain.each_with_index { |account, idx| associations[account.id] = idx }
self.user.update_account_associations(:incremental => true, :precalculated_associations => associations)
else
self.user.update_account_associations_later
end
end
end
def update_account_associations_later
self.user.update_account_associations_later
end
def infer_defaults
self.role ||= Role.get_built_in_role('AccountAdmin')
end
def valid_role?
return true if role.built_in?
unless role.account_role?
self.errors.add(:role_id, "is not a valid account role")
end
unless self.account.valid_role?(role)
self.errors.add(:role_id, "is not an available role for this account")
end
end
set_broadcast_policy do |p|
p.dispatch :new_account_user
p.to {|record| record.account.users}
p.whenever {|record| record.just_created }
p.dispatch :account_user_registration
p.to {|record| record.user }
p.whenever {|record| @account_user_registration }
p.dispatch :account_user_notification
p.to {|record| record.user }
p.whenever {|record| @account_user_notification }
end
set_policy do
given { |user| self.account.grants_right?(user, :manage_account_memberships) && is_subset_of?(user) }
can :create and can :destroy
end
def readable_type
AccountUser.readable_type(self.role.name)
end
def account_user_registration!
@account_user_registration = true
self.save!
@account_user_registration = false
end
def account_user_notification!
@account_user_notification = true
self.save!
@account_user_notification = false
end
def enabled_for?(context, action)
@permission_lookup ||= {}
@permission_lookup[[context.class, context.global_id, action]] ||= RoleOverride.enabled_for?(context, action, self.role, self.account)
end
def has_permission_to?(context, action)
enabled_for?(context, action).include?(:self)
end
def self.all_permissions_for(user, account)
account_users = account.account_users_for(user)
result = {}
account_users.each do |account_user|
RoleOverride.permissions.keys.each do |permission|
result[permission] ||= []
result[permission] |= account_user.enabled_for?(account, permission)
end
end
result
end
def is_subset_of?(user)
needed_permissions = RoleOverride.manageable_permissions(account).keys.inject({}) do |result, permission|
result[permission] = enabled_for?(account, permission)
result
end
target_permissions = AccountUser.all_permissions_for(user, account)
needed_permissions.all? do |(permission, needed_permission)|
next true unless needed_permission.present?
target_permission = target_permissions[permission]
next false unless target_permission.present?
(needed_permission - target_permission).empty?
end
end
def self.readable_type(type)
if type == 'AccountAdmin' || !type || type.empty?
t('types.account_admin', "Account Admin")
else
type
end
end
def self.any_for?(user)
!account_ids_for_user(user).empty?
end
def self.account_ids_for_user(user)
@account_ids_for ||= {}
@account_ids_for[user.id] ||= Rails.cache.fetch(['account_ids_for_user', user].cache_key) do
AccountUser.for_user(user).map(&:account_id)
end
end
def self.for_user_and_account?(user, account_id)
account_ids_for_user(user).include?(account_id)
end
scope :for_user, lambda { |user| where(:user_id => user) }
end