Class: SmtpConfiguration

Inherits:
Object
  • Object
show all
Includes:
StandardModel
Defined in:
lib/app/models/smtp_configuration.rb

Overview

SMTP Configuration for an account

Instance Method Summary collapse

Methods included from StandardModel

#clear_cache, included, #remove_blank_secure_fields, #update, #update!

Methods included from App47Logger

#clean_params, #delete_parameter_keys, #log_controller_error, #log_debug, log_debug, log_error, #log_error, log_exception, #log_message, log_message, #log_warn, log_warn, #mask_parameter_keys, #update_flash_messages

Instance Method Details

#build_message ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/app/models/smtp_configuration.rb', line 131

def build_message
  template = File.read(Rails.root.join('app', 'assets', 'templates', 'email', "verify_smtp_configuration.liquid"))
  engine = Liquid::Template.parse(template)
  engine.render({
                  'account_name' => .name,
                  'confirmation_url' => "#{SystemConfiguration.webui_url}/smtp_configuration/confirm_change?token=#{confirmation_token}"
                })
end

#secure_fields ⇒ Object



41
42
43
# File 'lib/app/models/smtp_configuration.rb', line 41

def secure_fields
  super + %i[password]
end

#send_confirmation(member) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/app/models/smtp_configuration.rb', line 84

def send_confirmation(member)
  sent = false
  begin
    mail = Mail.new
    # Set the from line
    mail.from = email_address

    # Set the to address
    mail.to = member.email

    # Set the subject line
    mail.subject = 'Verify SMTP configuration change'

    # set the message body and send
    html_message = build_message
    mail.html_part do
      content_type 'text/html; charset=UTF-8'
      body html_message
    end

    # Setup the delivery method for this message only.
    if Rails.env.test?
      mail.delivery_method :test
    else
      config = { address: self.server_name,
                 port:  self.port,
                 authentication: self.authentication_method.to_sym,
                 enable_starttls_auto: self.ssl.eql?(true) }
      config[:domain] = self.domain unless self.domain.nil? or self.domain.empty?
      config[:user_name] = self.username unless self.username.nil? or self.username.empty?
      config[:password] = self.password unless self.password.nil? or self.password.empty?

      mail.delivery_method :smtp, config
    end

    # Deliver it
    mail.deliver
    sent = true

  rescue Exception=>e
    App47::Logger.log_error "Unable to send SMTP confirmation email #{e.message}"
    App47::Logger.log_error e.backtrace
    set(verification_message: "Unable to send verification email to #{member.email}, error: #{e.message}")
  end
  sent
end

#send_smtp_verification ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/app/models/smtp_configuration.rb', line 70

def send_smtp_verification
  if self.active?
    sent_to= []
    self..smtp_admins.each do |admin|
      sent_to << admin.email if self.send_confirmation(admin)
    end
    if sent_to.empty?
      set(verification_message: "No confirmations emails sent: (#{verification_message}).")
    else
      set(verification_message: "Confirmation emails sent to SMTP admins: #{sent_to.join(", ")}")
    end
  end
end

#to_yaml_properties(wrap = true) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/app/models/smtp_configuration.rb', line 140

def to_yaml_properties(wrap = true)
  if wrap
    self.class.new.to_yaml_properties(false)
  else
    super()
  end
end

#update_token ⇒ Object

Update the token on save if we are active and the token is not already set.



60
61
62
63
64
65
66
67
68
# File 'lib/app/models/smtp_configuration.rb', line 60

def update_token
  if active?
    set(confirmation_token: Devise.friendly_token) if confirmation_token.nil?
    set(confirmed: false)
    set(verification_message: 'Sending SMTP verification email(s) to SMTP admins.')
  else
    set(verification_message: 'SMTP Configuration is not active, no verification email will be sent.')
  end
end

#use? ⇒ Boolean

If we can use this SMTP configuration or not, it must be confirmed and activate. Otherwise we return false.

Returns:

  • (Boolean)


37
38
39
# File 'lib/app/models/smtp_configuration.rb', line 37

def use?
  confirmed? && active?
end

#validate_token(token) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/app/models/smtp_configuration.rb', line 45

def validate_token(token)
  valid = false
  unless confirmation_token.nil?
    if confirmation_token.eql? token
      valid = true
      unset(:confirmation_token)
      unset(:verification_message)
    end
  end
  set(confirmed: valid)
  valid
end