2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-04-07 16:02:10 +08:00
|
|
|
class AnonymousShadowCreator
|
2019-05-29 12:26:06 +08:00
|
|
|
attr_reader :user
|
2015-04-07 16:02:10 +08:00
|
|
|
|
|
|
|
def self.get_master(user)
|
2019-05-29 12:26:06 +08:00
|
|
|
new(user).get_master
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.get(user)
|
|
|
|
new(user).get
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(user)
|
|
|
|
@user = user
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_master
|
2015-04-07 16:02:10 +08:00
|
|
|
return unless user
|
2015-05-15 20:20:15 +08:00
|
|
|
return unless SiteSetting.allow_anonymous_posting
|
2015-04-07 16:02:10 +08:00
|
|
|
|
2019-05-29 12:26:06 +08:00
|
|
|
user.master_user
|
2015-04-07 16:02:10 +08:00
|
|
|
end
|
|
|
|
|
2019-05-29 12:26:06 +08:00
|
|
|
def get
|
2015-04-07 16:02:10 +08:00
|
|
|
return unless user
|
2015-05-15 20:20:15 +08:00
|
|
|
return unless SiteSetting.allow_anonymous_posting
|
2023-10-25 09:45:10 +08:00
|
|
|
return if !user.in_any_groups?(SiteSetting.anonymous_posting_allowed_groups_map)
|
2018-02-13 14:58:38 +08:00
|
|
|
return if SiteSetting.must_approve_users? && !user.approved?
|
2015-04-07 16:02:10 +08:00
|
|
|
|
2019-05-29 12:26:06 +08:00
|
|
|
shadow = user.shadow_user
|
2015-04-08 10:29:43 +08:00
|
|
|
|
2019-05-29 12:26:06 +08:00
|
|
|
if shadow && (shadow.post_count + shadow.topic_count) > 0 && shadow.last_posted_at &&
|
|
|
|
shadow.last_posted_at < SiteSetting.anonymous_account_duration_minutes.minutes.ago
|
|
|
|
shadow = nil
|
2015-04-07 16:02:10 +08:00
|
|
|
end
|
2019-05-29 12:26:06 +08:00
|
|
|
|
|
|
|
shadow || create_shadow!
|
2015-04-07 16:02:10 +08:00
|
|
|
end
|
|
|
|
|
2019-05-29 12:26:06 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def create_shadow!
|
2021-12-22 01:09:55 +08:00
|
|
|
username = resolve_username
|
2015-05-15 20:20:15 +08:00
|
|
|
|
2015-04-07 16:02:10 +08:00
|
|
|
User.transaction do
|
|
|
|
shadow =
|
|
|
|
User.create!(
|
|
|
|
password: SecureRandom.hex,
|
2015-05-11 16:05:18 +08:00
|
|
|
email: "#{SecureRandom.hex}@anon.#{Discourse.current_hostname}",
|
2018-03-07 01:14:53 +08:00
|
|
|
skip_email_validation: true,
|
2015-05-15 20:20:15 +08:00
|
|
|
name: username, # prevents error when names are required
|
|
|
|
username: username,
|
2015-04-07 16:02:10 +08:00
|
|
|
active: true,
|
|
|
|
trust_level: 1,
|
2017-11-24 04:55:44 +08:00
|
|
|
manual_locked_trust_level: 1,
|
2018-02-13 14:58:38 +08:00
|
|
|
approved: true,
|
|
|
|
approved_at: 1.day.ago,
|
2015-04-08 10:29:43 +08:00
|
|
|
created_at: 1.day.ago, # bypass new user restrictions
|
2015-04-07 16:02:10 +08:00
|
|
|
)
|
|
|
|
|
2016-02-17 12:46:19 +08:00
|
|
|
shadow.user_option.update_columns(
|
2019-03-15 22:55:11 +08:00
|
|
|
email_messages_level: UserOption.email_level_types[:never],
|
2016-02-17 12:46:19 +08:00
|
|
|
email_digests: false,
|
|
|
|
)
|
|
|
|
|
2015-05-15 20:20:15 +08:00
|
|
|
shadow.email_tokens.update_all(confirmed: true)
|
2015-04-07 16:02:10 +08:00
|
|
|
shadow.activate
|
|
|
|
|
2019-05-29 12:26:06 +08:00
|
|
|
AnonymousUser.where(master_user_id: user.id, active: true).update_all(active: false)
|
|
|
|
AnonymousUser.create!(user_id: shadow.id, master_user_id: user.id, active: true)
|
2015-04-07 16:02:10 +08:00
|
|
|
|
|
|
|
shadow.reload
|
|
|
|
user.reload
|
|
|
|
|
|
|
|
shadow
|
|
|
|
end
|
|
|
|
end
|
2021-12-22 01:09:55 +08:00
|
|
|
|
|
|
|
def resolve_username
|
|
|
|
username = I18n.t("anonymous").downcase
|
2024-05-27 18:27:13 +08:00
|
|
|
username = "anonymous" if UserNameSuggester.sanitize_username(username).blank?
|
2021-12-22 01:09:55 +08:00
|
|
|
|
|
|
|
UserNameSuggester.suggest(username)
|
|
|
|
end
|
2015-04-07 16:02:10 +08:00
|
|
|
end
|