مرحباً بالجميع،
أواجه مشكلة مستمرة في محاولة التعليق/إلغاء تنشيط المستخدمين بشكل مجمّع على نسخة Discourse الخاصة بي (الإصدار 3.5.0.beta5-dev) عبر وحدة تحكم Rails. أحاول استبعاد حساب المسؤول الخاص بي (hoangthai، معرّف المستخدم 100) وتعليق جميع المستخدمين النشطين الآخرين غير المسؤولين/غير المشرفين بشكل دائم مع سبب مخصص “Chưa gia hạn tài khoản” (في انتظار تجديد الحساب).
لقد جربت عدة طرق بناءً على أوامر وحدة تحكم Discourse الشائعة واقتراحات التصحيح، ولكن تظل الحسابات active: true على الرغم من إمكانية تعيين suspended_at و suspended_till.
إليكم تفصيل لما حاولت القيام به والأخطاء/النتائج:
هدفي: تعليق جميع المستخدمين بشكل دائم (باستثناء hoangthai، المعرّف 100) مع ذكر السبب “Chưa gia hạn tài khoản” (في انتظار تجديد الحساب).
المحاولات والمشاكل:
-
المحاولة الأولية (تعيين
suspend_reasonوsuspended_atوsuspended_tillمباشرةً):admin_id_to_exclude = 100 User.where.not(id: admin_id_to_exclude).each do |user| if user.active? && !user.admin? && !user.moderator? user.suspend_reason = "Tài khoản tạm khóa do nghỉ phép dài hạn." # محاولة السبب الأولى user.suspended_at = Time.current user.suspended_till = Time.current + 100.years user.save! puts "Khóa tài khoản: #{user.username}" end end- النتيجة:
NoMethodError: undefined method 'suspend_reason=' for an instance of User(اقتُرحsuspended_at=). أشار هذا إلى أنsuspend_reasonلم يكن السمة الصحيحة.
- النتيجة:
-
المحاولة الثانية (باستخدام
UserSuspension.suspend_userكما هو مقترح للإصدارات الأحدث):admin_id_to_exclude = 100 User.where.not(id: admin_id_to_exclude).each do |user| if user.active? && !user.admin? && !user.moderator? UserSuspension.suspend_user( user, Time.current + 100.years, nil, # suspend_reason_id "Tài khoản tạm khóa: Nghỉ phép dài hạn.", # custom_suspend_reason Discourse.system_user ) puts "Đã khóa tài khoản: #{user.username}" end end- النتيجة:
NameError: uninitialized constant UserSuspension(اقتُرحUserSuspender). أشار هذا إلى أن اسم الفئة غير صحيح.
- النتيجة:
-
المحاولة الثالثة (تم تصحيح اسم الفئة إلى
UserSuspender):admin_id_to_exclude = 100 User.where.not(id: admin_id_to_exclude).each do |user| if user.active? && !user.admin? && !user.moderator? UserSuspender.suspend_user( # ما زلت أحاول suspend_user user, Time.current + 100.years, nil, "Tài khoản tạm khóa: Nghỉ phép dài hạn.", Discourse.system_user ) puts "Đã khóa tài khoản: #{user.username}" end end- النتيجة:
NoMethodError: undefined method 'suspend_user' for class UserSuspender(اقتُرحUserSuspended?). أظهر هذا أن اسم الطريقة كان غير صحيح أيضًا.
- النتيجة:
-
المحاولة الرابعة (باستخدام
UserSuspender.new(...).suspendكنمط لكائن الخدمة):admin_id_to_exclude = 100 User.where.not(id: admin_id_to_exclude).each do |user| if user.active? begin UserSuspender.new( user, Discourse.system_user, reason: "Tài khoản tạm khóa: Nghỉ phép dài hạn.", suspended_till: nil # For permanent suspension ).suspend puts "Đã khóa tài khoản: #{user.username}" rescue = puts "Lỗi khi khóa tài khoản #{user.username}: #{e.message}" end end end- النتيجة: تم تشغيل هذا الأمر بدون أخطاء في وحدة التحكم. طبع “Đã khóa tài khoản: [اسم المستخدم]”.
- ومع ذلك، عند التحقق من حالة المستخدم في واجهة المستخدم الخاصة بالمسؤول أو الاستعلام مباشرة عن
user.activeفي وحدة التحكم، لا يزال المستخدمون يظهرونactive: true. تم تحديثsuspended_atوsuspended_tillفقط (على سبيل المثال،suspended_at: "2025-05-26 00:37:01.406064419 +0000", suspended_till: nil). كانت الحسابات لا تزال قادرة على تسجيل الدخول.
-
المحاولة الخامسة (تعيين
active = falseمباشرةً مع حقول التعليق):admin_id_to_exclude = 100 User.where.not(id: admin_id_to_exclude).each do |user| if user.active? begin user.suspended_at = Time.current user.suspended_till = nil # Permanent user.custom_suspension_reason = "Chưa gia hạn tài khoản" # Custom reason user.active = false # Crucial part user.save! puts "Đã **khóa VĨNH VIỄN** và vô hiệu hóa tài khoản: #{user.username}" rescue = puts "LỖI khi xử lý tài khoản #{user.username}: #{e.message}" end end end- النتيجة: تم تشغيل هذا الأمر أيضًا بدون أخطاء وطبع رسائل النجاح.
- ومع ذلك، كانت النتيجة هي نفسها: بقيت
active: true، وتم تحديثsuspended_at/suspended_till، ولكن الحسابات لم يتم إلغاء تنشيطها فعليًا…