0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-05 17:25:34 +08:00
discourse/app/services/user/bulk_suspend.rb
Régis Hanol a1ebd19813
FEATURE: Bulk suspend users and filter the admin users list by activation (#41227)
- Adds an activation filter (All / Activated / Not activated) to the
Admin > Users > New tab so unverified accounts can be isolated in the
UI.
- Adds a bulk suspend action next to the existing bulk delete, plus a
"Select all" / "Clear all" control so a whole page of accounts can be
actioned at once.
- Shows the suspend reason on the Suspended tab, mirroring the silenced
tab.

Already-suspended users are excluded from bulk suspension, and the
penalty-reason columns are populated from a single batched query instead
of one query per row.

**SCREENSHOTS**

<img width="1400" height="1200"
alt="desktop-horizon-light-admin-users-activation-filter"
src="https://github.com/user-attachments/assets/31165d10-42cb-479b-897c-fafeb3283526"
/>
<img width="1400" height="1200"
alt="desktop-horizon-light-admin-users-bulk-select"
src="https://github.com/user-attachments/assets/037e8149-2ac6-40de-aaae-0d0e443ec143"
/>
<img width="1400" height="1200"
alt="desktop-horizon-light-admin-users-suspended"
src="https://github.com/user-attachments/assets/e6dbefed-4aaf-49bb-a9bd-52c7253e14b6"
/>
<img width="1400" height="1200"
alt="desktop-horizon-light-admin-users-bulk-suspend"
src="https://github.com/user-attachments/assets/5bdf85ef-4e30-4d8d-b521-d8639c1a2b97"
/>
2026-06-29 11:26:34 +02:00

52 lines
1.3 KiB
Ruby
Vendored

# frozen_string_literal: true
class User::BulkSuspend
include Service::Base
params do
attribute :user_ids, :array, compact_blank: true
attribute :reason, :string
attribute :suspend_until, :datetime
attribute :message, :string
validates :user_ids, length: { minimum: 1, maximum: 100 }
validates :reason, presence: true, length: { maximum: 300 }
validates :suspend_until, presence: true
end
model :users
policy :can_suspend_users
step :suspend
private
def fetch_users(params:)
# this order clause ensures we retrieve the users in the same order as the
# IDs in the param. we do this to ensure the users are suspended in the same
# order as they're selected in the UI
User
.where(id: params.user_ids)
.order(DB.sql_fragment("array_position(ARRAY[?], users.id)", params.user_ids))
.to_a
end
def can_suspend_users(guardian:, users:)
users.all? { guardian.can_suspend?(it) && !it.suspended? }
end
def suspend(users:, guardian:, params:)
users
.each
.with_index(1) do |user, position|
User::Action::SuspendAndPublish.call(
user:,
position:,
guardian:,
total_size: users.size,
suspend_until: params.suspend_until,
reason: params.reason,
message: params.message,
)
end
end
end