2012-11-21 02:44:14 +08:00
|
|
|
#
|
|
|
|
# Copyright (C) 2012 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 Role < ActiveRecord::Base
|
|
|
|
belongs_to :account
|
2012-11-28 04:52:33 +08:00
|
|
|
belongs_to :root_account, :class_name => 'Account'
|
2012-11-21 02:44:14 +08:00
|
|
|
attr_accessible :name
|
2012-11-28 04:52:33 +08:00
|
|
|
before_validation :infer_root_account_id
|
2012-11-21 02:44:14 +08:00
|
|
|
validates_presence_of :name
|
2012-12-11 05:23:29 +08:00
|
|
|
validates_inclusion_of :base_role_type, :in => RoleOverride::BASE_ROLE_TYPES, :message => 'is invalid'
|
|
|
|
validates_exclusion_of :name, :in => RoleOverride::KNOWN_ROLE_TYPES + Enrollment::SIS_TYPES.values
|
2012-12-01 06:35:18 +08:00
|
|
|
validates_uniqueness_of :name, :scope => :account_id
|
2012-11-28 04:52:33 +08:00
|
|
|
validate :ensure_no_name_conflict_with_different_base_role_type
|
|
|
|
|
|
|
|
def infer_root_account_id
|
|
|
|
unless self.account
|
|
|
|
self.errors.add(:account_id)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
self.root_account_id = self.account.root_account_id || self.account.id
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_no_name_conflict_with_different_base_role_type
|
2012-12-04 00:05:38 +08:00
|
|
|
if self.root_account.all_roles.not_deleted.scoped(:conditions => ["name = ? AND base_role_type <> ?", self.name, self.base_role_type]).any?
|
2012-11-28 04:52:33 +08:00
|
|
|
self.errors.add(:name, 'is already taken by a different type of Role in the same root account')
|
|
|
|
end
|
|
|
|
end
|
2012-11-21 02:44:14 +08:00
|
|
|
|
|
|
|
include Workflow
|
|
|
|
workflow do
|
2012-11-30 05:23:30 +08:00
|
|
|
state :active do
|
|
|
|
event :deactivate, :transitions_to => :inactive
|
|
|
|
end
|
|
|
|
state :inactive do
|
|
|
|
event :activate, :transitions_to => :active
|
|
|
|
end
|
2012-11-21 02:44:14 +08:00
|
|
|
state :deleted
|
|
|
|
end
|
|
|
|
|
2012-11-30 05:23:30 +08:00
|
|
|
def account_role?
|
|
|
|
base_role_type == AccountUser::BASE_ROLE_NAME
|
|
|
|
end
|
|
|
|
|
|
|
|
def course_role?
|
|
|
|
!account_role?
|
|
|
|
end
|
|
|
|
|
2012-11-21 02:44:14 +08:00
|
|
|
alias_method :destroy!, :destroy
|
|
|
|
def destroy
|
|
|
|
self.workflow_state = 'deleted'
|
|
|
|
self.deleted_at = Time.now
|
|
|
|
save!
|
|
|
|
end
|
|
|
|
|
2012-11-30 05:23:30 +08:00
|
|
|
named_scope :not_deleted, :conditions => ['roles.workflow_state != ?', 'deleted']
|
|
|
|
named_scope :deleted, :conditions => ['roles.workflow_state = ?', 'deleted']
|
|
|
|
named_scope :active, :conditions => ['roles.workflow_state = ?', 'active']
|
|
|
|
named_scope :inactive, :conditions => ['roles.workflow_state = ?', 'inactive']
|
|
|
|
named_scope :for_courses, :conditions => ['roles.base_role_type != ?', AccountUser::BASE_ROLE_NAME]
|
|
|
|
named_scope :for_accounts, :conditions => ['roles.base_role_type = ?', AccountUser::BASE_ROLE_NAME]
|
2012-11-27 03:27:24 +08:00
|
|
|
|
2012-11-30 05:23:30 +08:00
|
|
|
def self.get_base_role_and_workflow_state(role_name, account)
|
|
|
|
if RoleOverride.base_role_types.include?(role_name)
|
|
|
|
[ role_name, 'active' ]
|
|
|
|
elsif role = account.find_role(role_name)
|
|
|
|
[ role.base_role_type, role.workflow_state ]
|
|
|
|
else
|
2012-12-04 00:05:38 +08:00
|
|
|
[ RoleOverride::NO_PERMISSIONS_TYPE, 'deleted' ]
|
2012-11-30 05:23:30 +08:00
|
|
|
end
|
2012-11-27 03:27:24 +08:00
|
|
|
end
|
2012-12-11 23:41:08 +08:00
|
|
|
|
|
|
|
# Returns a list of hashes for each base enrollment type, and each will have a
|
|
|
|
# custom_roles key, each will look like:
|
|
|
|
# [{:base_role_name => "StudentEnrollment",
|
|
|
|
# :name => "StudentEnrollment",
|
|
|
|
# :label => "Student",
|
|
|
|
# :custom_roles =>
|
|
|
|
# [{:base_role_name => "StudentEnrollment",
|
|
|
|
# :name => "weirdstudent",
|
|
|
|
# :label => "weirdstudent"}]},
|
|
|
|
# ]
|
|
|
|
def self.all_enrollment_roles_for_account(account)
|
|
|
|
custom_roles = account.available_course_roles_by_name.values
|
|
|
|
RoleOverride::ENROLLMENT_TYPES.map do |br|
|
|
|
|
new = br.clone
|
|
|
|
new[:label] = br[:label].call
|
|
|
|
new[:custom_roles] = custom_roles.select{|cr|cr.base_role_type == new[:base_role_name]}.map do |cr|
|
|
|
|
{:base_role_name => cr.base_role_type, :name => cr.name, :label => cr.name}
|
|
|
|
end
|
|
|
|
new
|
|
|
|
end
|
|
|
|
end
|
2012-11-21 02:44:14 +08:00
|
|
|
end
|