trabajo-afectivo/app/controllers/getting_started_controller.rb

243 lines
5.4 KiB
Ruby
Raw Normal View History

2016-10-19 03:11:36 +00:00
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
2012-04-10 19:57:18 +00:00
class GettingStartedController < ApplicationController
=begin
Resource:
2014-11-16 22:45:57 +00:00
GET /api/v1/getting_started
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}
=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
# if master user already exists, we need to be authenticated
2014-10-22 21:00:11 +00:00
if setup_done
return if !authentication_check
end
2012-04-10 19:57:18 +00:00
# return result
render json: {
setup_done: setup_done,
import_mode: Setting.get('import_mode'),
import_backend: Setting.get('import_backend'),
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: {
auto_wizard: true,
2015-05-24 21:59:11 +00:00
auto_wizard_success: false,
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: {
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: {
auto_wizard: true,
2015-05-24 21:59:11 +00:00
auto_wizard_success: false,
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: {
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
# check admin permissions
permission_check('admin.wizard')
2014-10-22 21:00:11 +00:00
2014-11-16 22:45:57 +00:00
# validate url
messages = {}
settings = {}
if !Setting.get('system_online_service')
if !params[:url] || params[:url] !~ %r{^(http|https)://.+?$}
messages[:url] = 'A URL looks like http://zammad.example.com'
end
# split url in http_type and fqdn
if params[:url]
if params[:url] =~ %r{^(http|https)://(.+?)(:.+?|/.+?|)$}
settings[:http_type] = $1
settings[:fqdn] = $2
else
messages[:url] = 'A URL looks like http://zammad.example.com'
end
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!'
else
settings[:organization] = params[:organization]
2014-11-16 22:45:57 +00:00
end
# validate image
2014-11-19 22:45:37 +00:00
if params[:logo] && params[:logo] =~ /^data:image/i
file = StaticAssets.data_url_attributes(params[:logo])
2014-12-01 07:32:35 +00:00
if !file[:content] || !file[:mime_type]
2014-11-16 22:45:57 +00:00
messages[:logo] = 'Unable to process image upload.'
end
end
# add locale_default
if params[:locale_default].present?
settings[:locale_default] = params[:locale_default]
end
# 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?
render json: {
result: 'invalid',
messages: messages,
2014-10-22 21:00:11 +00:00
}
return
2014-10-22 21:00:11 +00:00
end
2014-11-16 22:45:57 +00:00
# save image
2014-11-19 22:45:37 +00:00
if params[:logo] && params[:logo] =~ /^data:image/i
2014-11-16 22:45:57 +00:00
# data:image/png;base64
file = StaticAssets.data_url_attributes(params[:logo])
# store image 1:1
StaticAssets.store_raw(file[:content], file[:mime_type])
end
2014-10-22 21:00:11 +00:00
2014-11-19 22:45:37 +00:00
if params[:logo_resize] && params[:logo_resize] =~ /^data:image/i
# data:image/png;base64
file = StaticAssets.data_url_attributes(params[:logo_resize])
# store image 1:1
settings[:product_logo] = StaticAssets.store(file[:content], file[:mime_type])
end
# set changed settings
settings.each do |key, value|
Setting.set(key, value)
end
render json: {
result: 'ok',
settings: settings,
2014-10-22 21:00:11 +00:00
}
end
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
2014-11-01 10:57:02 +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
groups = Group.where(active: true)
addresses = EmailAddress.where(active: true)
render json: {
setup_done: true,
import_mode: Setting.get('import_mode'),
import_backend: Setting.get('import_backend'),
system_online_service: Setting.get('system_online_service'),
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
end