Skip to content

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.gjs
index 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;
+ }
+
@action
onSelectedChanged(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.rb
index 5443ea52204..5cf21a370b7 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -1327,6 +1327,10 @@ class UsersController < ApplicationController
type = 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_type
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 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.yml
index b40ebb550ac..d76d5cde406 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -992,17 +992,25 @@ users:
default: 10000
hidden: true
area: "notifications"
+ gravatar_enabled:
+ type: "bool"
+ default: true
+ client: true
+ area: "users"
gravatar_name:
default: Gravatar
client: true
+ hidden: true
area: "users"
gravatar_base_url:
default: www.gravatar.com
client: true
+ hidden: true
area: "users"
gravatar_login_url:
default: /emails
client: true
+ hidden: true
area: "users"
max_bookmarks_per_user:
default: 2000
diff --git a/db/post_migrate/20250709051949_disable_gravatar_enabled_if_unconfigured.rb b/db/post_migrate/20250709051949_disable_gravatar_enabled_if_unconfigured.rb
new file mode 100644
index 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
+end
diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb
index 1de844fb967..f393bd793be 100644
--- a/spec/requests/users_controller_spec.rb
+++ b/spec/requests/users_controller_spec.rb
@@ -3646,6 +3646,17 @@ RSpec.describe UsersController do
expect(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" do
SiteSetting.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.rb
index 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 PageObjects
has_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']")
end
diff --git a/spec/system/user_page/user_preferences_account_spec.rb b/spec/system/user_page/user_preferences_account_spec.rb
index 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 do
it "saves custom picture and system assigned pictures" do
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", "gravatar", "upload")
avatar_selector_modal.select_avatar_upload_option
file_path = File.absolute_path(file_from_fixtures("logo.jpg"))
@@ -33,6 +34,14 @@ describe "User preferences | Account", type: :system do
expect(avatar_selector_modal).to be_open
expect(avatar_selector_modal).to have_no_avatar_upload_button
end
+
+ 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")
+ end
end
describe "external login provider URLs" do