FIX: anonymous mode don't work when names are required

This commit is contained in:
Régis Hanol 2015-05-15 14:20:15 +02:00
parent 48f4fe7e7a
commit d4974b7093
2 changed files with 50 additions and 49 deletions

View File

@ -2,18 +2,17 @@ class AnonymousShadowCreator
def self.get_master(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
User.find_by(id: master_id)
end
end
def self.get(user)
return unless user
return if !SiteSetting.allow_anonymous_posting ||
user.trust_level < SiteSetting.anonymous_posting_min_trust_level
return unless SiteSetting.allow_anonymous_posting
return if user.trust_level < SiteSetting.anonymous_posting_min_trust_level
if (shadow_id = user.custom_fields["shadow_id"].to_i) > 0
shadow = User.find_by(id: shadow_id)
@ -30,12 +29,14 @@ class AnonymousShadowCreator
end
def self.create_shadow(user)
username = UserNameSuggester.suggest(I18n.t(:anonymous).downcase)
User.transaction do
shadow = User.create!(
password: SecureRandom.hex,
email: "#{SecureRandom.hex}@anon.#{Discourse.current_hostname}",
name: "",
username: UserNameSuggester.suggest(I18n.t(:anonymous).downcase),
name: username, # prevents error when names are required
username: username,
active: true,
trust_level: 1,
trust_level_locked: true,
@ -44,27 +45,19 @@ class AnonymousShadowCreator
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
# can not hold dupes
UserCustomField.where(user_id: user.id,
name: "shadow_id").destroy_all
UserCustomField.where(user_id: user.id, name: "shadow_id").destroy_all
UserCustomField.create!(user_id: user.id,
name: "shadow_id",
value: shadow.id)
UserCustomField.create!(user_id: shadow.id,
name: "master_id",
value: user.id)
UserCustomField.create!(user_id: user.id, name: "shadow_id", value: shadow.id)
UserCustomField.create!(user_id: shadow.id, name: "master_id", value: user.id)
shadow.reload
user.reload
shadow
end
end
end

View File

@ -6,16 +6,19 @@ describe AnonymousShadowCreator do
expect(AnonymousShadowCreator.get(Fabricate.build(:user))).to eq(nil)
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
SiteSetting.allow_anonymous_posting = true
expect(AnonymousShadowCreator.get(Fabricate.build(:user, trust_level: 0))).to eq(nil)
end
it "returns a new shadow once time expires" do
SiteSetting.allow_anonymous_posting = true
SiteSetting.anonymous_account_duration_minutes = 1
user = Fabricate(:user, trust_level: 3)
shadow = AnonymousShadowCreator.get(user)
freeze_time 2.minutes.from_now
@ -32,9 +35,6 @@ describe AnonymousShadowCreator do
end
it "returns a shadow for a legit user" do
SiteSetting.allow_anonymous_posting = true
user = Fabricate(:user, trust_level: 3)
shadow = AnonymousShadowCreator.get(user)
shadow2 = AnonymousShadowCreator.get(user)
@ -54,4 +54,12 @@ describe AnonymousShadowCreator do
expect(shadow.anonymous?).to eq(true)
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