canvas-lms/app/models/account_user.rb

157 lines
4.7 KiB
Ruby
Raw Normal View History

2011-02-01 09:57:29 +08:00
#
# 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
has_many :role_overrides, :as => :context
has_a_broadcast_policy
before_save :infer_defaults
after_save :touch_user
after_destroy :touch_user
after_save :update_account_associations_if_changed
after_destroy :update_account_associations_later
attr_accessible :account, :user, :membership_type
EXPORTABLE_ATTRIBUTES = [:id, :account_id, :user_id, :membership_type, :created_at, :updated_at]
EXPORTABLE_ASSOCIATIONS = [:account, :user]
validates_presence_of :account_id, :user_id, :membership_type
alias_method :context, :account
BASE_ROLE_NAME = 'AccountMembership'
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
2011-02-01 09:57:29 +08:00
def infer_defaults
self.membership_type ||= 'AccountAdmin'
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
def readable_type
AccountUser.readable_type(self.membership_type)
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
add support for applying role overrides to just self or just descendants * wrap RoleOverride#permission_for with enabled_for? that also takes a context of where the permission is being applied, and recalculates its enabled-ness relative to that context; use that for checking account admin and enrollment permissions * refactor User#can_masquerade to properly check for descendant permissions test plan: * create a custom role in site admin. give it permission to manage permissions * in script/console, find that override and set apply_to_self=false * add a user to that role, and login as that user * the user should not be able to change permissions in site admin * the user should be able to change permissions in the default account * add another role in site admin. give it permission to manage permissions * in script/console, find the override and set apply_to_self=true, apply_to_descendants=false * add another user to that role, and login as that user * the user should be able to change permissions in site admin * the user should not be able to change permissions in the default account * the first user should not be able to masquerade as the second user and vice versa * an Account Admin should be able to masquerade as either user * create a custom role in the default account, give it permission to manage permissions, and add a user to that role * the first user should be able to masquerade as the new user; the second user should not be able to masquerade as the new user * general regression tests on permissions and masquerading Change-Id: I20a1183b7dfec419634a92cda498f245187060ef Reviewed-on: https://gerrit.instructure.com/15896 Reviewed-by: Cody Cutrer <cody@instructure.com> QA-Review: Cody Cutrer <cody@instructure.com> Tested-by: Cody Cutrer <cody@instructure.com>
2012-12-07 07:15:53 +08:00
def enabled_for?(context, action)
2011-02-01 09:57:29 +08:00
@permission_lookup ||= {}
@permission_lookup[[context.class, context.global_id, action]] ||= RoleOverride.enabled_for?(account, context, action, base_role_name, membership_type)
add support for applying role overrides to just self or just descendants * wrap RoleOverride#permission_for with enabled_for? that also takes a context of where the permission is being applied, and recalculates its enabled-ness relative to that context; use that for checking account admin and enrollment permissions * refactor User#can_masquerade to properly check for descendant permissions test plan: * create a custom role in site admin. give it permission to manage permissions * in script/console, find that override and set apply_to_self=false * add a user to that role, and login as that user * the user should not be able to change permissions in site admin * the user should be able to change permissions in the default account * add another role in site admin. give it permission to manage permissions * in script/console, find the override and set apply_to_self=true, apply_to_descendants=false * add another user to that role, and login as that user * the user should be able to change permissions in site admin * the user should not be able to change permissions in the default account * the first user should not be able to masquerade as the second user and vice versa * an Account Admin should be able to masquerade as either user * create a custom role in the default account, give it permission to manage permissions, and add a user to that role * the first user should be able to masquerade as the new user; the second user should not be able to masquerade as the new user * general regression tests on permissions and masquerading Change-Id: I20a1183b7dfec419634a92cda498f245187060ef Reviewed-on: https://gerrit.instructure.com/15896 Reviewed-by: Cody Cutrer <cody@instructure.com> QA-Review: Cody Cutrer <cody@instructure.com> Tested-by: Cody Cutrer <cody@instructure.com>
2012-12-07 07:15:53 +08:00
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.permissions.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 base_role_name
BASE_ROLE_NAME
2011-02-01 09:57:29 +08:00
end
def self.readable_type(type)
if type == 'AccountAdmin' || !type || type.empty?
t('types.account_admin', "Account Admin")
2011-02-01 09:57:29 +08:00
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) }
2011-02-01 09:57:29 +08:00
end