From 865cb3feb9dee7df19083b22592b5cbfe6b2f245 Mon Sep 17 00:00:00 2001 From: Osama Sayegh Date: Fri, 10 Aug 2018 14:12:02 +0300 Subject: [PATCH] FIX: allow selecting site's default theme from preference --- app/services/user_updater.rb | 1 + lib/guardian.rb | 2 ++ spec/services/user_updater_spec.rb | 23 +++++++++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/app/services/user_updater.rb b/app/services/user_updater.rb index a580e968da6..de238e50ec0 100644 --- a/app/services/user_updater.rb +++ b/app/services/user_updater.rb @@ -88,6 +88,7 @@ class UserUpdater # special handling for theme_id cause we need to bump a sequence number if attributes.key?(:theme_ids) user_guardian = Guardian.new(user) + attributes[:theme_ids].reject!(&:blank?) attributes[:theme_ids].map!(&:to_i) if user_guardian.allow_themes?(attributes[:theme_ids]) user.user_option.theme_key_seq += 1 if user.user_option.theme_ids != attributes[:theme_ids] diff --git a/lib/guardian.rb b/lib/guardian.rb index db7d168428a..30bf87acd4d 100644 --- a/lib/guardian.rb +++ b/lib/guardian.rb @@ -359,6 +359,8 @@ class Guardian end def allow_themes?(theme_ids) + return true if theme_ids.blank? + if is_staff? && (theme_ids - Theme.theme_ids).blank? return true end diff --git a/spec/services/user_updater_spec.rb b/spec/services/user_updater_spec.rb index 47e37edc0d0..34daad1b64b 100644 --- a/spec/services/user_updater_spec.rb +++ b/spec/services/user_updater_spec.rb @@ -129,6 +129,29 @@ describe UserUpdater do expect(user.user_option.mailing_list_mode).to eq true end + it "filters theme_ids blank values before updating perferences" do + user = Fabricate(:user) + user.user_option.update!(theme_ids: [1]) + updater = UserUpdater.new(acting_user, user) + + updater.update(theme_ids: [""]) + user.reload + expect(user.user_option.theme_ids).to eq([]) + + updater.update(theme_ids: [nil]) + user.reload + expect(user.user_option.theme_ids).to eq([]) + + theme = Fabricate(:theme) + child = Fabricate(:theme) + theme.add_child_theme!(child) + theme.set_default! + + updater.update(theme_ids: [theme.id.to_s, child.id.to_s, "", nil]) + user.reload + expect(user.user_option.theme_ids).to eq([theme.id, child.id]) + end + context 'when sso overrides bio' do it 'does not change bio' do SiteSetting.sso_url = "https://www.example.com/sso"