A platform for community discussion. Free, open, simple.
DEV: Add a Gravatar enable/disable toggle (#33533)
Ted Johansson3604aa2bccd2
diff --git a/app/assets/javascripts/discourse/app/components/modal/avatar-selector.gjs b/app/assets/javascripts/discourse/app/components/modal/avatar-selector.gjsindex 5936205ec22..1c6f66d19f5 100644--- a/app/assets/javascripts/discourse/app/components/modal/avatar-selector.gjs+++ b/app/assets/javascripts/discourse/app/components/modal/avatar-selector.gjs@@ -108,6 +108,10 @@ export default class AvatarSelectorModal extends Component {);}+ get allowGravatar() {+ return this.allowAvatarUpload && this.siteSettings.gravatar_enabled;+ }+@actiononSelectedChanged(value) {this.selected = value;@@ -210,7 +214,7 @@ export default class AvatarSelectorModal extends Component {</label></div>{{/if}}- <div class="avatar-choice">+ <div class="avatar-choice avatar-choice--system"><RadioButton@id="system-avatar"@name="avatar"@@ -223,8 +227,8 @@ export default class AvatarSelectorModal extends Component {{{i18n "user.change_avatar.letter_based"}}</label></div>- {{#if this.allowAvatarUpload}}- <div class="avatar-choice">+ {{#if this.allowGravatar}}+ <div class="avatar-choice avatar-choice--gravatar"><RadioButton@id="gravatar"@name="avatar"@@ -270,7 +274,9 @@ export default class AvatarSelectorModal extends Component {</p>{{/if}}</div>- <div class="avatar-choice">+ {{/if}}+ {{#if this.allowAvatarUpload}}+ <div class="avatar-choice avatar-choice--upload"><RadioButton@id="uploaded-avatar"@name="avatar"diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rbindex 5443ea52204..5cf21a370b7 100644--- a/app/controllers/users_controller.rb+++ b/app/controllers/users_controller.rb@@ -1327,6 +1327,10 @@ class UsersController < ApplicationControllertype = params[:type]+ if type == "gravatar" && !SiteSetting.gravatar_enabled?+ return render json: failed_json, status: 422+ end+invalid_type = type.present? && !AVATAR_TYPES_WITH_UPLOAD.include?(type) && type != "system"return render json: failed_json, status: 422 if invalid_typediff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex a21c90a8f71..5c31f80ef27 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -2691,6 +2691,7 @@ en:dashboard_visible_tabs: "Choose which dashboard tabs are visible."dashboard_general_tab_activity_metrics: "Choose reports to be displayed as activity metrics on the general tab."+ gravatar_enabled: "Use the <a href='https://gravatar.com/'>Gravatar</a> service for user avatars. This will prevent users from switching to Gravatar for their avatars, but will not impact users who already use it."gravatar_name: "Specify the name of the Gravatar service provider. This name is typically used to identify the source providing Gravatar avatars to the site."gravatar_base_url: "Specify the URL for accessing the Gravatar provider's API. This setting is critical for converting email addresses into Gravatar URLs where avatar images are stored."gravatar_login_url: "URL relative to `gravatar_base_url`, which provides the user with the login to the Gravatar service."diff --git a/config/site_settings.yml b/config/site_settings.ymlindex b40ebb550ac..d76d5cde406 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -992,17 +992,25 @@ users:default: 10000hidden: truearea: "notifications"+ gravatar_enabled:+ type: "bool"+ default: true+ client: true+ area: "users"gravatar_name:default: Gravatarclient: true+ hidden: truearea: "users"gravatar_base_url:default: www.gravatar.comclient: true+ hidden: truearea: "users"gravatar_login_url:default: /emailsclient: true+ hidden: truearea: "users"max_bookmarks_per_user:default: 2000diff --git a/db/post_migrate/20250709051949_disable_gravatar_enabled_if_unconfigured.rb b/db/post_migrate/20250709051949_disable_gravatar_enabled_if_unconfigured.rbnew file mode 100644index 00000000000..8db5197c594--- /dev/null+++ b/db/post_migrate/20250709051949_disable_gravatar_enabled_if_unconfigured.rb@@ -0,0 +1,24 @@+# frozen_string_literal: true++class DisableGravatarEnabledIfUnconfigured < ActiveRecord::Migration[7.2]+ def up+ execute <<~SQL+ INSERT INTO site_settings (name, data_type, value, created_at, updated_at)+ SELECT 'gravatar_enabled', 5, 'f', 'NOW()', 'NOW()'+ WHERE EXISTS (+ SELECT 1+ FROM site_settings+ WHERE name = 'gravatar_base_url' AND (+ VALUE = '' OR+ VALUE IS NULL+ )+ LIMIT 1+ )+ ON CONFLICT DO NOTHING;+ SQL+ end++ def down+ raise ActiveRecord::IrreversibleMigration+ end+enddiff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rbindex 1de844fb967..f393bd793be 100644--- a/spec/requests/users_controller_spec.rb+++ b/spec/requests/users_controller_spec.rb@@ -3646,6 +3646,17 @@ RSpec.describe UsersController doexpect(response.status).to eq(422)end+ it "raises an error when trying to pick Gravatar when gravatars are not enabled" do+ SiteSetting.gravatar_enabled = false+ put "/u/#{user1.username}/preferences/avatar/pick.json",+ params: {+ upload_id: upload.id,+ type: "gravatar",+ }++ expect(response.status).to eq(422)+ end+it "raises an error when selecting the custom/uploaded avatar and uploaded_avatars_allowed_groups is disabled" doSiteSetting.uploaded_avatars_allowed_groups = ""put "/u/#{user1.username}/preferences/avatar/pick.json",diff --git a/spec/system/page_objects/modals/avatar_selector.rb b/spec/system/page_objects/modals/avatar_selector.rbindex e4decff2ca5..0de7de2ed02 100644--- a/spec/system/page_objects/modals/avatar_selector.rb+++ b/spec/system/page_objects/modals/avatar_selector.rb@@ -26,6 +26,12 @@ module PageObjectshas_no_css?(AVATAR_UPLOAD_BUTTON_SELECTOR)end+ def has_avatar_options?(*options)+ ordered_options = options.map { |o| ".avatar-choice--#{o}" }.join(" + ")++ body.has_css?(ordered_options)+ end+def has_user_avatar_image_uploaded?body.has_css?(".avatar[src*='uploads/default']")enddiff --git a/spec/system/user_page/user_preferences_account_spec.rb b/spec/system/user_page/user_preferences_account_spec.rbindex 00f6b417c59..0696d95daf4 100644--- a/spec/system/user_page/user_preferences_account_spec.rb+++ b/spec/system/user_page/user_preferences_account_spec.rb@@ -11,6 +11,7 @@ describe "User preferences | Account", type: :system doit "saves custom picture and system assigned pictures" douser_account_preferences_page.open_avatar_selector_modal(user)expect(avatar_selector_modal).to be_open+ expect(avatar_selector_modal).to have_avatar_options("system", "gravatar", "upload")avatar_selector_modal.select_avatar_upload_optionfile_path = File.absolute_path(file_from_fixtures("logo.jpg"))@@ -33,6 +34,14 @@ describe "User preferences | Account", type: :system doexpect(avatar_selector_modal).to be_openexpect(avatar_selector_modal).to have_no_avatar_upload_buttonend++ it "does not show Gravatar option when gravatars are not enabled" do+ SiteSetting.gravatar_enabled = false++ user_account_preferences_page.open_avatar_selector_modal(user)+ expect(avatar_selector_modal).to be_open+ expect(avatar_selector_modal).to have_avatar_options("system", "upload")+ endenddescribe "external login provider URLs" do