2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2012-04-10 19:57:18 +00:00
|
|
|
class GettingStartedController < ApplicationController
|
2020-03-19 09:39:51 +00:00
|
|
|
prepend_before_action -> { authorize! }, only: [:base]
|
2012-04-10 19:57:18 +00:00
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
Resource:
|
2014-11-16 22:45:57 +00:00
|
|
|
GET /api/v1/getting_started
|
2012-09-20 12:08:02 +00:00
|
|
|
|
|
|
|
Response:
|
|
|
|
{
|
|
|
|
"master_user": 1,
|
|
|
|
"groups": [
|
|
|
|
{
|
|
|
|
"name": "group1",
|
|
|
|
"active":true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "group2",
|
|
|
|
"active":true
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
Test:
|
2014-11-16 22:45:57 +00:00
|
|
|
curl http://localhost/api/v1/getting_started -v -u #{login}:#{password}
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
=end
|
|
|
|
|
2012-04-10 19:57:18 +00:00
|
|
|
def index
|
|
|
|
|
|
|
|
# check if first user already exists
|
2014-10-22 21:00:11 +00:00
|
|
|
return if setup_done_response
|
2012-04-10 19:57:18 +00:00
|
|
|
|
2015-04-08 13:55:53 +00:00
|
|
|
# check it auto wizard is already done
|
2015-05-24 21:59:11 +00:00
|
|
|
return if auto_wizard_enabled_response
|
2015-04-08 13:55:53 +00:00
|
|
|
|
2021-08-17 12:10:02 +00:00
|
|
|
# if admin user already exists, we need to be authenticated
|
2020-09-30 09:07:01 +00:00
|
|
|
return if setup_done && !authentication_check
|
2012-09-20 12:08:02 +00:00
|
|
|
|
2012-04-10 19:57:18 +00:00
|
|
|
# return result
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {
|
2018-12-19 17:31:51 +00:00
|
|
|
setup_done: setup_done,
|
|
|
|
import_mode: Setting.get('import_mode'),
|
|
|
|
import_backend: Setting.get('import_backend'),
|
2015-04-27 13:42:53 +00:00
|
|
|
system_online_service: Setting.get('system_online_service'),
|
2014-10-22 21:00:11 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-05-24 21:59:11 +00:00
|
|
|
def auto_wizard_admin
|
|
|
|
|
|
|
|
# check if system setup is already done
|
|
|
|
return if setup_done_response
|
|
|
|
|
|
|
|
# check it auto wizard is enabled
|
|
|
|
if !AutoWizard.enabled?
|
|
|
|
render json: {
|
|
|
|
auto_wizard: false,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# verify auto wizard file
|
|
|
|
auto_wizard_data = AutoWizard.data
|
2017-11-23 08:09:44 +00:00
|
|
|
if auto_wizard_data.blank?
|
2015-05-24 21:59:11 +00:00
|
|
|
render json: {
|
2018-12-19 17:31:51 +00:00
|
|
|
auto_wizard: true,
|
2015-05-24 21:59:11 +00:00
|
|
|
auto_wizard_success: false,
|
2021-11-15 15:58:19 +00:00
|
|
|
message: __('Invalid auto wizard file.'),
|
2015-05-24 21:59:11 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# verify auto wizard token
|
|
|
|
if auto_wizard_data['Token'] && auto_wizard_data['Token'] != params[:token]
|
|
|
|
render json: {
|
2018-12-19 17:31:51 +00:00
|
|
|
auto_wizard: true,
|
2015-05-24 21:59:11 +00:00
|
|
|
auto_wizard_success: false,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# execute auto wizard
|
|
|
|
auto_wizard_admin = AutoWizard.setup
|
|
|
|
if !auto_wizard_admin
|
|
|
|
render json: {
|
2018-12-19 17:31:51 +00:00
|
|
|
auto_wizard: true,
|
2015-05-24 21:59:11 +00:00
|
|
|
auto_wizard_success: false,
|
2021-11-15 15:58:19 +00:00
|
|
|
message: __('Error during execution of auto wizard.'),
|
2015-05-24 21:59:11 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# set current session user
|
|
|
|
current_user_set(auto_wizard_admin)
|
|
|
|
|
|
|
|
# set system init to done
|
|
|
|
Setting.set('system_init_done', true)
|
|
|
|
|
|
|
|
render json: {
|
2018-12-19 17:31:51 +00:00
|
|
|
auto_wizard: true,
|
2015-05-24 21:59:11 +00:00
|
|
|
auto_wizard_success: true,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-11-16 22:45:57 +00:00
|
|
|
def base
|
|
|
|
# validate url
|
|
|
|
messages = {}
|
2017-01-16 01:16:14 +00:00
|
|
|
settings = {}
|
2014-11-20 16:04:31 +00:00
|
|
|
if !Setting.get('system_online_service')
|
2020-12-08 14:12:36 +00:00
|
|
|
if (result = self.class.validate_uri(params[:url]))
|
|
|
|
settings[:http_type] = result[:scheme]
|
|
|
|
settings[:fqdn] = result[:fqdn]
|
|
|
|
else
|
2021-12-15 21:00:53 +00:00
|
|
|
messages[:url] = __('A URL looks like this: https://zammad.example.com')
|
2017-01-16 01:16:14 +00:00
|
|
|
end
|
2014-11-16 22:45:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# validate organization
|
2017-11-23 08:09:44 +00:00
|
|
|
if params[:organization].blank?
|
2014-11-16 22:45:57 +00:00
|
|
|
messages[:organization] = 'Invalid!'
|
2017-01-16 01:16:14 +00:00
|
|
|
else
|
|
|
|
settings[:organization] = params[:organization]
|
2014-11-16 22:45:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# validate image
|
2021-05-12 11:37:44 +00:00
|
|
|
if params[:logo] && params[:logo] =~ %r{^data:image}i
|
2016-05-10 13:06:51 +00:00
|
|
|
file = StaticAssets.data_url_attributes(params[:logo])
|
2014-12-01 07:32:35 +00:00
|
|
|
if !file[:content] || !file[:mime_type]
|
2022-01-19 13:59:52 +00:00
|
|
|
messages[:logo] = __('The uploaded image could not be processed.')
|
2014-11-16 22:45:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-06 15:49:20 +00:00
|
|
|
# add locale_default
|
|
|
|
if params[:locale_default].present?
|
|
|
|
settings[:locale_default] = params[:locale_default]
|
|
|
|
end
|
|
|
|
|
2019-02-10 11:01:38 +00:00
|
|
|
# add timezone_default
|
|
|
|
if params[:timezone_default].present?
|
|
|
|
settings[:timezone_default] = params[:timezone_default]
|
|
|
|
end
|
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
if messages.present?
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {
|
2018-12-19 17:31:51 +00:00
|
|
|
result: 'invalid',
|
2015-04-27 13:42:53 +00:00
|
|
|
messages: messages,
|
2014-10-22 21:00:11 +00:00
|
|
|
}
|
2014-10-22 21:26:00 +00:00
|
|
|
return
|
2014-10-22 21:00:11 +00:00
|
|
|
end
|
|
|
|
|
2014-11-16 22:45:57 +00:00
|
|
|
# save image
|
2021-05-12 11:37:44 +00:00
|
|
|
if params[:logo] && params[:logo] =~ %r{^data:image}i
|
2014-11-16 22:45:57 +00:00
|
|
|
|
|
|
|
# data:image/png;base64
|
2016-05-10 13:06:51 +00:00
|
|
|
file = StaticAssets.data_url_attributes(params[:logo])
|
2014-11-19 22:22:13 +00:00
|
|
|
|
|
|
|
# store image 1:1
|
2016-05-10 13:06:51 +00:00
|
|
|
StaticAssets.store_raw(file[:content], file[:mime_type])
|
2014-10-22 21:26:00 +00:00
|
|
|
end
|
2014-10-22 21:00:11 +00:00
|
|
|
|
2021-05-12 11:37:44 +00:00
|
|
|
if params[:logo_resize] && params[:logo_resize] =~ %r{^data:image}i
|
2014-11-19 22:22:13 +00:00
|
|
|
|
|
|
|
# data:image/png;base64
|
2016-05-10 13:06:51 +00:00
|
|
|
file = StaticAssets.data_url_attributes(params[:logo_resize])
|
2014-11-19 22:22:13 +00:00
|
|
|
|
|
|
|
# store image 1:1
|
2016-05-10 13:06:51 +00:00
|
|
|
settings[:product_logo] = StaticAssets.store(file[:content], file[:mime_type])
|
2014-11-19 22:22:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# set changed settings
|
2017-10-01 12:25:52 +00:00
|
|
|
settings.each do |key, value|
|
2014-11-19 22:22:13 +00:00
|
|
|
Setting.set(key, value)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2014-11-19 22:22:13 +00:00
|
|
|
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {
|
2018-12-19 17:31:51 +00:00
|
|
|
result: 'ok',
|
2015-04-27 13:42:53 +00:00
|
|
|
settings: settings,
|
2014-10-22 21:00:11 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-12-08 14:12:36 +00:00
|
|
|
def self.validate_uri(string)
|
|
|
|
uri = URI(string)
|
|
|
|
|
|
|
|
return false if %w[http https].exclude?(uri.scheme) || uri.host.blank?
|
|
|
|
|
|
|
|
defaults = [['http', 80], ['https', 443]]
|
|
|
|
actual = [uri.scheme, uri.port]
|
|
|
|
|
|
|
|
fqdn = if defaults.include? actual
|
|
|
|
uri.host
|
|
|
|
else
|
|
|
|
"#{uri.host}:#{uri.port}"
|
|
|
|
end
|
|
|
|
|
|
|
|
{ scheme: uri.scheme, fqdn: fqdn }
|
|
|
|
rescue
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2015-08-23 20:21:04 +00:00
|
|
|
private
|
2014-11-16 22:45:57 +00:00
|
|
|
|
2015-05-24 21:59:11 +00:00
|
|
|
def auto_wizard_enabled_response
|
|
|
|
return false if !AutoWizard.enabled?
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
auto_wizard: true
|
|
|
|
}
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2014-10-22 21:00:11 +00:00
|
|
|
def setup_done
|
2021-07-16 13:44:10 +00:00
|
|
|
# return false
|
2014-10-22 21:00:11 +00:00
|
|
|
count = User.all.count()
|
|
|
|
done = true
|
|
|
|
if count <= 2
|
|
|
|
done = false
|
|
|
|
end
|
|
|
|
done
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_done_response
|
2015-05-24 21:59:11 +00:00
|
|
|
return false if !setup_done
|
2014-11-20 16:04:31 +00:00
|
|
|
|
2017-12-06 15:49:20 +00:00
|
|
|
groups = Group.where(active: true)
|
|
|
|
addresses = EmailAddress.where(active: true)
|
2015-04-27 13:42:53 +00:00
|
|
|
|
|
|
|
render json: {
|
2018-12-19 17:31:51 +00:00
|
|
|
setup_done: true,
|
|
|
|
import_mode: Setting.get('import_mode'),
|
|
|
|
import_backend: Setting.get('import_backend'),
|
2015-04-27 13:42:53 +00:00
|
|
|
system_online_service: Setting.get('system_online_service'),
|
2018-12-19 17:31:51 +00:00
|
|
|
addresses: addresses,
|
|
|
|
groups: groups,
|
|
|
|
config: config_to_update,
|
|
|
|
channel_driver: {
|
2015-08-31 23:12:51 +00:00
|
|
|
email: EmailHelper.available_driver,
|
|
|
|
},
|
2012-04-11 06:34:56 +00:00
|
|
|
}
|
2014-10-22 21:00:11 +00:00
|
|
|
true
|
2012-04-10 19:57:18 +00:00
|
|
|
end
|
2014-10-22 21:00:11 +00:00
|
|
|
|
2015-08-04 19:42:49 +00:00
|
|
|
def config_to_update
|
2015-08-04 18:57:11 +00:00
|
|
|
{
|
|
|
|
product_logo: Setting.get('product_logo')
|
|
|
|
}
|
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|