avatar.rb 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class User
  3. module Avatar
  4. extend ActiveSupport::Concern
  5. included do
  6. after_commit :fetch_avatar_for_email, on: %i[create update], unless: -> { BulkImportInfo.enabled? }
  7. before_validation :ensure_existing_image, :remove_invalid_image_source
  8. end
  9. def remove_invalid_image_source
  10. return if image_source.blank?
  11. return if image_source.match?(URI::DEFAULT_PARSER.make_regexp(%w[http https]))
  12. Rails.logger.debug { "Removed invalid image source '#{image_source}' for user '#{email}'" }
  13. self.image_source = nil
  14. end
  15. private
  16. def fetch_avatar_for_email
  17. return if Setting.get('import_mode')
  18. return if !valid_email_for_avatar?
  19. # save/update avatar using background job
  20. AvatarCreateJob.perform_later self
  21. end
  22. def ensure_existing_image
  23. return if Setting.get('import_mode')
  24. return if changes['image'].blank?
  25. return if ::Avatar.exists?(store_hash: image)
  26. raise Exceptions::UnprocessableEntity, "Invalid Store reference '#{image}' in 'image' attribute."
  27. end
  28. def valid_email_for_avatar?
  29. return if !saved_change_to_email?
  30. return if email.blank?
  31. email_address_validation = EmailAddressValidation.new(email)
  32. return if !email_address_validation.valid?
  33. true
  34. end
  35. end
  36. end