A platform for community discussion. Free, open, simple.
FIX: title selector needs to flag whether title comes from badge or not
Neil Lalondef7c4c714094c
diff --git a/app/services/user_updater.rb b/app/services/user_updater.rbindex d2e92f63086..ade3e97db9f 100644--- a/app/services/user_updater.rb+++ b/app/services/user_updater.rb@@ -62,8 +62,13 @@ class UserUpdateruser.locale = attributes.fetch(:locale) { user.locale }user.date_of_birth = attributes.fetch(:date_of_birth) { user.date_of_birth }- if guardian.can_grant_title?(user)- user.title = attributes.fetch(:title) { user.title }+ if attributes[:title] &&+ attributes[:title] != user.title &&+ guardian.can_grant_title?(user, attributes[:title])+ user.title = attributes[:title]+ if user.badges.where(name: user.title).exists?+ user_profile.badge_granted_title = true+ endendCATEGORY_IDS.each do |attribute, level|diff --git a/lib/guardian.rb b/lib/guardian.rbindex d7b3e3fe93f..baa26363aa3 100644--- a/lib/guardian.rb+++ b/lib/guardian.rb@@ -221,6 +221,7 @@ class Guardiandef can_grant_title?(user, title = nil)return true if user && is_staff?+ return false if title.nil?return false if user != @userreturn true if user.badges.where(name: title, allow_title: true).exists?user.groups.where(title: title).exists?diff --git a/spec/services/user_updater_spec.rb b/spec/services/user_updater_spec.rbindex a65004ed7e3..14d820d488e 100644--- a/spec/services/user_updater_spec.rb+++ b/spec/services/user_updater_spec.rb@@ -159,7 +159,7 @@ describe UserUpdater doit 'allows user to change title' douser = Fabricate(:user, title: 'Emperor')guardian = stub- guardian.stubs(:can_grant_title?).with(user).returns(true)+ guardian.stubs(:can_grant_title?).with(user, 'Minion').returns(true)Guardian.stubs(:new).with(acting_user).returns(guardian)updater = UserUpdater.new(acting_user, user)@@ -169,11 +169,63 @@ describe UserUpdater doendend+ context 'title is from a badge' do+ let(:user) { Fabricate(:user, title: 'Emperor') }+ let(:badge) { Fabricate(:badge, name: 'Minion') }++ context 'badge can be used as a title' do+ before do+ badge.update_attributes(allow_title: true)+ end++ it 'can use as title, sets badge_granted_title' do+ BadgeGranter.grant(badge, user)+ updater = UserUpdater.new(user, user)+ updater.update(title: badge.name)+ user.reload+ expect(user.user_profile.badge_granted_title).to eq(true)+ end++ it 'badge has not been granted, does not change title' do+ badge.update_attributes(allow_title: true)+ updater = UserUpdater.new(user, user)+ updater.update(title: badge.name)+ user.reload+ expect(user.title).not_to eq(badge.name)+ expect(user.user_profile.badge_granted_title).to eq(false)+ end++ it 'changing to a title that is not from a badge, unsets badge_granted_title' do+ user.update_attributes(title: badge.name)+ user.user_profile.update_attributes(badge_granted_title: true)++ guardian = stub+ guardian.stubs(:can_grant_title?).with(user, 'Dancer').returns(true)+ Guardian.stubs(:new).with(user).returns(guardian)++ updater = UserUpdater.new(user, user)+ updater.update(title: 'Dancer')+ user.reload+ expect(user.title).to eq('Dancer')+ expect(user.user_profile.badge_granted_title).to eq(false)+ end+ end++ it 'cannot use as title, does not change title' do+ BadgeGranter.grant(badge, user)+ updater = UserUpdater.new(user, user)+ updater.update(title: badge.name)+ user.reload+ expect(user.title).not_to eq(badge.name)+ expect(user.user_profile.badge_granted_title).to eq(false)+ end+ end+context 'without permission to update title' doit 'does not allow user to change title' douser = Fabricate(:user, title: 'Emperor')guardian = stub- guardian.stubs(:can_grant_title?).with(user).returns(false)+ guardian.stubs(:can_grant_title?).with(user, 'Minion').returns(false)Guardian.stubs(:new).with(acting_user).returns(guardian)updater = UserUpdater.new(acting_user, user)