send.rb 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Service::User::PasswordReset::Send < Service::Base
  3. attr_reader :username
  4. def initialize(username:)
  5. super()
  6. @username = username
  7. @path = {
  8. reset: 'desktop/reset-password/verify/'
  9. }
  10. end
  11. def execute
  12. Service::CheckFeatureEnabled.new(name: 'user_lost_password').execute
  13. result = ::User.password_reset_new_token(username)
  14. # Result is always positive to avoid leaking of existing user accounts.
  15. return true if !result || !result[:token]
  16. raise EmailError if !result[:user] || result[:user].email.blank?
  17. result[:url] = "#{Setting.get('http_type')}://#{Setting.get('fqdn')}/#{@path[:reset]}#{result[:token].token}"
  18. NotificationFactory::Mailer.notification(
  19. template: 'password_reset',
  20. user: result[:user],
  21. objects: result,
  22. )
  23. true
  24. end
  25. class EmailError < StandardError
  26. def initialize
  27. super(__('The email could not be sent to the user.'))
  28. end
  29. end
  30. end