module CanvasSync::Concerns module Role module Base extend ActiveSupport::Concern DEFAULT_ROLE_LABELS = %w[TeacherEnrollment TaEnrollment StudentEnrollment DesignerEnrollment ObserverEnrollment].freeze CanvasSync::Record.define_feature self, default: true class_methods do def for_labels(labels, account) built_ins = [] labels = labels.split(',') if labels.is_a?(String) custom_labels = Array(labels).reject do |l| if DEFAULT_ROLE_LABELS.include?(l) built_ins << l elsif l == 'Account Admin' || l == 'AccountAdmin' built_ins << 'AccountMembership' else next end true end account_ids = Rails.cache.fetch([self.class.name, "AccountAncestry", account.canvas_id], expires_in: 6.hours) do if account.respond_to?(:path_ids) account.path.pluck(:canvas_id) else [].tap do |pids| acc = account loop do break unless acc pids.unshift(acc.canvas_id) acc = acc.canvas_parent end end end end where(workflow_state: 'built_in', base_role_type: built_ins) .or(where.not(workflow_state: 'built_in').where(label: custom_labels, canvas_account_id: account_ids)) end def joined_permissions(roles) final = {} roles.each do |role| role.permissions.each do |perm_name, perm| final[perm_name] = false if final[perm_name].nil? final[perm_name] = true if perm['enabled'] == true end end final end end end end end