Class: ReviewableUser

Inherits:
Reviewable
  • Object
show all
Defined in:
app/models/reviewable_user.rb

Constant Summary

Constants inherited from Reviewable

Reviewable::TYPE_TO_BASIC_SERIALIZER

Instance Attribute Summary

Attributes inherited from Reviewable

#created_new

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Reviewable

action_aliases, #actions_for, add_custom_filter, #add_score, #apply_review_group, #basic_serializer, basic_serializers_for_list, #build_editable_fields, bulk_perform_targets, clear_custom_filters!, count_by_date, #create_result, #created_new!, custom_filters, default_visible, #delete_user_actions, #editable_for, #explain_score, #history, list_for, #log_history, lookup_serializer_for, min_score_for_priority, needs_review!, pending_count, #perform, #recalculate_score, score_required_to_hide_post, score_to_auto_close_topic, scores_with_topics, sensitivity_score, sensitivity_score_value, #serializer, serializer_for, set_priorities, spam_score_to_silence_new_user, #transition_to, types, typical_sensitivity, unseen_list_for, unseen_reviewable_count, #updatable_reviewable_scores, #update_fields, user_menu_list_for, valid_type?, viewable_by

Class Method Details

.create_for(user) ⇒ Object



4
5
6
# File 'app/models/reviewable_user.rb', line 4

def self.create_for(user)
  create(created_by_id: Discourse.system_user.id, target: user)
end

.set_approved_fields!(user, approved_by) ⇒ Object

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



81
82
83
84
85
# File 'app/models/reviewable_user.rb', line 81

def self.set_approved_fields!(user, approved_by)
  user.approved = true
  user.approved_by ||= approved_by
  user.approved_at ||= Time.zone.now
end

Instance Method Details

#build_actions(actions, guardian, args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/models/reviewable_user.rb', line 8

def build_actions(actions, guardian, args)
  return unless pending?

  if guardian.can_approve?(target)
    actions.add(:approve_user) do |a|
      a.icon = "user-plus"
      a.label = "reviewables.actions.approve_user.title"
    end
  end

  (actions, require_reject_reason: !is_a_suspect_user?)
end

#is_a_suspect_user?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'app/models/reviewable_user.rb', line 87

def is_a_suspect_user?
  reviewable_scores.any? { |rs| rs.reason == "suspect_user" }
end

#perform_approve_user(performed_by, args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/reviewable_user.rb', line 21

def perform_approve_user(performed_by, args)
  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, type: "signup_after_approval", user_id: target.id)
  end
  StaffActionLogger.new(performed_by).log_user_approve(target)

  create_result(:success, :approved)
end

#perform_delete_user(performed_by, args) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/models/reviewable_user.rb', line 35

def perform_delete_user(performed_by, args)
  # We'll delete the user if we can
  if target.present?
    destroyer = UserDestroyer.new(performed_by)

    DiscourseEvent.trigger(:suspect_user_deleted, target) if is_a_suspect_user?

    begin
      self.reject_reason = args[:reject_reason]

      if args[:send_email] && SiteSetting.must_approve_users?
        # 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

      delete_args = {}
      delete_args[:block_ip] = true if args[:block_ip]
      delete_args[:block_email] = true if args[:block_email]
      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

      destroyer.destroy(target, delete_args)
    rescue UserDestroyer::PostsExistError, Discourse::InvalidAccess
      # If a user has posts or user is an admin, we won't delete them to preserve their content.
      # However the reviewable record will be "rejected" and they will remain
      # unapproved in the database. A staff member can still approve them
      # via the admin.
    end
  end

  create_result(:success, :rejected)
end

#perform_delete_user_block(performed_by, args) ⇒ Object



73
74
75
76
77
# File 'app/models/reviewable_user.rb', line 73

def perform_delete_user_block(performed_by, args)
  args[:block_email] = true
  args[:block_ip] = true
  perform_delete_user(performed_by, args)
end