-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathaccount_settings.rb
230 lines (205 loc) · 7.96 KB
/
account_settings.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# frozen_string_literal: true
# All settings have a presedence order as follows
# Per Tenant Setting > ENV['HYKU_SETTING_NAME'] > ENV['HYRAX_SETTING_NAME'] > default
# rubocop:disable Metrics/ModuleLength
module AccountSettings
extend ActiveSupport::Concern
# rubocop:disable Metrics/BlockLength
included do
cattr_accessor :array_settings, :boolean_settings, :hash_settings, :json_editor_settings, :string_settings, :private_settings do
[]
end
cattr_accessor :all_settings do
{}
end
##
# Consider the configured superadmin_settings only available for those with
# the superadmin role.
class_attribute :superadmin_settings, default: []
setting :allow_downloads, type: 'boolean', default: true
setting :allow_signup, type: 'boolean', default: true
setting :analytics_provider, type: 'string'
setting :batch_email_notifications, type: 'boolean', default: false
setting :bulkrax_field_mappings, type: 'json_editor', default: Hyku.default_bulkrax_field_mappings.to_json
setting :bulkrax_validations, type: 'boolean', disabled: true
setting :cache_api, type: 'boolean', default: false
setting :contact_email, type: 'string', default: '[email protected]'
setting :contact_email_to, type: 'string', default: '[email protected]'
setting :depositor_email_notifications, type: 'boolean', default: false
setting :doi_reader, type: 'boolean', default: false
setting :doi_writer, type: 'boolean', default: false
setting :file_acl, type: 'boolean', default: true, private: true
setting :email_domain, type: 'string', default: 'example.com'
setting :email_format, type: 'array'
setting :email_subject_prefix, type: 'string'
setting :enable_oai_metadata, type: 'string', disabled: true
setting :file_size_limit, type: 'string', default: 5.gigabytes.to_s
setting :google_analytics_id, type: 'string'
setting :google_scholarly_work_types, type: 'array', disabled: true
setting :geonames_username, type: 'string', default: ''
setting :gtm_id, type: 'string'
setting :locale_name, type: 'string', disabled: true
setting :monthly_email_list, type: 'array', disabled: true
setting :oai_admin_email, type: 'string', default: '[email protected]'
setting :oai_prefix, type: 'string', default: 'oai:hyku'
setting :oai_sample_identifier, type: 'string', default: '806bbc5e-8ebe-468c-a188-b7c14fbe34df'
setting :s3_bucket, type: 'string', private: true
setting :shared_login, type: 'boolean', disabled: true
setting :smtp_settings, type: 'hash', private: true, default: {}
setting :solr_collection_options, type: 'hash', default: solr_collection_options
setting :ssl_configured, type: 'boolean', default: true, private: true
setting :user_analytics, type: 'boolean', default: false
setting :weekly_email_list, type: 'array', disabled: true
setting :yearly_email_list, type: 'array', disabled: true
store :settings, coder: JSON, accessors: all_settings.keys
validates :gtm_id, format: { with: /GTM-[A-Z0-9]{4,7}/, message: "Invalid GTM ID" }, allow_blank: true
validates :contact_email, :oai_admin_email,
format: { with: URI::MailTo::EMAIL_REGEXP },
allow_blank: true
validate :validate_email_format, :validate_contact_emails, :validate_json
validates :google_analytics_id,
format: { with: /((UA|YT|MO)-\d+-\d+|G-[A-Z0-9]{10})/i },
allow_blank: true
after_initialize :initialize_settings
end
# rubocop:enable Metrics/BlockLength
# rubocop:disable Metrics/BlockLength
class_methods do
def setting(name, args)
known_type = ['array', 'boolean', 'hash', 'string', 'json_editor'].include?(args[:type])
raise "Setting type #{args[:type]} is not supported. Can not laod." unless known_type
send("#{args[:type]}_settings") << name
all_settings[name] = args
private_settings << name if args[:private]
# watch out because false is a valid value to return here
define_method(name) do
value = super()
value = value.nil? ? ENV.fetch("HYKU_#{name.upcase}", nil) : value
value = value.nil? ? ENV.fetch("HYRAX_#{name.upcase}", nil) : value
value = value.nil? ? args[:default] : value
set_type(value, (args[:type]).to_s)
end
end
# rubocop:disable Metrics/MethodLength
def solr_collection_options
{
async: nil,
auto_add_replicas: nil,
collection: {
config_name: ENV.fetch('SOLR_CONFIGSET_NAME', 'hyku')
},
create_node_set: nil,
max_shards_per_node: nil,
num_shards: 1,
replication_factor: nil,
router: {
name: nil,
field: nil
},
rule: nil,
shards: nil,
snitch: nil
}
end
# rubocop:disable Metrics/MethodLength
end
# rubocop:enable Metrics/BlockLength
def public_settings(is_superadmin: false)
all_settings.reject do |key, value|
value[:disabled] ||
self.class.private_settings.include?(key.to_s) ||
(!is_superadmin && superadmin_settings.include?(key.to_sym))
end
end
def live_settings
all_settings.reject { |_k, v| v[:disabled] }
end
private
def set_type(value, to_type)
case to_type
when 'array'
value.is_a?(String) ? value.split(',') : Array.wrap(value)
when 'boolean'
ActiveModel::Type::Boolean.new.cast(value)
when 'hash'
value.is_a?(String) ? JSON.parse(value) : value
when 'string'
value.to_s
when 'json_editor'
begin
JSON.pretty_generate(JSON.parse(value))
rescue JSON::ParserError
value
end
end
end
def validate_email_format
return if settings['email_format'].blank?
settings['email_format'].each do |email|
errors.add(:email_format) unless email.match?(/@\S*\.\S*/)
end
end
def validate_contact_emails
['weekly_email_list', 'monthly_email_list', 'yearly_email_list'].each do |key|
next if settings[key].blank?
settings[key].each do |email|
errors.add(:"#{key}") unless email.match?(URI::MailTo::EMAIL_REGEXP)
end
end
end
def validate_json
json_editor_settings.each do |key|
next if settings[key].blank?
begin
JSON.parse(settings[key])
rescue JSON::ParserError => e
errors.add(:"#{key}", e.message)
end
end
end
def initialize_settings
return true unless self.class.column_names.include?('settings')
set_smtp_settings
reload_library_config
end
def set_smtp_settings
current_smtp_settings = settings&.[]("smtp_settings").presence || {}
self.smtp_settings = current_smtp_settings.with_indifferent_access.reverse_merge!(
PerTenantSmtpInterceptor.available_smtp_fields.each_with_object("").to_h
)
end
# rubocop:disable Metrics/AbcSize
def reload_library_config
Hyrax.config do |config|
# A short-circuit of showing download links
config.display_media_download_link = allow_downloads.nil? || ActiveModel::Type::Boolean.new.cast(allow_downloads)
config.contact_email = contact_email
config.geonames_username = geonames_username
config.uploader[:maxFileSize] = file_size_limit.to_i
end
Devise.mailer_sender = contact_email
if s3_bucket.present?
CarrierWave.configure do |config|
config.storage = :aws
config.aws_bucket = s3_bucket
config.aws_acl = 'bucket-owner-full-control'
end
elsif !file_acl
CarrierWave.configure do |config|
config.permissions = nil
config.directory_permissions = nil
end
else
CarrierWave.configure do |config|
config.storage = :file
config.permissions = 420
config.directory_permissions = 493
end
end
return unless ssl_configured
ActionMailer::Base.default_url_options ||= {}
ActionMailer::Base.default_url_options[:protocol] = 'https'
end
# rubocop:enable Metrics/AbcSize
end
# rubocop:enable Metrics/ModuleLength