2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2015-04-08 14:12:19 +00:00
|
|
|
module AutoWizard
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2015-05-24 21:59:11 +00:00
|
|
|
check if auto wizard is enabled
|
|
|
|
|
|
|
|
AutoWizard.enabled?
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
true | false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.enabled?
|
|
|
|
auto_wizard_file_location = file_location
|
|
|
|
return false if !File.file?(auto_wizard_file_location)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-05-24 21:59:11 +00:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
get auto wizard data
|
|
|
|
|
|
|
|
AutoWizard.data
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
content of auto wizard file as object
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.data
|
|
|
|
auto_wizard_file_location = file_location
|
2016-03-01 14:26:46 +00:00
|
|
|
raise "So such file #{auto_wizard_file_location}" if !File.file?(auto_wizard_file_location)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-06-18 14:54:26 +00:00
|
|
|
JSON.parse(File.read(auto_wizard_file_location))
|
2015-05-24 21:59:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2015-04-08 14:12:19 +00:00
|
|
|
creates or updates Users, EmailAddresses and sets Settings based on the 'auto_wizard.json' file placed in the root directory.
|
|
|
|
|
|
|
|
there is an example file 'contrib/auto_wizard_example.json'
|
|
|
|
|
|
|
|
AutoWizard.setup
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
the first created User if a 'auto_wizard.json' file was found and processed, containing at least one entry in Users
|
|
|
|
|
|
|
|
the User with id 1 (NULL) if a 'auto_wizard.json' file was found and processed, containing no Users
|
|
|
|
|
|
|
|
nil if no 'auto_wizard.json' file was found
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.setup
|
2015-05-24 21:59:11 +00:00
|
|
|
auto_wizard_file_location = file_location
|
2015-04-08 14:12:19 +00:00
|
|
|
|
2015-05-24 21:59:11 +00:00
|
|
|
auto_wizard_hash = data
|
2015-04-08 14:12:19 +00:00
|
|
|
|
2015-05-24 21:59:11 +00:00
|
|
|
admin_user = User.find(1)
|
2015-04-08 14:12:19 +00:00
|
|
|
|
2015-07-06 21:19:00 +00:00
|
|
|
UserInfo.current_user_id = admin_user.id
|
|
|
|
|
2015-09-28 00:11:44 +00:00
|
|
|
# set default calendar
|
2020-09-30 09:07:01 +00:00
|
|
|
if auto_wizard_hash['CalendarSetup'] && auto_wizard_hash['CalendarSetup']['Ip']
|
|
|
|
Calendar.init_setup(auto_wizard_hash['CalendarSetup']['Ip'])
|
2015-09-28 00:11:44 +00:00
|
|
|
end
|
|
|
|
|
2016-07-25 20:13:38 +00:00
|
|
|
# load text modules
|
2020-09-30 09:07:01 +00:00
|
|
|
if auto_wizard_hash['TextModuleLocale'] && auto_wizard_hash['TextModuleLocale']['Locale']
|
|
|
|
begin
|
|
|
|
TextModule.load(auto_wizard_hash['TextModuleLocale']['Locale'])
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.error "Unable to load text modules #{auto_wizard_hash['TextModuleLocale']['Locale']}: #{e.message}"
|
2016-07-25 20:13:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-22 23:30:21 +00:00
|
|
|
# set Settings
|
2017-11-23 08:09:44 +00:00
|
|
|
auto_wizard_hash['Settings']&.each do |setting_data|
|
|
|
|
Setting.set(setting_data['name'], setting_data['value'])
|
2015-05-22 23:30:21 +00:00
|
|
|
end
|
|
|
|
|
2017-10-23 14:33:16 +00:00
|
|
|
# create Permissions/Organization
|
2016-06-18 14:54:26 +00:00
|
|
|
model_map = {
|
2018-12-19 17:31:51 +00:00
|
|
|
'Permissions' => 'Permission',
|
2016-06-18 14:54:26 +00:00
|
|
|
'Organizations' => 'Organization',
|
|
|
|
}
|
2017-10-01 12:25:52 +00:00
|
|
|
model_map.each do |map_name, model|
|
2016-06-18 14:54:26 +00:00
|
|
|
next if !auto_wizard_hash[map_name]
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-10-01 12:25:52 +00:00
|
|
|
auto_wizard_hash[map_name].each do |data|
|
2016-06-18 14:54:26 +00:00
|
|
|
data.symbolize_keys!
|
2019-01-06 18:41:29 +00:00
|
|
|
model.constantize.create_or_update_with_ref(data)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
|
|
|
end
|
2015-05-22 23:30:21 +00:00
|
|
|
|
2015-04-08 14:12:19 +00:00
|
|
|
# create Users
|
2017-11-23 08:09:44 +00:00
|
|
|
auto_wizard_hash['Users']&.each do |user_data|
|
|
|
|
user_data.symbolize_keys!
|
|
|
|
|
|
|
|
if admin_user.id == 1
|
|
|
|
if !user_data[:roles] && !user_data[:role_ids]
|
|
|
|
user_data[:roles] = Role.where(name: %w[Agent Admin])
|
|
|
|
end
|
|
|
|
if !user_data[:groups] && !user_data[:group_ids]
|
|
|
|
user_data[:groups] = Group.all
|
2015-05-22 23:54:33 +00:00
|
|
|
end
|
2017-11-23 08:09:44 +00:00
|
|
|
end
|
2015-04-08 14:12:19 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
created_user = User.create_or_update_with_ref(user_data)
|
2015-04-08 14:12:19 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
# use first created user as admin
|
|
|
|
next if admin_user.id != 1
|
2015-04-08 14:12:19 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
admin_user = created_user
|
|
|
|
UserInfo.current_user_id = admin_user.id
|
2015-07-07 05:56:31 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
# fetch org logo
|
|
|
|
if admin_user.email.present?
|
|
|
|
Service::Image.organization_suggest(admin_user.email)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-04-08 14:12:19 +00:00
|
|
|
end
|
|
|
|
|
2015-05-23 01:53:43 +00:00
|
|
|
# create EmailAddresses/Channels/Signatures
|
|
|
|
model_map = {
|
|
|
|
'Channels' => 'Channel',
|
2015-08-28 08:19:27 +00:00
|
|
|
'EmailAddresses' => 'EmailAddress',
|
2015-05-23 01:53:43 +00:00
|
|
|
'Signatures' => 'Signature',
|
2015-12-17 11:49:40 +00:00
|
|
|
'Groups' => 'Group',
|
2015-05-23 01:53:43 +00:00
|
|
|
}
|
2017-10-01 12:25:52 +00:00
|
|
|
model_map.each do |map_name, model|
|
2015-05-23 01:53:43 +00:00
|
|
|
next if !auto_wizard_hash[map_name]
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-10-01 12:25:52 +00:00
|
|
|
auto_wizard_hash[map_name].each do |data|
|
2016-06-18 14:54:26 +00:00
|
|
|
data.symbolize_keys!
|
2019-01-06 18:41:29 +00:00
|
|
|
model.constantize.create_or_update_with_ref(data)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
|
|
|
end
|
2015-05-23 00:41:29 +00:00
|
|
|
|
2016-11-25 14:54:13 +00:00
|
|
|
# reset primary key sequences
|
|
|
|
DbHelper.import_post
|
|
|
|
|
2015-05-23 00:41:29 +00:00
|
|
|
# remove auto wizard file
|
|
|
|
FileUtils.rm auto_wizard_file_location
|
|
|
|
|
2015-04-08 14:12:19 +00:00
|
|
|
admin_user
|
|
|
|
end
|
2015-05-24 21:59:11 +00:00
|
|
|
|
|
|
|
def self.file_location
|
2020-07-07 06:30:20 +00:00
|
|
|
auto_wizard_file_name = 'auto_wizard.json'
|
|
|
|
Rails.root.join(auto_wizard_file_name)
|
|
|
|
|
2015-05-24 21:59:11 +00:00
|
|
|
end
|
2016-01-15 17:22:57 +00:00
|
|
|
private_class_method :file_location
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|