]> git.openstreetmap.org Git - rails.git/commitdiff
Disallow username changes to user_n if n isn't their id
authorAnton Khorev <tony29@yandex.ru>
Sat, 2 Sep 2023 11:19:59 +0000 (14:19 +0300)
committerAndy Allan <git@gravitystorm.co.uk>
Thu, 18 Jan 2024 10:33:56 +0000 (10:33 +0000)
.rubocop_todo.yml
app/models/user.rb
config/locales/en.yml
test/models/user_test.rb

index e6772b8a4fc6d764a35eca646cd41609e406abb1..d80653712bf255eb7e8cc288e2b42ed151ae1c47 100644 (file)
@@ -61,7 +61,7 @@ Metrics/BlockNesting:
 # Offense count: 26
 # Configuration parameters: CountComments, CountAsOne.
 Metrics/ClassLength:
-  Max: 299
+  Max: 305
 
 # Offense count: 59
 # Configuration parameters: AllowedMethods, AllowedPatterns.
index 5790d81e51d1be60d561eaa934aa863e2aad913b..8a471586af020cb3217386d8ebc4527eed655b2a 100644 (file)
@@ -99,6 +99,7 @@ class User < ApplicationRecord
   validates :display_name, :if => proc { |u| u.display_name_changed? },
                            :characters => { :url_safe => true },
                            :whitespace => { :leading => false, :trailing => false }
+  validate :display_name_cannot_be_user_id_with_other_id
   validates :email, :presence => true, :confirmation => true, :characters => true
   validates :email, :if => proc { |u| u.email_changed? },
                     :uniqueness => { :case_sensitive => false }
@@ -124,6 +125,12 @@ class User < ApplicationRecord
   before_save :update_tile
   after_save :spam_check
 
+  def display_name_cannot_be_user_id_with_other_id
+    display_name_changed? && display_name&.match(/^user_(\d+)$/i) do |m|
+      errors.add :display_name, I18n.t("activerecord.errors.messages.display_name_is_user_n") unless m[1].to_i == id
+    end
+  end
+
   def to_param
     display_name
   end
index 7ff2bfd0676ed460f1b4a66afc209529e34da16c..b266a87d66166d659bfe40784e370b48231b7e59 100644 (file)
@@ -40,6 +40,7 @@ en:
       messages:
         invalid_email_address: does not appear to be a valid e-mail address
         email_address_not_routable: is not routable
+        display_name_is_user_n: can't be user_n unless n is your user id
       models:
         user_mute:
           attributes:
index 5c48bb9698a6a73d56b7ab120b871d24902d18c5..c2571d0c0aaf785e64a522e98571af267ecc27bd 100644 (file)
@@ -91,6 +91,28 @@ class UserTest < ActiveSupport::TestCase
     end
   end
 
+  def test_display_name_user_id_new
+    existing_user = create(:user)
+    user = build(:user)
+
+    user.display_name = "user_#{existing_user.id}"
+    assert_not user.valid?, "user_<id> name is valid for existing user id when it shouldn't be"
+
+    user.display_name = "user_#{existing_user.id + 1}"
+    assert_not user.valid?, "user_<id> name is valid for new user id when it shouldn't be"
+  end
+
+  def test_display_name_user_id_rename
+    existing_user = create(:user)
+    user = create(:user)
+
+    user.display_name = "user_#{existing_user.id}"
+    assert_not user.valid?, "user_<id> name is valid for existing user id when it shouldn't be"
+
+    user.display_name = "user_#{user.id}"
+    assert_predicate user, :valid?, "user_<id> name is invalid for own id, when it should be"
+  end
+
   def test_friends_with
     alice = create(:user, :active)
     bob = create(:user, :active)