]> git.openstreetmap.org Git - rails.git/commitdiff
Validate avatar images
authorTom Hughes <tom@compton.nu>
Tue, 16 Feb 2021 17:37:18 +0000 (17:37 +0000)
committerTom Hughes <tom@compton.nu>
Tue, 16 Feb 2021 17:37:18 +0000 (17:37 +0000)
Closes #3097

app/models/user.rb
app/validators/image_validator.rb [new file with mode: 0644]

index 9cc8b7d8361cb62a64f7f089a8256d6d6a80ee03..42fa40e538da528c8149c8bedcfa6f7504adab2a 100644 (file)
@@ -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 (file)
index 0000000..5aad717
--- /dev/null
@@ -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