2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
class ReviewableUser < Reviewable
|
2019-04-16 14:42:47 -04:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
def self.create_for(user)
|
|
|
|
create(
|
|
|
|
created_by_id: Discourse.system_user.id,
|
|
|
|
target: user
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_actions(actions, guardian, args)
|
|
|
|
return unless pending?
|
|
|
|
|
2022-06-02 10:10:48 -04:00
|
|
|
if guardian.can_approve?(target)
|
2019-04-17 11:26:43 -04:00
|
|
|
actions.add(:approve_user) do |a|
|
|
|
|
a.icon = 'user-plus'
|
|
|
|
a.label = "reviewables.actions.approve_user.title"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-15 11:35:45 -04:00
|
|
|
delete_user_actions(actions, require_reject_reason: !is_a_suspect_user?)
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
|
2019-04-17 11:26:43 -04:00
|
|
|
def perform_approve_user(performed_by, args)
|
2019-01-03 12:03:01 -05:00
|
|
|
ReviewableUser.set_approved_fields!(target, performed_by)
|
|
|
|
target.save!
|
|
|
|
|
|
|
|
DiscourseEvent.trigger(:user_approved, target)
|
|
|
|
|
|
|
|
if args[:send_email] != false && SiteSetting.must_approve_users?
|
|
|
|
Jobs.enqueue(
|
|
|
|
:critical_user_email,
|
2022-02-04 18:43:53 -05:00
|
|
|
type: "signup_after_approval",
|
2019-01-03 12:03:01 -05:00
|
|
|
user_id: target.id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
StaffActionLogger.new(performed_by).log_user_approve(target)
|
|
|
|
|
|
|
|
create_result(:success, :approved)
|
|
|
|
end
|
|
|
|
|
2021-06-15 11:35:45 -04:00
|
|
|
def perform_delete_user(performed_by, args)
|
2019-04-10 11:00:14 -04:00
|
|
|
# We'll delete the user if we can
|
|
|
|
if target.present?
|
|
|
|
destroyer = UserDestroyer.new(performed_by)
|
|
|
|
|
2021-02-19 10:57:01 -05:00
|
|
|
DiscourseEvent.trigger(:suspect_user_deleted, target) if is_a_suspect_user?
|
2020-01-29 13:38:27 -05:00
|
|
|
|
2019-04-10 11:00:14 -04:00
|
|
|
begin
|
2021-01-14 17:43:26 -05:00
|
|
|
self.reject_reason = args[:reject_reason]
|
|
|
|
|
2021-02-22 08:07:47 -05:00
|
|
|
if args[:send_email] && SiteSetting.must_approve_users?
|
2021-01-14 17:43:26 -05:00
|
|
|
# Execute job instead of enqueue because user has to exists to send email
|
|
|
|
Jobs::CriticalUserEmail.new.execute({
|
|
|
|
type: :signup_after_reject,
|
|
|
|
user_id: target.id,
|
|
|
|
reject_reason: self.reject_reason
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2019-04-17 11:26:43 -04:00
|
|
|
delete_args = {}
|
|
|
|
delete_args[:block_ip] = true if args[:block_ip]
|
|
|
|
delete_args[:block_email] = true if args[:block_email]
|
2021-02-22 08:07:47 -05:00
|
|
|
delete_args[:context] = if performed_by.id == Discourse.system_user.id
|
|
|
|
I18n.t("user.destroy_reasons.reviewable_reject_auto")
|
|
|
|
else
|
|
|
|
I18n.t("user.destroy_reasons.reviewable_reject")
|
|
|
|
end
|
2019-04-17 11:26:43 -04:00
|
|
|
|
|
|
|
destroyer.destroy(target, delete_args)
|
2019-04-10 11:00:14 -04:00
|
|
|
rescue UserDestroyer::PostsExistError
|
|
|
|
# If a user has posts, we won't delete them to preserve their content.
|
2021-02-22 08:07:47 -05:00
|
|
|
# However the reviewable record will be "rejected" and they will remain
|
2019-04-10 11:00:14 -04:00
|
|
|
# unapproved in the database. A staff member can still approve them
|
|
|
|
# via the admin.
|
|
|
|
end
|
|
|
|
end
|
2019-01-03 12:03:01 -05:00
|
|
|
|
|
|
|
create_result(:success, :rejected)
|
|
|
|
end
|
|
|
|
|
2021-06-15 11:35:45 -04:00
|
|
|
def perform_delete_user_block(performed_by, args)
|
2019-04-17 11:26:43 -04:00
|
|
|
args[:block_email] = true
|
|
|
|
args[:block_ip] = true
|
2021-06-15 11:35:45 -04:00
|
|
|
perform_delete_user(performed_by, args)
|
2019-04-17 11:26:43 -04:00
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
# Update's the user's fields for approval but does not save. This
|
|
|
|
# can be used when generating a new user that is approved on create
|
|
|
|
def self.set_approved_fields!(user, approved_by)
|
|
|
|
user.approved = true
|
|
|
|
user.approved_by ||= approved_by
|
|
|
|
user.approved_at ||= Time.zone.now
|
|
|
|
end
|
2021-02-19 10:57:01 -05:00
|
|
|
|
|
|
|
def is_a_suspect_user?
|
|
|
|
reviewable_scores.any? { |rs| rs.reason == 'suspect_user' }
|
|
|
|
end
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: reviewables
|
|
|
|
#
|
2019-05-02 18:34:12 -04:00
|
|
|
# id :bigint not null, primary key
|
2019-01-03 12:03:01 -05:00
|
|
|
# type :string not null
|
|
|
|
# status :integer default(0), not null
|
|
|
|
# created_by_id :integer not null
|
|
|
|
# reviewable_by_moderator :boolean default(FALSE), not null
|
|
|
|
# reviewable_by_group_id :integer
|
|
|
|
# category_id :integer
|
|
|
|
# topic_id :integer
|
|
|
|
# score :float default(0.0), not null
|
|
|
|
# potential_spam :boolean default(FALSE), not null
|
|
|
|
# target_id :integer
|
|
|
|
# target_type :string
|
|
|
|
# target_created_by_id :integer
|
|
|
|
# payload :json
|
|
|
|
# version :integer default(0), not null
|
|
|
|
# latest_score :datetime
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2021-07-05 18:14:15 -04:00
|
|
|
# force_review :boolean default(FALSE), not null
|
|
|
|
# reject_reason :text
|
2019-01-03 12:03:01 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2022-07-28 04:16:33 -04:00
|
|
|
# idx_reviewables_score_desc_created_at_desc (score,created_at)
|
2019-05-02 18:34:12 -04:00
|
|
|
# index_reviewables_on_reviewable_by_group_id (reviewable_by_group_id)
|
2019-04-12 09:55:27 -04:00
|
|
|
# index_reviewables_on_status_and_created_at (status,created_at)
|
|
|
|
# index_reviewables_on_status_and_score (status,score)
|
|
|
|
# index_reviewables_on_status_and_type (status,type)
|
2020-09-01 04:00:36 -04:00
|
|
|
# index_reviewables_on_target_id_where_post_type_eq_post (target_id) WHERE ((target_type)::text = 'Post'::text)
|
2019-04-12 09:55:27 -04:00
|
|
|
# index_reviewables_on_topic_id_and_status_and_created_by_id (topic_id,status,created_by_id)
|
|
|
|
# index_reviewables_on_type_and_target_id (type,target_id) UNIQUE
|
2019-01-03 12:03:01 -05:00
|
|
|
#
|