forked from zammad/zammad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_wizard.rb
165 lines (119 loc) · 3.95 KB
/
auto_wizard.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
module AutoWizard
=begin
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)
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
raise "So such file #{auto_wizard_file_location}" if !File.file?(auto_wizard_file_location)
JSON.parse(File.read(auto_wizard_file_location))
end
=begin
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
auto_wizard_file_location = file_location
auto_wizard_hash = data
admin_user = User.find(1)
UserInfo.current_user_id = admin_user.id
# set default calendar
if auto_wizard_hash['CalendarSetup']
if auto_wizard_hash['CalendarSetup']['Ip']
Calendar.init_setup(auto_wizard_hash['CalendarSetup']['Ip'])
end
end
# load text modules
if auto_wizard_hash['TextModuleLocale']
if 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}"
end
end
end
# set Settings
auto_wizard_hash['Settings']&.each do |setting_data|
Setting.set(setting_data['name'], setting_data['value'])
end
# create Permissions/Organization
model_map = {
'Permissions' => 'Permission',
'Organizations' => 'Organization',
}
model_map.each do |map_name, model|
next if !auto_wizard_hash[map_name]
auto_wizard_hash[map_name].each do |data|
generic_object = Kernel.const_get(model)
data.symbolize_keys!
generic_object.create_or_update_with_ref(data)
end
end
# create Users
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
end
end
created_user = User.create_or_update_with_ref(user_data)
# use first created user as admin
next if admin_user.id != 1
admin_user = created_user
UserInfo.current_user_id = admin_user.id
# fetch org logo
if admin_user.email.present?
Service::Image.organization_suggest(admin_user.email)
end
end
# create EmailAddresses/Channels/Signatures
model_map = {
'Channels' => 'Channel',
'EmailAddresses' => 'EmailAddress',
'Signatures' => 'Signature',
'Groups' => 'Group',
}
model_map.each do |map_name, model|
next if !auto_wizard_hash[map_name]
auto_wizard_hash[map_name].each do |data|
generic_object = Kernel.const_get(model)
data.symbolize_keys!
generic_object.create_or_update_with_ref(data)
end
end
# reset primary key sequences
DbHelper.import_post
# remove auto wizard file
FileUtils.rm auto_wizard_file_location
admin_user
end
def self.file_location
auto_wizard_file_name = 'auto_wizard.json'
auto_wizard_file_location = Rails.root.join(auto_wizard_file_name)
auto_wizard_file_location
end
private_class_method :file_location
end