]> git.openstreetmap.org Git - rails.git/commitdiff
Extend user list to allow searching by name or email
authorTom Hughes <tom@compton.nu>
Sat, 22 Feb 2025 11:40:58 +0000 (11:40 +0000)
committerTom Hughes <tom@compton.nu>
Sat, 22 Feb 2025 11:40:58 +0000 (11:40 +0000)
app/controllers/users/lists_controller.rb
app/views/users/lists/show.html.erb
config/locales/en.yml
test/controllers/users/lists_controller_test.rb

index a2f35e9b213a8f5ea91a4a8ecbbe76ca1e889c97..7e3fa2a32801cb70854c8e4fc8b3f0dea9d70d66 100644 (file)
@@ -13,10 +13,11 @@ module Users
     ##
     # display a list of users matching specified criteria
     def show
-      @params = params.permit(:status, :ip, :before, :after)
+      @params = params.permit(:status, :username, :ip, :before, :after)
 
       users = User.all
       users = users.where(:status => @params[:status]) if @params[:status].present?
+      users = users.where("LOWER(email) = LOWER(?) OR LOWER(NORMALIZE(display_name, NFKC)) = LOWER(NORMALIZE(?, NFKC))", @params[:username], @params[:username]) if @params[:username].present?
       users = users.where("creation_address <<= ?", @params[:ip]) if @params[:ip].present?
 
       @users_count = users.limit(501).count
index dd037c7af6c3a13dee647bf9ba3e077ca658be7e..c3d15d18afe6656385195801474c89a299f5f4cb 100644 (file)
                      :data => { :behavior => "category_dropdown" },
                      :class => "form-select" %>
     </div>
+    <div class="mb-3 col-md">
+      <%= text_field_tag :username,
+                         params[:username],
+                         :placeholder => t(".name_or_email"),
+                         :autocomplete => "on",
+                         :class => "form-control" %>
+    </div>
     <div class="mb-3 col-md">
       <%= text_field_tag :ip,
                          params[:ip],
index f03a6eeb1f6ae418100d83b6c16aa4e8fae4be19..0deb199a38d034a4f8828891046ab418fa841674 100644 (file)
@@ -2897,6 +2897,7 @@ en:
           confirmed: Confirmed
           suspended: Suspended
           deleted: Deleted
+        name_or_email: Name or Email
         ip_address: IP Address
         search: Search
       page:
index 68e414bb85c78f8d063a0fb17bce173c9921139b..724c43222b61a84869a1f7787919b8c45153c623 100644 (file)
@@ -28,11 +28,13 @@ module Users
       moderator_user = create(:moderator_user)
       administrator_user = create(:administrator_user)
       suspended_user = create(:user, :suspended)
+      name_user = create(:user, :display_name => "Test User")
+      email_user = create(:user, :email => "test@example.com")
       ip_user = create(:user, :creation_address => "1.2.3.4")
 
-      # There are now 7 users - the five above, plus two extra "granters" for the
+      # There are now 9 users - the 7 above, plus two extra "granters" for the
       # moderator_user and administrator_user
-      assert_equal 7, User.count
+      assert_equal 9, User.count
 
       # Shouldn't work when not logged in
       get users_list_path
@@ -57,7 +59,7 @@ module Users
       get users_list_path
       assert_response :success
       assert_template :show
-      assert_select "table#user_list tbody tr", :count => 7
+      assert_select "table#user_list tbody tr", :count => 9
 
       # Should be able to limit by status
       get users_list_path, :params => { :status => "suspended" }
@@ -67,6 +69,38 @@ module Users
         assert_select "a[href='#{user_path(suspended_user)}']", :count => 1
       end
 
+      # Should be able to limit by name
+      get users_list_path, :params => { :username => "Test User" }
+      assert_response :success
+      assert_template :show
+      assert_select "table#user_list tbody tr", :count => 1 do
+        assert_select "a[href='#{user_path(name_user)}']", :count => 1
+      end
+
+      # Should be able to limit by name ignoring case
+      get users_list_path, :params => { :username => "test user" }
+      assert_response :success
+      assert_template :show
+      assert_select "table#user_list tbody tr", :count => 1 do
+        assert_select "a[href='#{user_path(name_user)}']", :count => 1
+      end
+
+      # Should be able to limit by email
+      get users_list_path, :params => { :username => "test@example.com" }
+      assert_response :success
+      assert_template :show
+      assert_select "table#user_list tbody tr", :count => 1 do
+        assert_select "a[href='#{user_path(email_user)}']", :count => 1
+      end
+
+      # Should be able to limit by email ignoring case
+      get users_list_path, :params => { :username => "TEST@example.com" }
+      assert_response :success
+      assert_template :show
+      assert_select "table#user_list tbody tr", :count => 1 do
+        assert_select "a[href='#{user_path(email_user)}']", :count => 1
+      end
+
       # Should be able to limit by IP address
       get users_list_path, :params => { :ip => "1.2.3.4" }
       assert_response :success