123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
- class GettingStartedController < ApplicationController
- prepend_before_action -> { authorize! }, only: [:base]
- =begin
- Resource:
- GET /api/v1/getting_started
- Response:
- {
- "master_user": 1,
- "groups": [
- {
- "name": "group1",
- "active":true
- },
- {
- "name": "group2",
- "active":true
- }
- ]
- }
- Test:
- curl http://localhost/api/v1/getting_started -v -u #{login}:#{password}
- =end
- def index
- # check if first user already exists
- return if setup_done_response
- # check it auto wizard is already done
- return if auto_wizard_enabled_response
- # if admin user already exists, we need to be authenticated
- return if setup_done && !authentication_check
- # 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'),
- }
- end
- 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
- if auto_wizard_data.blank?
- render json: {
- auto_wizard: true,
- auto_wizard_success: false,
- message: __('Invalid auto wizard file.'),
- }
- return
- end
- # verify auto wizard token
- if auto_wizard_data['Token'] && auto_wizard_data['Token'] != params[:token]
- render json: {
- auto_wizard: true,
- auto_wizard_success: false,
- }
- return
- end
- # execute auto wizard
- auto_wizard_admin = AutoWizard.setup
- if !auto_wizard_admin
- render json: {
- auto_wizard: true,
- auto_wizard_success: false,
- message: __('Error during execution of auto wizard.'),
- }
- 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,
- auto_wizard_success: true,
- }
- end
- def base
- # validate url
- messages = {}
- settings = {}
- if !Setting.get('system_online_service')
- if (result = self.class.validate_uri(params[:url]))
- settings[:http_type] = result[:scheme]
- settings[:fqdn] = result[:fqdn]
- else
- messages[:url] = __('A URL looks like this: https://zammad.example.com')
- end
- end
- # validate organization
- if params[:organization].blank?
- messages[:organization] = 'Invalid!'
- else
- settings[:organization] = params[:organization]
- end
- # validate image
- if params[:logo] && params[:logo] =~ %r{^data:image}i
- file = StaticAssets.data_url_attributes(params[:logo])
- if !file[:content] || !file[:mime_type]
- messages[:logo] = __('The uploaded image could not be processed.')
- 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
- if messages.present?
- render json: {
- result: 'invalid',
- messages: messages,
- }
- return
- end
- # save image
- if params[:logo] && params[:logo] =~ %r{^data:image}i
- # data:image/png;base64
- file = StaticAssets.data_url_attributes(params[:logo])
- # store image 1:1
- StaticAssets.store_raw(file[:content], file[:mime_type])
- end
- if params[:logo_resize] && params[:logo_resize] =~ %r{^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,
- }
- end
- 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
- private
- def auto_wizard_enabled_response
- return false if !AutoWizard.enabled?
- render json: {
- auto_wizard: true
- }
- true
- end
- def setup_done
- # return false
- count = User.all.count
- done = true
- if count <= 2
- done = false
- end
- done
- end
- def setup_done_response
- 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: {
- email: EmailHelper.available_driver,
- },
- }
- true
- end
- def config_to_update
- {
- product_logo: Setting.get('product_logo')
- }
- end
- end
|