-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathaccount_settings.rb
290 lines (256 loc) · 9.81 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# 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, type: 'boolean', default: false
setting :analytics_reporting, type: 'boolean', default: false
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', default: ENV.fetch('GOOGLE_ANALYTICS_ID', '')
setting :google_analytics_property_id, type: 'string', default: ENV.fetch('GOOGLE_ANALYTICS_PROPERTY_ID', '')
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 :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? ? ENV.fetch(name.upcase.to_s, 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
def reload_library_config
configure_hyrax
reload_hyrax_analytics
configure_devise
configure_carrierwave
configure_ssl
end
def configure_hyrax
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
configure_hyrax_analytics_settings(config)
end
end
def configure_hyrax_analytics_settings(config)
if analytics_credentials_present?
config.analytics = true
config.analytics_reporting = true
else
config.analytics = false
config.analytics_reporting = false
end
end
def configure_devise
Devise.mailer_sender = contact_email
end
def configure_carrierwave
CarrierWave.configure do |config|
if s3_bucket.present?
configure_s3_storage(config)
elsif !file_acl
configure_no_permissions(config)
else
configure_file_storage(config)
end
end
end
def configure_s3_storage(config)
config.storage = :aws
config.aws_bucket = s3_bucket
config.aws_acl = 'bucket-owner-full-control'
end
def configure_no_permissions(config)
config.permissions = nil
config.directory_permissions = nil
end
def configure_file_storage(config)
config.storage = :file
config.permissions = 420
config.directory_permissions = 493
end
def configure_ssl
return unless ssl_configured
ActionMailer::Base.default_url_options ||= {}
ActionMailer::Base.default_url_options[:protocol] = 'https'
end
def analytics_credentials_present?
google_analytics_id.present? &&
google_analytics_property_id.present? &&
(ENV.fetch('GOOGLE_ACCOUNT_JSON', '').present? || ENV.fetch('GOOGLE_ACCOUNT_JSON_PATH', '').present?)
end
def reload_hyrax_analytics
# Configure analytics if all required settings are present
if google_analytics_id.present? &&
google_analytics_property_id.present? &&
(ENV.fetch('GOOGLE_ACCOUNT_JSON', '').present? || ENV.fetch('GOOGLE_ACCOUNT_JSON_PATH', '').present?)
Hyrax::Analytics.config.analytics_id = google_analytics_id
Hyrax::Analytics.config.property_id = google_analytics_property_id
else
# Disable analytics if any required settings are missing
Hyrax.config.analytics = false
Hyrax.config.analytics_reporting = false
end
rescue StandardError => e
# Log the error but don't crash the application
Rails.logger.error "Failed to configure analytics: #{e.message}"
Hyrax.config.analytics = false
Hyrax.config.analytics_reporting = false
end
end
# rubocop:enable Metrics/ModuleLength