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