Project

General

Profile

Download (4.29 KB) Statistics
| Branch: | Tag: | Revision:
require 'facter'

class Setting::Email < Setting
NON_EMAIL_YAML_SETTINGS = %w(send_welcome_email email_reply_address email_subject_prefix)

def self.default_settings
domain = Facter.value(:domain) || SETTINGS[:domain]
email_reply_address = "Foreman-noreply@#{domain}"

[
self.set('email_reply_address', N_("Email reply address for emails that Foreman is sending"), email_reply_address, N_('Email reply address')),
self.set('email_subject_prefix', N_("Prefix to add to all outgoing email"), '[foreman]', N_('Email subject prefix')),
self.set('send_welcome_email', N_("Send a welcome email including username and URL to new users"), false, N_('Send welcome email')),
self.set('delivery_method', N_("Method used to deliver email"), 'sendmail', N_('Delivery method'), nil, { :collection => Proc.new {{:sendmail => _("Sendmail"), :smtp => _("SMTP")}}}),
self.set('smtp_enable_starttls_auto', N_("SMTP automatic STARTTLS"), true, N_('SMTP enable StartTLS auto')),
self.set('smtp_openssl_verify_mode', N_("When using TLS, you can set how OpenSSL checks the certificate"), '', N_('SMTP OpenSSL verify mode'), nil, { :collection => Proc.new {{'' => _("Default verification mode"), :none => _("none"), :peer => "peer", :client_once => "client_once", :fail_if_no_peer_cert => "fail_if_no_peer_cert"}}}),
self.set('smtp_address', N_("Address to connect to"), '', N_('SMTP address')),
self.set('smtp_port', N_("Port to connect to"), 25, N_('SMTP port')),
self.set('smtp_domain', N_("HELO/EHLO domain"), '', N_('SMTP HELO/EHLO domain')),
self.set('smtp_user_name', N_("Username to use to authenticate, if required"), '', N_('SMTP username')),
self.set('smtp_password', N_("Password to use to authenticate, if required"), '', N_('SMTP password'), nil, {:encrypted => true}),
self.set('smtp_authentication', N_("Specify authentication type, if required"), '', N_('SMTP authentication'), nil, { :collection => Proc.new {{:plain => "plain", :login => "login", :cram_md5 => "cram_md5", '' => _("none")}}}),
self.set('sendmail_arguments', N_("Specify additional options to sendmail"), '-i', N_('Sendmail arguments')),
self.set('sendmail_location', N_("The location of the sendmail executable"), "/usr/sbin/sendmail", N_('Sendmail location'))
]
end

def self.load_defaults
Foreman::Deprecation.deprecation_warning('1.16', "'email.yaml' file configuration has been deprecated, email configuration has been moved to Foreman settings") if mailconfig.present?
# Check the table exists
return unless super

self.transaction do
default_settings.each { |s| self.create! s.update(:category => "Setting::Email")}
end

true
end

def self.config_file
File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'config', 'email.yaml'))
end

def self.delivery_settings
options = {}
self.all.each do |setting|
extracted = {:smtp => extract_prefix(setting.name, 'smtp'), :sendmail => extract_prefix(setting.name, 'sendmail')}
["smtp", "sendmail"].each do |method|
if Setting[:delivery_method].to_s == method && setting.name.start_with?(method) && setting.value.to_s.present?
options[extracted[method.to_sym]] = setting.value
end
end
end
options
end

# Load the configuration from email.yaml, if it exists. Stored in class instance variable to prevent multiple reads.
def self.mailconfig
@mailconfig ||= File.file?(config_file) ? YAML.load_file(config_file).with_indifferent_access : {}
@mailconfig[Rails.env] if @mailconfig.is_a?(Hash)
end

def self.lookup_value(name)
extract_smtp = extract_prefix(name, 'smtp')
extract_sendmail = extract_prefix(name, 'sendmail')
if name.to_s.start_with?('smtp_') && mailconfig["smtp_settings"].is_a?(Hash)
mailconfig["smtp_settings"][extract_smtp]
elsif name.to_s.start_with?('sendmail_') && mailconfig["sendmail_settings"].is_a?(Hash)
mailconfig["sendmail_settings"][extract_sendmail]
else
mailconfig[name.to_s]
end
end

def self.readonly_value(name)
return if mailconfig.blank?
lookup_value(name)
end

def has_readonly_value?
self.class.mailconfig.present? && NON_EMAIL_YAML_SETTINGS.exclude?(name)
end

def self.extract_prefix(name, prefix)
name.to_s.gsub(/^#{prefix}_/, '')
end
end
(2-2/5)