mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: anonymous mode don't work when names are required
This commit is contained in:
parent
48f4fe7e7a
commit
d4974b7093
@ -2,18 +2,17 @@ class AnonymousShadowCreator
|
|||||||
|
|
||||||
def self.get_master(user)
|
def self.get_master(user)
|
||||||
return unless user
|
return unless user
|
||||||
return if !SiteSetting.allow_anonymous_posting
|
return unless SiteSetting.allow_anonymous_posting
|
||||||
|
|
||||||
if (master_id = user.custom_fields["master_id"].to_i) > 0
|
if (master_id = user.custom_fields["master_id"].to_i) > 0
|
||||||
User.find_by(id: master_id)
|
User.find_by(id: master_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.get(user)
|
def self.get(user)
|
||||||
return unless user
|
return unless user
|
||||||
return if !SiteSetting.allow_anonymous_posting ||
|
return unless SiteSetting.allow_anonymous_posting
|
||||||
user.trust_level < SiteSetting.anonymous_posting_min_trust_level
|
return if user.trust_level < SiteSetting.anonymous_posting_min_trust_level
|
||||||
|
|
||||||
if (shadow_id = user.custom_fields["shadow_id"].to_i) > 0
|
if (shadow_id = user.custom_fields["shadow_id"].to_i) > 0
|
||||||
shadow = User.find_by(id: shadow_id)
|
shadow = User.find_by(id: shadow_id)
|
||||||
@ -30,12 +29,14 @@ class AnonymousShadowCreator
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.create_shadow(user)
|
def self.create_shadow(user)
|
||||||
|
username = UserNameSuggester.suggest(I18n.t(:anonymous).downcase)
|
||||||
|
|
||||||
User.transaction do
|
User.transaction do
|
||||||
shadow = User.create!(
|
shadow = User.create!(
|
||||||
password: SecureRandom.hex,
|
password: SecureRandom.hex,
|
||||||
email: "#{SecureRandom.hex}@anon.#{Discourse.current_hostname}",
|
email: "#{SecureRandom.hex}@anon.#{Discourse.current_hostname}",
|
||||||
name: "",
|
name: username, # prevents error when names are required
|
||||||
username: UserNameSuggester.suggest(I18n.t(:anonymous).downcase),
|
username: username,
|
||||||
active: true,
|
active: true,
|
||||||
trust_level: 1,
|
trust_level: 1,
|
||||||
trust_level_locked: true,
|
trust_level_locked: true,
|
||||||
@ -44,27 +45,19 @@ class AnonymousShadowCreator
|
|||||||
created_at: 1.day.ago # bypass new user restrictions
|
created_at: 1.day.ago # bypass new user restrictions
|
||||||
)
|
)
|
||||||
|
|
||||||
shadow.email_tokens.update_all confirmed: true
|
shadow.email_tokens.update_all(confirmed: true)
|
||||||
shadow.activate
|
shadow.activate
|
||||||
|
|
||||||
|
|
||||||
# can not hold dupes
|
# can not hold dupes
|
||||||
UserCustomField.where(user_id: user.id,
|
UserCustomField.where(user_id: user.id, name: "shadow_id").destroy_all
|
||||||
name: "shadow_id").destroy_all
|
|
||||||
|
|
||||||
UserCustomField.create!(user_id: user.id,
|
UserCustomField.create!(user_id: user.id, name: "shadow_id", value: shadow.id)
|
||||||
name: "shadow_id",
|
UserCustomField.create!(user_id: shadow.id, name: "master_id", value: user.id)
|
||||||
value: shadow.id)
|
|
||||||
|
|
||||||
UserCustomField.create!(user_id: shadow.id,
|
|
||||||
name: "master_id",
|
|
||||||
value: user.id)
|
|
||||||
|
|
||||||
shadow.reload
|
shadow.reload
|
||||||
user.reload
|
user.reload
|
||||||
|
|
||||||
shadow
|
shadow
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -6,16 +6,19 @@ describe AnonymousShadowCreator do
|
|||||||
expect(AnonymousShadowCreator.get(Fabricate.build(:user))).to eq(nil)
|
expect(AnonymousShadowCreator.get(Fabricate.build(:user))).to eq(nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "Anonymous posting is enabled" do
|
||||||
|
|
||||||
|
before { SiteSetting.allow_anonymous_posting = true }
|
||||||
|
|
||||||
|
let(:user) { Fabricate(:user, trust_level: 3) }
|
||||||
|
|
||||||
it "returns no shadow if trust level is not met" do
|
it "returns no shadow if trust level is not met" do
|
||||||
SiteSetting.allow_anonymous_posting = true
|
|
||||||
expect(AnonymousShadowCreator.get(Fabricate.build(:user, trust_level: 0))).to eq(nil)
|
expect(AnonymousShadowCreator.get(Fabricate.build(:user, trust_level: 0))).to eq(nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns a new shadow once time expires" do
|
it "returns a new shadow once time expires" do
|
||||||
SiteSetting.allow_anonymous_posting = true
|
|
||||||
SiteSetting.anonymous_account_duration_minutes = 1
|
SiteSetting.anonymous_account_duration_minutes = 1
|
||||||
|
|
||||||
user = Fabricate(:user, trust_level: 3)
|
|
||||||
shadow = AnonymousShadowCreator.get(user)
|
shadow = AnonymousShadowCreator.get(user)
|
||||||
|
|
||||||
freeze_time 2.minutes.from_now
|
freeze_time 2.minutes.from_now
|
||||||
@ -32,9 +35,6 @@ describe AnonymousShadowCreator do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "returns a shadow for a legit user" do
|
it "returns a shadow for a legit user" do
|
||||||
SiteSetting.allow_anonymous_posting = true
|
|
||||||
user = Fabricate(:user, trust_level: 3)
|
|
||||||
|
|
||||||
shadow = AnonymousShadowCreator.get(user)
|
shadow = AnonymousShadowCreator.get(user)
|
||||||
shadow2 = AnonymousShadowCreator.get(user)
|
shadow2 = AnonymousShadowCreator.get(user)
|
||||||
|
|
||||||
@ -54,4 +54,12 @@ describe AnonymousShadowCreator do
|
|||||||
expect(shadow.anonymous?).to eq(true)
|
expect(shadow.anonymous?).to eq(true)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "works even when names are required" do
|
||||||
|
SiteSetting.full_name_required = true
|
||||||
|
|
||||||
|
expect { AnonymousShadowCreator.get(user) }.to_not raise_error
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user