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/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: plugin_settings
|
|
|
|
#
|
|
|
|
# id :integer(4) not null, primary key
|
|
|
|
# name :string(255) default(""), not null
|
|
|
|
# settings :text
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
|
|
|
#
|
|
|
|
class PluginSetting < ActiveRecord::Base
|
2011-02-11 04:24:31 +08:00
|
|
|
validates_uniqueness_of :name, :if => :validate_uniqueness_of_name?
|
|
|
|
before_save :validate_posted_settings
|
2011-02-01 09:57:29 +08:00
|
|
|
serialize :settings
|
2011-02-11 04:24:31 +08:00
|
|
|
attr_accessor :posted_settings
|
2011-05-14 00:49:23 +08:00
|
|
|
attr_accessible :name, :settings
|
2011-02-18 08:45:55 +08:00
|
|
|
attr_writer :plugin
|
|
|
|
|
|
|
|
before_save :encrypt_settings
|
2012-10-30 00:32:28 +08:00
|
|
|
after_save :clear_cache
|
|
|
|
after_destroy :clear_cache
|
2014-07-24 01:14:22 +08:00
|
|
|
after_initialize :initialize_plugin_setting
|
2011-02-11 04:24:31 +08:00
|
|
|
|
|
|
|
def validate_uniqueness_of_name?
|
|
|
|
true
|
|
|
|
end
|
2011-02-01 09:57:29 +08:00
|
|
|
|
2011-02-11 04:24:31 +08:00
|
|
|
def validate_posted_settings
|
|
|
|
if @posted_settings
|
|
|
|
plugin = Canvas::Plugin.find(name.to_s)
|
|
|
|
plugin.validate_settings(self, @posted_settings)
|
|
|
|
end
|
|
|
|
end
|
2011-02-18 08:45:55 +08:00
|
|
|
|
|
|
|
def plugin
|
|
|
|
@plugin ||= Canvas::Plugin.find(name.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
# dummy value for encrypted fields so that you can still have something in the form (to indicate
|
|
|
|
# it's set) and be able to tell when it gets blanked out.
|
|
|
|
DUMMY_STRING = "~!?3NCRYPT3D?!~"
|
2014-01-20 23:31:58 +08:00
|
|
|
def initialize_plugin_setting
|
2011-04-07 05:06:32 +08:00
|
|
|
return unless settings && self.plugin
|
|
|
|
@valid_settings = true
|
|
|
|
if self.plugin.encrypted_settings
|
2011-02-18 08:45:55 +08:00
|
|
|
self.plugin.encrypted_settings.each do |key|
|
|
|
|
if settings["#{key}_enc".to_sym]
|
2011-04-07 05:06:32 +08:00
|
|
|
begin
|
|
|
|
settings["#{key}_dec".to_sym] = self.class.decrypt(settings["#{key}_enc".to_sym], settings["#{key}_salt".to_sym])
|
|
|
|
rescue
|
|
|
|
@valid_settings = false
|
|
|
|
end
|
2011-02-18 08:45:55 +08:00
|
|
|
settings[key] = DUMMY_STRING
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-07 05:06:32 +08:00
|
|
|
def valid_settings?
|
|
|
|
@valid_settings
|
|
|
|
end
|
|
|
|
|
2011-02-18 08:45:55 +08:00
|
|
|
def encrypt_settings
|
|
|
|
if settings && self.plugin && self.plugin.encrypted_settings
|
|
|
|
self.plugin.encrypted_settings.each do |key|
|
|
|
|
unless settings[key].blank?
|
|
|
|
value = settings.delete(key)
|
|
|
|
settings.delete("#{key}_dec".to_sym)
|
|
|
|
if value == DUMMY_STRING # no change, use what was there previously
|
|
|
|
settings["#{key}_enc".to_sym] = settings_was["#{key}_enc".to_sym]
|
|
|
|
settings["#{key}_salt".to_sym] = settings_was["#{key}_salt".to_sym]
|
|
|
|
else
|
|
|
|
settings["#{key}_enc".to_sym], settings["#{key}_salt".to_sym] = self.class.encrypt(value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-05-08 11:18:41 +08:00
|
|
|
|
|
|
|
def enabled?
|
|
|
|
read_attribute(:disabled) != true
|
|
|
|
end
|
2013-06-29 03:48:26 +08:00
|
|
|
|
2013-10-12 06:54:51 +08:00
|
|
|
def self.cached_plugin_setting(name)
|
2015-02-03 04:26:27 +08:00
|
|
|
plugin_setting = MultiCache.fetch(settings_cache_key(name)) do
|
2013-10-12 06:54:51 +08:00
|
|
|
PluginSetting.find_by_name(name.to_s) || :nil
|
|
|
|
end
|
|
|
|
plugin_setting = nil if plugin_setting == :nil
|
|
|
|
plugin_setting
|
|
|
|
end
|
2013-10-05 04:28:20 +08:00
|
|
|
|
2013-10-12 06:54:51 +08:00
|
|
|
def self.settings_for_plugin(name, plugin=nil)
|
|
|
|
if (plugin_setting = cached_plugin_setting(name)) && plugin_setting.valid_settings? && plugin_setting.enabled?
|
|
|
|
plugin_setting.plugin = plugin
|
|
|
|
settings = plugin_setting.settings
|
|
|
|
else
|
|
|
|
plugin ||= Canvas::Plugin.find(name.to_s)
|
|
|
|
raise Canvas::NoPluginError unless plugin
|
|
|
|
settings = plugin.default_settings
|
2013-10-11 23:23:56 +08:00
|
|
|
end
|
2013-10-12 06:54:51 +08:00
|
|
|
|
|
|
|
settings
|
2012-08-27 23:07:49 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.settings_cache_key(name)
|
2013-10-12 06:54:51 +08:00
|
|
|
["settings_for_plugin2", name].cache_key
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|
|
|
|
|
2012-10-30 00:32:28 +08:00
|
|
|
def clear_cache
|
2013-09-24 22:49:31 +08:00
|
|
|
connection.after_transaction_commit do
|
2015-02-03 04:26:27 +08:00
|
|
|
MultiCache.delete(PluginSetting.settings_cache_key(self.name))
|
2013-09-24 22:49:31 +08:00
|
|
|
end
|
2012-10-30 00:32:28 +08:00
|
|
|
end
|
|
|
|
|
2011-02-18 08:45:55 +08:00
|
|
|
def self.encrypt(text)
|
|
|
|
Canvas::Security.encrypt_password(text, 'instructure_plugin_setting')
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.decrypt(text, salt)
|
|
|
|
Canvas::Security.decrypt_password(text, salt, 'instructure_plugin_setting')
|
|
|
|
end
|
2014-08-21 06:35:07 +08:00
|
|
|
|
|
|
|
def self.find_by_name(name)
|
|
|
|
where(name: name).first
|
|
|
|
end
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|