Files
docuseal/lib/accounts.rb
T

125 lines
3.5 KiB
Ruby
Raw Normal View History

2023-06-30 01:26:13 +03:00
# frozen_string_literal: true
module Accounts
module_function
2023-07-12 01:24:25 +03:00
def create_duplicate(account)
new_account = account.dup
new_user = account.users.first.dup
2023-10-14 21:19:21 +03:00
new_user.uuid = SecureRandom.uuid
2023-07-12 01:24:25 +03:00
new_user.account = new_account
new_user.encrypted_password = SecureRandom.hex
new_user.email = "#{SecureRandom.hex}@docuseal.co"
account.templates.each do |template|
new_template = template.dup
new_template.account = new_account
2023-07-14 22:33:42 +03:00
new_template.slug = SecureRandom.base58(14)
2023-07-12 01:24:25 +03:00
2023-12-30 00:37:03 +02:00
new_template.archived_at = nil
2023-07-12 01:24:25 +03:00
new_template.save!
Templates::CloneAttachments.call(template: new_template, original_template: template)
end
new_user.save!(validate: false)
2023-10-14 21:19:21 +03:00
new_account.templates.update_all(folder_id: new_account.default_template_folder.id)
2023-07-12 01:24:25 +03:00
new_account
end
2024-01-20 23:53:14 +02:00
def find_or_create_testing_user(account)
user = User.find_by(account: account.testing_accounts)
return user if user
testing_account = account.dup.tap { |a| a.name = "Testing - #{a.name}" }
ApplicationRecord.transaction do
account.testing_accounts << testing_account
testing_account.users.create!(
email: account.users.order(:id).first.email.sub('@', '+test@'),
first_name: 'Testing',
last_name: 'Environment',
password: SecureRandom.hex,
role: :admin
)
end
end
2023-07-14 22:33:42 +03:00
def create_default_template(account)
template = Template.find(1)
new_template = Template.find(1).dup
new_template.account_id = account.id
new_template.slug = SecureRandom.base58(14)
2023-09-22 02:20:38 +03:00
new_template.folder = account.default_template_folder
2023-07-14 22:33:42 +03:00
new_template.save!
Templates::CloneAttachments.call(template: new_template, original_template: template)
new_template
end
2023-10-08 12:04:32 +03:00
def load_webhook_configs(account)
2023-11-01 13:11:42 +02:00
configs = account.encrypted_configs.find_by(key: EncryptedConfig::WEBHOOK_URL_KEY)
2023-10-08 12:04:32 +03:00
2023-11-01 13:11:42 +02:00
unless Docuseal.multitenant?
configs ||= Account.order(:id).first.encrypted_configs.find_by(key: EncryptedConfig::WEBHOOK_URL_KEY)
end
configs
2023-10-08 12:04:32 +03:00
end
2023-08-31 01:12:24 +03:00
def load_signing_pkcs(account)
cert_data =
2023-06-30 20:31:17 +03:00
if Docuseal.multitenant?
2023-10-08 12:04:32 +03:00
EncryptedConfig.find_by(account:, key: EncryptedConfig::ESIGN_CERTS_KEY)&.value || Docuseal::CERTS
2023-06-30 20:31:17 +03:00
else
2024-01-20 23:53:14 +02:00
EncryptedConfig.find_by(account:, key: EncryptedConfig::ESIGN_CERTS_KEY)&.value ||
EncryptedConfig.find_by(key: EncryptedConfig::ESIGN_CERTS_KEY).value
2023-06-30 20:31:17 +03:00
end
2023-08-31 01:12:24 +03:00
if (default_cert = cert_data['custom']&.find { |e| e['status'] == 'default' })
2023-09-01 01:27:13 +03:00
OpenSSL::PKCS12.new(Base64.urlsafe_decode64(default_cert['data']), default_cert['password'].to_s)
2023-08-31 01:12:24 +03:00
else
GenerateCertificate.load_pkcs(cert_data)
end
2023-06-30 20:31:17 +03:00
end
2023-12-02 18:14:33 +02:00
def load_timeserver_url(account)
if Docuseal.multitenant?
Docuseal::TIMESERVER_URL
else
2023-12-04 12:47:19 +02:00
url = EncryptedConfig.find_by(account:, key: EncryptedConfig::TIMESTAMP_SERVER_URL_KEY)&.value
unless Docuseal.multitenant?
url ||=
Account.order(:id).first.encrypted_configs.find_by(key: EncryptedConfig::TIMESTAMP_SERVER_URL_KEY)&.value
end
url
2023-12-02 18:14:33 +02:00
end.presence
end
2023-10-28 00:42:17 +03:00
def can_send_emails?(_account)
2023-06-30 20:31:17 +03:00
return true if Docuseal.multitenant?
return true if ENV['SMTP_ADDRESS'].present?
2023-06-30 20:31:17 +03:00
2023-10-28 00:42:17 +03:00
EncryptedConfig.exists?(key: EncryptedConfig::EMAIL_SMTP_KEY)
2023-06-30 20:31:17 +03:00
end
2023-06-30 01:26:13 +03:00
def normalize_timezone(timezone)
tzinfo = TZInfo::Timezone.get(ActiveSupport::TimeZone::MAPPING[timezone] || timezone)
::ActiveSupport::TimeZone.all.find { |e| e.tzinfo == tzinfo }&.name || timezone
2023-09-21 10:36:21 +03:00
rescue TZInfo::InvalidTimezoneIdentifier
'UTC'
2023-06-30 01:26:13 +03:00
end
end