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