حذف المستخدمين في وحدة تحكم Rails

We are using SSO to log in users that autenticated on our main website

One of our users got a message (on login) that there was an error and that she should contact an administrator. So we searched for her but could not find her profile.

I did the first best thing i could imagine and “deleted” her via command line

 User.find_by(email: "ex@example.com").delete

which was not smart and did not really work. When she tried to login she was faced with the same error. Now her profile exist (not findable via search but via url) but without an email or salt or password hash.

How can i purge her from the system in a way that she will be able to login again?

Im Glad for any input

Attched the Profile when searched for by user.find_by

id: 1541,
 username: "anon.nymus",
 created_at: Tue, 15 May 2018 17:25:11 UTC +00:00,
 updated_at: Sun, 02 Feb 2020 07:00:59 UTC +00:00,
 name: "Anon Nymus",
 seen_notification_id: 29204,
 last_posted_at: Mon, 28 May 2018 17:42:14 UTC +00:00,
 password_hash: nil,
 salt: nil,
 active: false,
 username_lower: "anon.nymus",
 last_seen_at: Thu, 17 May 2018 19:55:26 UTC +00:00,
 admin: false,
 last_emailed_at: Sun, 02 Feb 2020 07:01:07 UTC +00:00,
 last_emailed_at: Sun, 02 Feb 2020 07:01:07 UTC +00:00,
 trust_level: 2,
 approved: false,
 approved_by_id: nil,
 approved_at: nil,
 previous_visit_at: Wed, 16 May 2018 18:21:47 UTC +00:00,
 suspended_at: nil,
 suspended_till: nil,
 date_of_birth: nil,
 views: 0,
 flag_level: 0,
 ip_address: #################
 moderator: false,
 title: nil,
 uploaded_avatar_id: nil,
 locale: nil,
 primary_group_id: 49,
 registration_ip_address: nil,
 staged: false,
 first_seen_at: Tue, 15 May 2018 17:26:19 UTC +00:00,
 silenced_till: nil,
 group_locked_trust_level: nil,
 manual_locked_trust_level: nil,
 secure_identifier: nil>
إعجاب واحد (1)

ستحتاج إلى استخدام UserDestroyer لحذف المستخدم بشكل صحيح. يجب أن يعمل شيء مثل هذا:

UserDestroyer.new(Discourse.system_user).destroy(User.find_by_username_or_email("ex@example.com"), delete_posts: false)

سيؤدي ذلك إلى حذف المستخدم ذي البريد الإلكتروني ex@example.com بواسطة المستخدم النظامي. يمكنك أيضًا تقديم اسم مستخدم بدلاً من البريد الإلكتروني.

إذا كنت تريد أن تظهر السجلات من قام بحذف المستخدم، فاستبدل Discourse.system_user بـ User.find_by_username_or_email("admin@example.com") حيث admin@example.com هو البريد الإلكتروني لمدير الموقع.

إعجابَين (2)

Hi Joshua and thank you for your quick reply.

Unfortunatelly this resulted in an Error

UserDestroyer::PostsExistError: UserDestroyer::PostsExistError
from /var/www/discourse/app/services/user_destroyer.rb:18:in `destroy'

note: i used the username instead of the email to find the user due to the email being deleted by dumb me

OK, so that means the user you’re trying to delete has posts: PostsExistError.

The suggested method of resolution here would be to review the users posts, and delete them 1-by-1, in case there are any you wish to keep by changing the user. Remember that deleting the first post in a topic deletes the topic too.

The “just do it” method would be to change delete_posts: false to delete_posts: true.

إعجاب واحد (1)

I see this error. I found the user by username. (Unable to use email for some reason)

Discourse::InvalidAccess: can_delete_user? failed
from /var/www/discourse/lib/guardian/ensure_magic.rb:11:in `method_missing'

The command

UserDestroyer.new(Discourse.system_user).destroy(User.find_by(username: "someuserwhocertainlyexists"), delete_posts: true)

لقد واجهت نفس الخطأ:

UndefinedColumn: ERROR:  column users.email does not exist
LINE 1: SELECT "users".* FROM "users" WHERE "users"."email" = 'test'...

أعتقد أن رد jomaxro يمكن تحديثه بمثال يعمل. :slight_smile:
أيضًا، ما هو الأمر لحذف مستخدم باستخدام عنوان بريده الإلكتروني؟

إعجاب واحد (1)

نعتذر عن التأخير، تم الإصلاح.

3 إعجابات

مرحبًا، أواجه نفس الخطأ:

Discourse::InvalidAccess: can_delete_user? failed
from /var/www/discourse/lib/guardian/ensure_magic.rb:11:in `method_missing'

هل لديك أي فكرة؟

مرحبًا، هل لا يزال لا يوجد أحد لديه حل؟ يبدو أن المشكلة تحدث فقط عند محاولة حذف بعض الحسابات وليس غيرها، ولا أستطيع معرفة السبب.

مثال على حلقة التكرار الخاصة بي:

2821 : Gurpreet
2969 : GUY26
3012 : dboy3587
3015 : devilsports
3020 : jimyyyy
3029 : vdubgrub
3071 : Frank1
3126 : volgano
3172 : dawei
3307 : almolatham
3320 : embsoft
3352 : auto
3379 : sulemaninc
3447 : Baton
3463 : galvin
3465 : autopartchina
3497 : tracker
Discourse::InvalidAccess: can_delete_user? failed
from /var/www/discourse/lib/guardian/ensure_magic.rb:11:in `method_missing'

تم حذف جميع المستخدمين ما عدا الأخير. إذا قمت بتخطي هذا المستخدم، سيتم حذف المستخدمين التاليين حتى يتعذر حذف مستخدم آخر لسبب غير معروف.

تعديل: كان علي تعديل الإعدادات التالية:
delete all post max
delete_user_max_post_age
:+1:t6:

4 إعجابات

يبدو أنك لا تستطيع حذف مستخدمي المسؤول بهذه الأمر؟ :thinking:

صحيح. لا يمكن حذف مستخدمين من نوع الموظفين. لحذف المستخدم، يجب عليك أولاً إزالة صلاحيات المسؤول الخاصة به.

u = User.find_by_username_or_email("ex@example.com")
u.admin = false
u.moderator = false
u.save
UserDestroyer.new(Discourse.system_user).destroy(u, delete_posts: false)
3 إعجابات

شكرًا: delete_user_max_post_age يساعدني في تنظيف قاعدة بياناتي القديمة (أكثر من 20 عامًا)

إعجابَين (2)