2015-04-07 04:02:10 -04:00
|
|
|
class AnonymousShadowCreator
|
|
|
|
|
|
|
|
def self.get_master(user)
|
|
|
|
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
|
|
|
|
|
|
|
if (master_id = user.custom_fields["master_id"].to_i) > 0
|
|
|
|
User.find_by(id: master_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.get(user)
|
|
|
|
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
|
|
|
|
|
|
|
if (shadow_id = user.custom_fields["shadow_id"].to_i) > 0
|
2015-04-07 22:29:43 -04:00
|
|
|
shadow = User.find_by(id: shadow_id)
|
|
|
|
|
2017-11-09 18:05:53 -05:00
|
|
|
if shadow && (shadow.post_count + shadow.topic_count) > 0 &&
|
2015-04-07 22:29:43 -04:00
|
|
|
shadow.last_posted_at < SiteSetting.anonymous_account_duration_minutes.minutes.ago
|
|
|
|
shadow = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
shadow || create_shadow(user)
|
2015-04-07 04:02:10 -04:00
|
|
|
else
|
|
|
|
create_shadow(user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.create_shadow(user)
|
2015-05-15 08:20:15 -04:00
|
|
|
username = UserNameSuggester.suggest(I18n.t(:anonymous).downcase)
|
|
|
|
|
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
|
|
|
|
|
2015-04-07 22:29:43 -04:00
|
|
|
# can not hold dupes
|
2015-05-15 08:20:15 -04:00
|
|
|
UserCustomField.where(user_id: user.id, name: "shadow_id").destroy_all
|
2015-04-07 22:29:43 -04:00
|
|
|
|
2015-05-15 08:20:15 -04:00
|
|
|
UserCustomField.create!(user_id: user.id, name: "shadow_id", value: shadow.id)
|
|
|
|
UserCustomField.create!(user_id: shadow.id, name: "master_id", value: user.id)
|
2015-04-07 04:02:10 -04:00
|
|
|
|
|
|
|
shadow.reload
|
|
|
|
user.reload
|
|
|
|
|
|
|
|
shadow
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|