|
| 1 | +class ContextExternalTool < ActiveRecord::Base |
| 2 | + include Workflow |
| 3 | + has_many :content_tags, :as => :content |
| 4 | + belongs_to :context, :polymorphic => true |
| 5 | + attr_accessible :privacy_level, :domain, :url, :shared_secret, :consumer_key, :name, :description, :custom_fields |
| 6 | + validates_presence_of :name |
| 7 | + validates_presence_of :consumer_key |
| 8 | + validates_presence_of :shared_secret |
| 9 | + |
| 10 | + before_save :infer_defaults |
| 11 | + adheres_to_policy |
| 12 | + |
| 13 | + workflow do |
| 14 | + state :anonymous |
| 15 | + state :name_only |
| 16 | + state :public |
| 17 | + state :deleted |
| 18 | + end |
| 19 | + |
| 20 | + set_policy do |
| 21 | + given { |user, session| self.cached_context_grants_right?(user, session, :update) } |
| 22 | + set { can :read and can :update and can :delete } |
| 23 | + end |
| 24 | + |
| 25 | + def settings |
| 26 | + read_attribute(:settings) || write_attribute(:settings, {}) |
| 27 | + end |
| 28 | + |
| 29 | + def readable_state |
| 30 | + workflow_state.titleize |
| 31 | + end |
| 32 | + |
| 33 | + def privacy_level=(val) |
| 34 | + if ['anonymous', 'name_only', 'public'].include?(val) |
| 35 | + self.workflow_state = val |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + def custom_fields=(hash) |
| 40 | + settings[:custom_fields] ||= {} |
| 41 | + hash.each do |key, val| |
| 42 | + settings[:custom_fields][key] = val if key.match(/\Acustom_/) |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + def shared_secret=(val) |
| 47 | + write_attribute(:shared_secret, val) unless val.blank? |
| 48 | + end |
| 49 | + |
| 50 | + def infer_defaults |
| 51 | + url = nil if url.blank? |
| 52 | + domain = nil if domain.blank? |
| 53 | + end |
| 54 | + |
| 55 | + def self.standardize_url(url) |
| 56 | + return "" if url.empty? |
| 57 | + url = "http://" + url unless url.match(/:\/\//) |
| 58 | + res = URI.parse(url).normalize |
| 59 | + res.query = res.query.split(/&/).sort.join('&') if !res.query.blank? |
| 60 | + res.to_s |
| 61 | + end |
| 62 | + |
| 63 | + alias_method :destroy!, :destroy |
| 64 | + def destroy |
| 65 | + self.workflow_state = 'deleted' |
| 66 | + save! |
| 67 | + end |
| 68 | + |
| 69 | + def include_email? |
| 70 | + public? |
| 71 | + end |
| 72 | + |
| 73 | + def include_name? |
| 74 | + name_only? || public? |
| 75 | + end |
| 76 | + |
| 77 | + def precedence |
| 78 | + if domain |
| 79 | + # Somebody tell me if we should be expecting more than |
| 80 | + # 25 dots in a url host... |
| 81 | + 25 - domain.split(/\./).length |
| 82 | + elsif url |
| 83 | + 25 |
| 84 | + else |
| 85 | + 26 |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + def matches_url?(url) |
| 90 | + if !defined?(@standard_url) |
| 91 | + @standard_url = !self.url.blank? && ContextExternalTool.standardize_url(self.url) |
| 92 | + end |
| 93 | + return true if url == @standard_url |
| 94 | + host = URI.parse(url).host rescue nil |
| 95 | + !!(host && ('.' + host).match(/\.#{domain}\z/)) |
| 96 | + end |
| 97 | + |
| 98 | + def self.all_tools_for(context) |
| 99 | + contexts = [] |
| 100 | + tools = [] |
| 101 | + while context |
| 102 | + if context.is_a?(Group) |
| 103 | + contexts << context |
| 104 | + context = context.context || context.account |
| 105 | + elsif context.is_a?(Course) |
| 106 | + contexts << context |
| 107 | + context = context.account |
| 108 | + elsif context.is_a?(Account) |
| 109 | + contexts << context |
| 110 | + context = context.parent_account |
| 111 | + else |
| 112 | + context = nil |
| 113 | + end |
| 114 | + end |
| 115 | + return nil if contexts.empty? |
| 116 | + contexts.each do |context| |
| 117 | + tools += context.context_external_tools.active |
| 118 | + end |
| 119 | + tools.sort_by(&:name) |
| 120 | + end |
| 121 | + |
| 122 | + # Order of precedence: Basic LTI defines precedence as first |
| 123 | + # checking for a match on domain. Subdomains count as a match |
| 124 | + # on less-specific domains, but the most-specific domain will |
| 125 | + # match first. So awesome.bob.example.com matches an |
| 126 | + # external_tool with example.com as the domain, but only if |
| 127 | + # there isn't another external_tool where awesome.bob.example.com |
| 128 | + # or bob.example.com is set as the domain. |
| 129 | + # |
| 130 | + # If there is no domain match then check for an exact url match |
| 131 | + # as configured by an admin. If there is still no match |
| 132 | + # then check for a match on the current context (configured by |
| 133 | + # the teacher). |
| 134 | + def self.find_external_tool(url, context) |
| 135 | + url = ContextExternalTool.standardize_url(url) |
| 136 | + account_contexts = [] |
| 137 | + other_contexts = [] |
| 138 | + while context |
| 139 | + if context.is_a?(Group) |
| 140 | + other_contexts << context |
| 141 | + context = context.context || context.account |
| 142 | + elsif context.is_a?(Course) |
| 143 | + other_contexts << context |
| 144 | + context = context.account |
| 145 | + elsif context.is_a?(Account) |
| 146 | + account_contexts << context |
| 147 | + context = context.parent_account |
| 148 | + else |
| 149 | + context = nil |
| 150 | + end |
| 151 | + end |
| 152 | + return nil if account_contexts.empty? && other_contexts.empty? |
| 153 | + account_contexts.each do |context| |
| 154 | + res = context.context_external_tools.active.sort_by(&:precedence).detect{|tool| tool.domain && tool.matches_url?(url) } |
| 155 | + return res if res |
| 156 | + end |
| 157 | + account_contexts.each do |context| |
| 158 | + res = context.context_external_tools.active.sort_by(&:precedence).detect{|tool| tool.matches_url?(url) } |
| 159 | + return res if res |
| 160 | + end |
| 161 | + other_contexts.reverse.each do |context| |
| 162 | + res = context.context_external_tools.active.sort_by(&:precedence).detect{|tool| tool.matches_url?(url) } |
| 163 | + return res if res |
| 164 | + end |
| 165 | + nil |
| 166 | + end |
| 167 | + |
| 168 | + named_scope :active, :conditions => ['context_external_tools.workflow_state != ?', 'deleted'] |
| 169 | + |
| 170 | + def self.serialization_excludes; [:shared_secret,:settings]; end |
| 171 | +end |
0 commit comments