Class: Spree::ApiKeyMailer

Inherits:
BaseMailer
  • Object
show all
Defined in:
app/mailers/spree/api_key_mailer.rb

Overview

Security notices about a store's API keys, sent to its admins.

Constant Summary collapse

IP_ADDRESS =
'created_by_ip_address'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseMailer

#current_store, #from_address, #mail, #money, #render_in_email_layout, #reply_to_address, #with_store_locale

Class Method Details

.decrypt_ip_address(value) ⇒ Object



46
47
48
49
50
# File 'app/mailers/spree/api_key_mailer.rb', line 46

def self.decrypt_ip_address(value)
  return value if value.blank? || !ActiveRecord::Encryption.config.has_primary_key?

  ActiveRecord::Encryption.encryptor.decrypt(value)
end

.encrypt_ip_address(value) ⇒ Object

Encrypted the same way as Spree::ApiKey#created_by_ip_address: only when Active Record encryption keys are configured.



40
41
42
43
44
# File 'app/mailers/spree/api_key_mailer.rb', line 40

def self.encrypt_ip_address(value)
  return value if value.blank? || !ActiveRecord::Encryption.config.has_primary_key?

  ActiveRecord::Encryption.encryptor.encrypt(value)
end

.notice_for(api_key) ⇒ Hash

The key as the email shows it, taken when the key is created. The notice is sent from this copy rather than the record, so deleting the key before the job runs cannot suppress it. The IP stays encrypted while the copy waits in the job queue.

Parameters:

Returns:

  • (Hash)


13
14
15
16
# File 'app/mailers/spree/api_key_mailer.rb', line 13

def self.notice_for(api_key)
  notice = Spree::Emails::TemplateData.call(api_key, Spree::Emails::ApiKeySerializer, store: api_key.store)
  notice.merge(IP_ADDRESS => encrypt_ip_address(notice[IP_ADDRESS]))
end

Instance Method Details

#secret_key_created(store, notice, admin_user) ⇒ Object

Tells one store admin that a secret (Admin API) key was created.

Parameters:

  • store (Spree::Store) —

    the key's store

  • notice (Hash) —
  • admin_user (Object) —

    the admin to notify



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/mailers/spree/api_key_mailer.rb', line 23

def secret_key_created(store, notice, admin_user)
  @current_store = store

  with_store_locale(store, staff_locale(admin_user, store)) do
    mail_template(
      {
        api_key: notice.merge(IP_ADDRESS => self.class.decrypt_ip_address(notice[IP_ADDRESS])),
        user: email_data(admin_user, Spree::Emails::UserSerializer),
        review_url: "#{Spree::Stores::DashboardUrl.call(store: store)}/#{store.prefixed_id}/settings/api-keys"
      },
      to: admin_user.email, store_url: store.formatted_url
    )
  end
end