add_internal.rb 1021 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Service::User::AddInternal < Service::BaseWithCurrentUser
  3. include Service::Concerns::HandlesCoreWorkflow
  4. def execute(user_data:, send_invite: false)
  5. UserInfo.with_user_id(current_user.id) do
  6. new_user = create_user!(user_data)
  7. send_invite(new_user) if send_invite
  8. new_user
  9. end
  10. end
  11. private
  12. def create_user!(user_data)
  13. Service::User::FilterPermissionAssignments
  14. .new(current_user: current_user)
  15. .execute(user_data: user_data)
  16. set_core_workflow_information(user_data, ::User)
  17. User.new(user_data).tap(&:save!)
  18. end
  19. def send_invite(user)
  20. return if user.email.blank?
  21. token = Token.create(action: 'PasswordReset', user_id: user.id)
  22. NotificationFactory::Mailer.notification(
  23. template: 'user_invite',
  24. user: user,
  25. objects: {
  26. token: token,
  27. user: user,
  28. current_user: current_user,
  29. }
  30. )
  31. end
  32. end