add_first_admin.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Service::User::AddFirstAdmin < Service::Base
  3. def execute(user_data:, request:)
  4. if Service::System::CheckSetup.done?
  5. raise Service::System::CheckSetup::SystemSetupError, __('This system has already been configured and an administrator account exists.')
  6. end
  7. if user_data[:email].blank?
  8. raise Exceptions::MissingAttribute.new('email', __("The required attribute 'email' is missing."))
  9. end
  10. PasswordPolicy.new(user_data[:password]).valid!
  11. User.new(user_data).tap do |user|
  12. user.role_ids = Role.where(name: %w[Admin Agent]).pluck(:id)
  13. user.group_ids = Group.pluck(:id)
  14. UserInfo.ensure_current_user_id { user.save! }
  15. configure_system(user:, request:)
  16. end
  17. end
  18. private
  19. def configure_system(user:, request:)
  20. Setting.set('system_init_done', true)
  21. Service::Image.organization_suggest(user.email) if user.email.present?
  22. Calendar.init_setup(request.remote_ip)
  23. begin
  24. TextModule.load(request.env['HTTP_ACCEPT_LANGUAGE'] || 'en-us')
  25. rescue => e
  26. logger.error "Unable to load text modules #{request.env['HTTP_ACCEPT_LANGUAGE'] || 'en-us'}: #{e.message}"
  27. end
  28. end
  29. end