change_password.rb 782 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Service::User::ChangePassword < Service::Base
  3. attr_reader :user, :current_password, :new_password
  4. def initialize(user:, current_password:, new_password:)
  5. super()
  6. @user = user
  7. @current_password = current_password
  8. @new_password = new_password
  9. end
  10. def execute
  11. PasswordHash.verified!(@user.password, @current_password)
  12. PasswordPolicy.new(@new_password).valid!
  13. @user.update!(password: @new_password)
  14. notify_user
  15. true
  16. end
  17. private
  18. def notify_user
  19. return if @user.email.blank?
  20. NotificationFactory::Mailer.notification(
  21. template: 'password_change',
  22. user: @user,
  23. objects: {
  24. user: @user,
  25. }
  26. )
  27. end
  28. end