check_setup.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Service::System::CheckSetup < Service::Base
  3. attr_reader :status, :type
  4. STATES = %w[new automated in_progress done].freeze
  5. TYPES = %w[auto manual import].freeze
  6. def self.new?
  7. setup = new
  8. setup.execute
  9. setup.status == 'new'
  10. end
  11. def self.new!
  12. raise SystemSetupError, __('The system setup cannot be started, because there is another one running or it was completed before.') if !new?
  13. end
  14. def self.done?
  15. setup = new
  16. setup.execute
  17. setup.status == 'done'
  18. end
  19. def self.done!
  20. raise SystemSetupError, __('This operation cannot be continued, because the system set-up was not completed yet.') if !done?
  21. end
  22. def execute
  23. if Setting.get('import_mode')
  24. @status = 'in_progress'
  25. @type = 'import'
  26. return
  27. end
  28. if setup_done!
  29. @status = 'done'
  30. return
  31. end
  32. if Service::ExecuteLockedBlock.locked?('Zammad::System::Setup')
  33. @status = 'in_progress'
  34. @type = AutoWizard.enabled? ? 'auto' : 'manual'
  35. return
  36. end
  37. if AutoWizard.enabled?
  38. @status = 'automated'
  39. return
  40. end
  41. @status = 'new'
  42. end
  43. private
  44. def setup_done!
  45. is_done = Setting.get('system_init_done')
  46. has_admin = User.all.any? { |user| user.role?('Admin') && user.active? && user.id != 1 }
  47. if !is_done && has_admin
  48. Rails.logger.warn('The system setup is not marked as done, but at least one admin user is existing. Marking system setup as done.')
  49. Setting.set('system_init_done', true)
  50. return true
  51. end
  52. if is_done && !has_admin
  53. Setting.set('system_init_done', false)
  54. raise SystemSetupError, __('The system setup is marked as done, but no admin user is existing. Please run the system setup again.')
  55. end
  56. if is_done && has_admin
  57. return true
  58. end
  59. false
  60. end
  61. class SystemSetupError < StandardError; end
  62. end