Skip to content

Commit 3604aa2

Browse files
authored
DEV: Add a Gravatar enable/disable toggle (#33533)
We want to add the ability to enable/disable users selecting Gravatar for their avatar. This change adds a site setting to enable/disable the option (default to enabled.) This will prevent users from configuring Gravatars from that point on. It does not affect already configured avatars. We're also taking this chance to hide some of the advanced settings from the UI.
1 parent bccf4e0 commit 3604aa2

File tree

8 files changed

+73
-4
lines changed

8 files changed

+73
-4
lines changed

app/assets/javascripts/discourse/app/components/modal/avatar-selector.gjs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ export default class AvatarSelectorModal extends Component {
108108
);
109109
}
110110

111+
get allowGravatar() {
112+
return this.allowAvatarUpload && this.siteSettings.gravatar_enabled;
113+
}
114+
111115
@action
112116
onSelectedChanged(value) {
113117
this.selected = value;
@@ -210,7 +214,7 @@ export default class AvatarSelectorModal extends Component {
210214
</label>
211215
</div>
212216
{{/if}}
213-
<div class="avatar-choice">
217+
<div class="avatar-choice avatar-choice--system">
214218
<RadioButton
215219
@id="system-avatar"
216220
@name="avatar"
@@ -223,8 +227,8 @@ export default class AvatarSelectorModal extends Component {
223227
{{i18n "user.change_avatar.letter_based"}}
224228
</label>
225229
</div>
226-
{{#if this.allowAvatarUpload}}
227-
<div class="avatar-choice">
230+
{{#if this.allowGravatar}}
231+
<div class="avatar-choice avatar-choice--gravatar">
228232
<RadioButton
229233
@id="gravatar"
230234
@name="avatar"
@@ -270,7 +274,9 @@ export default class AvatarSelectorModal extends Component {
270274
</p>
271275
{{/if}}
272276
</div>
273-
<div class="avatar-choice">
277+
{{/if}}
278+
{{#if this.allowAvatarUpload}}
279+
<div class="avatar-choice avatar-choice--upload">
274280
<RadioButton
275281
@id="uploaded-avatar"
276282
@name="avatar"

app/controllers/users_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,6 +1327,10 @@ def pick_avatar
13271327

13281328
type = params[:type]
13291329

1330+
if type == "gravatar" && !SiteSetting.gravatar_enabled?
1331+
return render json: failed_json, status: 422
1332+
end
1333+
13301334
invalid_type = type.present? && !AVATAR_TYPES_WITH_UPLOAD.include?(type) && type != "system"
13311335
return render json: failed_json, status: 422 if invalid_type
13321336

config/locales/server.en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2691,6 +2691,7 @@ en:
26912691
dashboard_visible_tabs: "Choose which dashboard tabs are visible."
26922692
dashboard_general_tab_activity_metrics: "Choose reports to be displayed as activity metrics on the general tab."
26932693

2694+
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."
26942695
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."
26952696
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."
26962697
gravatar_login_url: "URL relative to `gravatar_base_url`, which provides the user with the login to the Gravatar service."

config/site_settings.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,17 +992,25 @@ users:
992992
default: 10000
993993
hidden: true
994994
area: "notifications"
995+
gravatar_enabled:
996+
type: "bool"
997+
default: true
998+
client: true
999+
area: "users"
9951000
gravatar_name:
9961001
default: Gravatar
9971002
client: true
1003+
hidden: true
9981004
area: "users"
9991005
gravatar_base_url:
10001006
default: www.gravatar.com
10011007
client: true
1008+
hidden: true
10021009
area: "users"
10031010
gravatar_login_url:
10041011
default: /emails
10051012
client: true
1013+
hidden: true
10061014
area: "users"
10071015
max_bookmarks_per_user:
10081016
default: 2000
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
class DisableGravatarEnabledIfUnconfigured < ActiveRecord::Migration[7.2]
4+
def up
5+
execute <<~SQL
6+
INSERT INTO site_settings (name, data_type, value, created_at, updated_at)
7+
SELECT 'gravatar_enabled', 5, 'f', 'NOW()', 'NOW()'
8+
WHERE EXISTS (
9+
SELECT 1
10+
FROM site_settings
11+
WHERE name = 'gravatar_base_url' AND (
12+
VALUE = '' OR
13+
VALUE IS NULL
14+
)
15+
LIMIT 1
16+
)
17+
ON CONFLICT DO NOTHING;
18+
SQL
19+
end
20+
21+
def down
22+
raise ActiveRecord::IrreversibleMigration
23+
end
24+
end

spec/requests/users_controller_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3646,6 +3646,17 @@ def enabled?
36463646
expect(response.status).to eq(422)
36473647
end
36483648

3649+
it "raises an error when trying to pick Gravatar when gravatars are not enabled" do
3650+
SiteSetting.gravatar_enabled = false
3651+
put "/u/#{user1.username}/preferences/avatar/pick.json",
3652+
params: {
3653+
upload_id: upload.id,
3654+
type: "gravatar",
3655+
}
3656+
3657+
expect(response.status).to eq(422)
3658+
end
3659+
36493660
it "raises an error when selecting the custom/uploaded avatar and uploaded_avatars_allowed_groups is disabled" do
36503661
SiteSetting.uploaded_avatars_allowed_groups = ""
36513662
put "/u/#{user1.username}/preferences/avatar/pick.json",

spec/system/page_objects/modals/avatar_selector.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ def has_no_avatar_upload_button?
2626
has_no_css?(AVATAR_UPLOAD_BUTTON_SELECTOR)
2727
end
2828

29+
def has_avatar_options?(*options)
30+
ordered_options = options.map { |o| ".avatar-choice--#{o}" }.join(" + ")
31+
32+
body.has_css?(ordered_options)
33+
end
34+
2935
def has_user_avatar_image_uploaded?
3036
body.has_css?(".avatar[src*='uploads/default']")
3137
end

spec/system/user_page/user_preferences_account_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
it "saves custom picture and system assigned pictures" do
1212
user_account_preferences_page.open_avatar_selector_modal(user)
1313
expect(avatar_selector_modal).to be_open
14+
expect(avatar_selector_modal).to have_avatar_options("system", "gravatar", "upload")
1415

1516
avatar_selector_modal.select_avatar_upload_option
1617
file_path = File.absolute_path(file_from_fixtures("logo.jpg"))
@@ -33,6 +34,14 @@
3334
expect(avatar_selector_modal).to be_open
3435
expect(avatar_selector_modal).to have_no_avatar_upload_button
3536
end
37+
38+
it "does not show Gravatar option when gravatars are not enabled" do
39+
SiteSetting.gravatar_enabled = false
40+
41+
user_account_preferences_page.open_avatar_selector_modal(user)
42+
expect(avatar_selector_modal).to be_open
43+
expect(avatar_selector_modal).to have_avatar_options("system", "upload")
44+
end
3645
end
3746

3847
describe "external login provider URLs" do

0 commit comments

Comments
 (0)