From: Tom Hughes Date: Tue, 16 Feb 2021 17:37:18 +0000 (+0000) Subject: Validate avatar images X-Git-Tag: live~1752 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/3c4f32a76075e91d60efd82c91bd64ebff5b6082 Validate avatar images Closes #3097 --- diff --git a/app/models/user.rb b/app/models/user.rb index 9cc8b7d83..42fa40e53 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -104,6 +104,8 @@ class User < ApplicationRecord validates :preferred_editor, :inclusion => Editors::ALL_EDITORS, :allow_nil => true validates :auth_uid, :unless => proc { |u| u.auth_provider.nil? }, :uniqueness => { :scope => :auth_provider } + validates :avatar, :if => proc { |u| u.attachment_changes["avatar"] }, + :image => true validates_email_format_of :email, :if => proc { |u| u.email_changed? } validates_email_format_of :new_email, :allow_blank => true, :if => proc { |u| u.new_email_changed? } diff --git a/app/validators/image_validator.rb b/app/validators/image_validator.rb new file mode 100644 index 000000000..5aad717f8 --- /dev/null +++ b/app/validators/image_validator.rb @@ -0,0 +1,5 @@ +class ImageValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + record.errors.add(attribute, " must be an image") unless value.image? + end +end