Skip to content

A platform for community discussion. Free, open, simple.

UX: Show all available avatar sources in the picker

Régis Hanol46851346109c
diff --git a/app/assets/stylesheets/common/base/user.scss b/app/assets/stylesheets/common/base/user.scss
index 20ac5772934..63f374dc698 100644
--- a/app/assets/stylesheets/common/base/user.scss
+++ b/app/assets/stylesheets/common/base/user.scss
@@ -563,15 +563,14 @@
}
.avatar-selector {
- --label-max-width: 300px;
+ --avatar-choice-size: 48px;
&__placeholder {
display: inline-flex;
align-items: center;
justify-content: center;
- flex: 0 0 45px;
- height: 45px;
- margin-inline-end: 0.75em;
+ flex: 0 0 var(--avatar-choice-size);
+ height: var(--avatar-choice-size);
border-radius: 50%;
background: var(--primary-low);
color: var(--primary-medium);
@@ -584,7 +583,7 @@
.avatar-choice {
display: grid;
- grid-template-columns: 2em 1fr auto;
+ grid-template-columns: 2em minmax(0, 1fr) auto;
grid-template-rows: auto auto;
align-items: center;
@@ -601,7 +600,7 @@
}
button {
- margin-left: auto;
+ margin-inline-start: auto;
}
label a {
@@ -612,22 +611,34 @@
label.radio {
display: flex;
align-items: center;
- max-width: var(--label-max-width);
- margin: 0 0.5em 0 0;
+ gap: 0.75em;
+ min-width: 0;
+ margin: 0;
+ margin-inline-end: 0.5em;
padding: 0;
- .avatar {
+ .avatar,
+ .avatar-placeholder {
flex: 0 0 auto;
- margin: 0 0.75em 0 0;
}
+
+ .avatar-placeholder {
+ width: var(--avatar-choice-size);
+ }
+ }
+
+ .avatar-uploader {
+ display: contents;
+ }
+
+ .error,
+ .warning {
+ margin: 0;
+ grid-column: 2 / -1;
}
.error {
color: var(--danger);
- margin: 0;
- max-width: calc(var(--label-max-width) - 20px);
- grid-column-start: 2;
- grid-column-end: 3;
}
}
diff --git a/app/controllers/session_controller.rb b/app/controllers/session_controller.rb
index b8e6896dd78..4a62aa5912c 100644
--- a/app/controllers/session_controller.rb
+++ b/app/controllers/session_controller.rb
@@ -1330,6 +1330,9 @@ class SessionController < ApplicationController
avatar_template: username && User.default_template(username),
can_upload_avatar:
group_ids.intersect?(SiteSetting.uploaded_avatars_allowed_groups_map),
+ can_edit_avatar:
+ !SiteSetting.auth_overrides_avatar &&
+ !SiteSetting.discourse_connect_overrides_avatar,
}
end
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 4586c15bcc1..9dc58dd302e 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -2344,10 +2344,12 @@ en:
letter_based: "System assigned profile picture"
uploaded_avatar: "Custom picture"
uploaded_avatar_empty: "Add a custom picture"
+ current_avatar: "Current picture"
+ associated_account: "%{provider} picture"
upload_title: "Upload your picture"
image_is_not_a_square: "Warning: we've cropped your image; width and height were not equal."
logo_small: "Site's small logo. Used by default."
- use_custom: "Or upload a custom avatar:"
+ use_custom: "Or choose another picture:"
change_profile_background:
title: "Profile Header"
diff --git a/frontend/discourse/app/components/code-login-form.gjs b/frontend/discourse/app/components/code-login-form.gjs
index fe59ea389d3..d66b3136f7c 100644
--- a/frontend/discourse/app/components/code-login-form.gjs
+++ b/frontend/discourse/app/components/code-login-form.gjs
@@ -32,7 +32,7 @@ import UserFieldsValidationHelper from "discourse/lib/user-fields-validation-hel
import { emailValid } from "discourse/lib/utilities";
import { getWebauthnCredential } from "discourse/lib/webauthn";
import User, { SECOND_FACTOR_METHODS } from "discourse/models/user";
-import { or } from "discourse/truth-helpers";
+import { not, or } from "discourse/truth-helpers";
import DButton from "discourse/ui-kit/d-button";
import DInputTip from "discourse/ui-kit/d-input-tip";
import DOtp from "discourse/ui-kit/d-otp";
@@ -60,6 +60,7 @@ export default class CodeLoginForm extends Component {
@tracked resendCooldown = 0;
@tracked otpGeneration = 0;
@tracked canUploadAvatar = false;
+ @tracked canEditAvatar = true;
@tracked pendingAvatar;
@tracked pendingAvatarUrl;
@tracked name = "";
@@ -592,6 +593,7 @@ export default class CodeLoginForm extends Component {
this._signupTrustLevel = result.trust_level ?? 0;
this.canUploadAvatar =
result.can_upload_avatar && allowsImages(false, this.siteSettings);
+ this.canEditAvatar = result.can_edit_avatar ?? true;
if (this.generatedUsername) {
this.usernameAvailable = !!this.username;
schedule("afterRender", () => this.#autoCreateGeneratedAccount());
@@ -1433,8 +1435,11 @@ export default class CodeLoginForm extends Component {
<button
aria-label={{i18n "code_login.change_avatar"}}
class="code-login-form__avatar"
- disabled={{this.verifying}}
- title={{i18n "code_login.change_avatar"}}
+ disabled={{or this.verifying (not this.canEditAvatar)}}
+ title={{if
+ this.canEditAvatar
+ (i18n "code_login.change_avatar")
+ }}
type="button"
{{on "click" this.changeAvatar}}
>
@@ -1445,9 +1450,11 @@ export default class CodeLoginForm extends Component {
{{dIcon "user"}}
</span>
{{/if}}
- <span class="code-login-form__avatar-edit">
- {{dIcon "pencil"}}
- </span>
+ {{#if this.canEditAvatar}}
+ <span class="code-login-form__avatar-edit">
+ {{dIcon "pencil"}}
+ </span>
+ {{/if}}
</button>
<div class="code-login-form__username-field">
diff --git a/frontend/discourse/app/components/modal/avatar-selector.gjs b/frontend/discourse/app/components/modal/avatar-selector.gjs
index 081daf92aff..ed96b2e5001 100644
--- a/frontend/discourse/app/components/modal/avatar-selector.gjs
+++ b/frontend/discourse/app/components/modal/avatar-selector.gjs
@@ -1,6 +1,6 @@
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
-import { fn, hash } from "@ember/helper";
+import { concat, fn, hash } from "@ember/helper";
import { on } from "@ember/modifier";
import { action, get } from "@ember/object";
import { service } from "@ember/service";
@@ -10,6 +10,7 @@ import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { isTesting } from "discourse/lib/environment";
import { allowsImages, validateUploadedFile } from "discourse/lib/uploads";
+import { findAll as findLoginMethods } from "discourse/models/login-method";
import { eq, or } from "discourse/truth-helpers";
import DButton from "discourse/ui-kit/d-button";
import DModal from "discourse/ui-kit/d-modal";
@@ -20,6 +21,24 @@ import dBoundAvatarTemplate from "discourse/ui-kit/helpers/d-bound-avatar-templa
import dIcon from "discourse/ui-kit/helpers/d-icon";
import { i18n } from "discourse-i18n";
+const ASSOCIATED_ACCOUNT_AVATAR = "associated_account";
+
+const AvatarChoice = <template>
+ <div class="avatar-choice" ...attributes>
+ <DRadioButton
+ @id={{@id}}
+ @name={{or @name "avatar"}}
+ @onChange={{@onChange}}
+ @selection={{@selection}}
+ @value={{@value}}
+ />
+ <label class="radio" for={{@id}}>
+ {{yield}}
+ </label>
+ {{yield to="action"}}
+ </div>
+</template>;
+
export default class AvatarSelectorModal extends Component {
@service currentUser;
@service siteSettings;
@@ -52,10 +71,6 @@ export default class AvatarSelectorModal extends Component {
return this._selected ?? this.defaultSelection;
}
- set selected(value) {
- this._selected = value;
- }
-
get deferSave() {
return this.args.model.deferSave;
}
@@ -84,13 +99,12 @@ export default class AvatarSelectorModal extends Component {
}
get selectableAvatars() {
- const mode = this.siteSettings.selectable_avatars_mode;
+ if (!this.showSelectableAvatars) {
+ return null;
+ }
+
const list = this.siteSettings.selectable_avatars;
- return mode !== "disabled"
- ? Array.isArray(list)
- ? list
- : list?.split("|") || []
- : null;
+ return Array.isArray(list) ? list : list?.split("|") || [];
}
get showSelectableAvatars() {
@@ -110,42 +124,65 @@ export default class AvatarSelectorModal extends Component {
case "tl3":
case "tl4":
const allowedTl = parseInt(mode.replace("tl", ""), 10);
- return (
- this.user?.admin ||
- this.user?.moderator ||
- (trustLevel ?? 0) >= allowedTl
- );
+ return this.user?.staff || (trustLevel ?? 0) >= allowedTl;
case "staff":
- return this.user?.admin || this.user?.moderator;
- case "everyone":
+ return this.user?.staff;
default:
return true;
}
}
+ get associatedAccountAvatars() {
+ const methods = findLoginMethods();
+ return (this.user?.associated_account_avatars || []).map((avatar) => ({
+ ...avatar,
+ name:
+ methods.find(({ name }) => name === avatar.name)?.prettyName ||
+ avatar.name,
+ selection: `${ASSOCIATED_ACCOUNT_AVATAR}:${avatar.id}`,
+ }));
+ }
+
get defaultSelection() {
if (this.deferSave) {
return "system";
} else if (this.user.use_logo_small_as_avatar) {
return "logo";
- } else if (this.user.avatar_template === this.user.system_avatar_template) {
- return "system";
- } else if (
- this.user.avatar_template === this.user.gravatar_avatar_template
+ }
+
+ const account = this.associatedAccountAvatars.find(
+ ({ id }) => id === this.user.selected_user_associated_account_id
+ );
+ if (account) {
+ return account.selection;
+ }
+
+ if (
+ !this.user.uploaded_avatar_id ||
+ this.user.avatar_template === this.user.system_avatar_template
) {
+ return "system";
+ }
+
+ if (this.user.avatar_template === this.user.gravatar_avatar_template) {
return "gravatar";
- } else {
+ }
+
+ if (this.user.avatar_template === this.user.custom_avatar_template) {
return "custom";
}
+
+ return "current";
}
- get selectedUploadId() {
- const selected = this.selected;
- switch (selected) {
+ get #selectedUploadId() {
+ switch (this.selected) {
case "system":
return this.user.system_avatar_upload_id;
case "gravatar":
return this.user.gravatar_avatar_upload_id;
+ case "current":
+ return this.user.uploaded_avatar_id;
default:
return this.user.custom_avatar_upload_id;
}
@@ -160,7 +197,9 @@ export default class AvatarSelectorModal extends Component {
}
const user = this.currentUser ?? this.user;
return (
- user.can_upload_avatar && allowsImages(user.staff, this.siteSettings)
+ this.user.can_upload_avatar &&
+ user.can_upload_avatar &&
+ allowsImages(user.staff, this.siteSettings)
);
}
@@ -174,28 +213,19 @@ export default class AvatarSelectorModal extends Component {
@action
onSelectedChanged(value) {
- this.selected = value;
- }
-
- afterAvatarSaved({ guardTesting = false } = {}) {
- if (this.args.model.onAvatarChange) {
- this.args.model.onAvatarChange();
- this.args.closeModal?.();
- } else if (!guardTesting || !isTesting()) {
- window.location.reload();
- }
+ this._selected = value;
}
@action
async selectAvatar(url, event) {
event?.preventDefault();
if (this.deferSave) {
- this.selected = url;
+ this._selected = url;
return;
}
try {
await this.user.selectAvatar(url);
- this.afterAvatarSaved();
+ this.#afterAvatarSaved();
} catch (error) {
popupAjaxError(error);
}
@@ -215,12 +245,7 @@ export default class AvatarSelectorModal extends Component {
this.#clearFilePreview();
this._pendingFile = file;
this._filePreview = URL.createObjectURL(file);
- this.selected = "custom";
- }
-
- @action
- uploadComplete() {
- this.selected = "custom";
+ this._selected = "custom";
}
@action
@@ -235,15 +260,15 @@ export default class AvatarSelectorModal extends Component {
}
);
- if (!result.gravatar_upload_id) {
- this.gravatarFailed = true;
- } else {
- this.gravatarFailed = false;
- this.user.setProperties({
- gravatar_avatar_upload_id: result.gravatar_upload_id,
- gravatar_avatar_template: result.gravatar_avatar_template,
- });
+ this.gravatarFailed = !result.gravatar_upload_id;
+ if (this.gravatarFailed) {
+ return;
}
+
+ this.user.setProperties({
+ gravatar_avatar_upload_id: result.gravatar_upload_id,
+ gravatar_avatar_template: result.gravatar_avatar_template,
+ });
} finally {
this.gravatarRefreshDisabled = false;
}
@@ -262,14 +287,32 @@ export default class AvatarSelectorModal extends Component {
this.args.closeModal();
return;
}
+
+ const account = this.associatedAccountAvatars.find(
+ ({ selection }) => selection === this.selected
+ );
+
try {
- await this.user.pickAvatar(this.selectedUploadId, this.selected);
- this.afterAvatarSaved({ guardTesting: true });
+ await this.user.pickAvatar(
+ account?.upload_id ?? this.#selectedUploadId,
+ account ? ASSOCIATED_ACCOUNT_AVATAR : this.selected,
+ account?.id
+ );
+ this.#afterAvatarSaved();
} catch (error) {
popupAjaxError(error);
}
}
+ #afterAvatarSaved() {
+ if (this.args.model.onAvatarChange) {
+ this.args.model.onAvatarChange();
+ this.args.closeModal?.();
+ } else if (!isTesting()) {
+ window.location.reload();
+ }
+ }
+
#clearFilePreview() {
if (this._filePreview) {
URL.revokeObjectURL(this._filePreview);
@@ -304,52 +347,43 @@ export default class AvatarSelectorModal extends Component {
{{/if}}
{{#if this.showCustomAvatarSelector}}
{{#if this.user.use_logo_small_as_avatar}}
- <div class="avatar-choice">
- <DRadioButton
- @id="logo-small"
- @name="logo"
- @onChange={{this.onSelectedChanged}}
- @selection={{this.selected}}
- @value="logo"
- />
- <label class="radio" for="logo-small">
- {{dBoundAvatarTemplate
- this.siteSettings.site_logo_small_url
- "large"
- }}
- {{i18n "user.change_avatar.logo_small"}}
- </label>
- </div>
- {{/if}}
- <div class="avatar-choice avatar-choice--system">
- <DRadioButton
- @id="system-avatar"
- @name="avatar"
+ <AvatarChoice
+ @id="logo-small"
+ @name="logo"
@onChange={{this.onSelectedChanged}}
@selection={{this.selected}}
- @value="system"
- />
- <label class="radio" for="system-avatar">
- {{#if this.systemAvatarTemplate}}
- {{dBoundAvatarTemplate this.systemAvatarTemplate "large"}}
- {{else}}
- <span class="avatar-selector__placeholder">{{dIcon
- "user"
- }}</span>
- {{/if}}
- {{i18n "user.change_avatar.letter_based"}}
- </label>
- </div>
+ @value="logo"
+ >
+ {{dBoundAvatarTemplate
+ this.siteSettings.site_logo_small_url
+ "large"
+ }}
+ {{i18n "user.change_avatar.logo_small"}}
+ </AvatarChoice>
+ {{/if}}
+ <AvatarChoice
+ class="avatar-choice--system"
+ @id="system-avatar"
+ @onChange={{this.onSelectedChanged}}
+ @selection={{this.selected}}
+ @value="system"
+ >
+ {{#if this.systemAvatarTemplate}}
+ {{dBoundAvatarTemplate this.systemAvatarTemplate "large"}}
+ {{else}}
+ <span class="avatar-selector__placeholder">{{dIcon "user"}}</span>
+ {{/if}}
+ {{i18n "user.change_avatar.letter_based"}}
+ </AvatarChoice>
{{#if this.allowGravatar}}
- <div class="avatar-choice avatar-choice--gravatar">
- <DRadioButton
- @id="gravatar"
- @name="avatar"
- @onChange={{this.onSelectedChanged}}
- @selection={{this.selected}}
- @value="gravatar"
- />
- <label class="radio" for="gravatar">
+ <AvatarChoice
+ class="avatar-choice--gravatar"
+ @id="gravatar"
+ @onChange={{this.onSelectedChanged}}
+ @selection={{this.selected}}
+ @value="gravatar"
+ >
+ <:default>
{{dBoundAvatarTemplate
this.user.gravatar_avatar_template
"large"
@@ -365,68 +399,97 @@ export default class AvatarSelectorModal extends Component {
}}
{{this.user.email}}
</span>
- </label>
-
- <DButton
- class="btn-default avatar-selector-refresh-gravatar"
- @action={{this.refreshGravatar}}
- @disabled={{this.gravatarRefreshDisabled}}
- @icon="arrows-rotate"
- @translatedTitle={{i18n
- "user.change_avatar.refresh_gravatar_title"
- gravatarName=this.siteSettings.gravatar_name
- }}
- />
-
- {{#if this.gravatarFailed}}
- <p class="error">
- {{i18n
- "user.change_avatar.gravatar_failed"
+ </:default>
+ <:action>
+ <DButton
+ class="btn-default avatar-selector-refresh-gravatar"
+ @action={{this.refreshGravatar}}
+ @disabled={{this.gravatarRefreshDisabled}}
+ @icon="arrows-rotate"
+ @translatedTitle={{i18n
+ "user.change_avatar.refresh_gravatar_title"
gravatarName=this.siteSettings.gravatar_name
}}
- </p>
- {{/if}}
- </div>
+ />
+
+ {{#if this.gravatarFailed}}
+ <p class="error">
+ {{i18n
+ "user.change_avatar.gravatar_failed"
+ gravatarName=this.siteSettings.gravatar_name
+ }}
+ </p>
+ {{/if}}
+ </:action>
+ </AvatarChoice>
{{/if}}
{{#if this.allowAvatarUpload}}
- <div class="avatar-choice avatar-choice--upload">
- <DRadioButton
- @id="uploaded-avatar"
- @name="avatar"
+ {{#if (eq this.defaultSelection "current")}}
+ <AvatarChoice
+ class="avatar-choice--current"
+ @id="current-avatar"
+ @onChange={{this.onSelectedChanged}}
+ @selection={{this.selected}}
+ @value="current"
+ >
+ {{dBoundAvatarTemplate this.user.avatar_template "large"}}
+ {{i18n "user.change_avatar.current_avatar"}}
+ </AvatarChoice>
+ {{/if}}
+ {{#each this.associatedAccountAvatars as |account|}}
+ <AvatarChoice
+ class="avatar-choice--associated-account"
+ @id={{concat "associated-account-avatar-" account.id}}
@onChange={{this.onSelectedChanged}}
@selection={{this.selected}}
- @value="custom"
- />
- <label class="radio" for="uploaded-avatar">
+ @value={{account.selection}}
+ >
+ {{dBoundAvatarTemplate account.avatar_template "large"}}
+ {{i18n
+ "user.change_avatar.associated_account"
+ provider=account.name
+ }}
+ </AvatarChoice>
+ {{/each}}
+ <AvatarChoice
+ class="avatar-choice--upload"
+ @id="uploaded-avatar"
+ @onChange={{this.onSelectedChanged}}
+ @selection={{this.selected}}
+ @value="custom"
+ >
+ <:default>
+ {{dBoundAvatarTemplate this.customAvatarTemplate "large"}}
{{#if this.customAvatarTemplate}}
- {{dBoundAvatarTemplate this.customAvatarTemplate "large"}}
{{i18n "user.change_avatar.uploaded_avatar"}}
{{else}}
{{i18n "user.change_avatar.uploaded_avatar_empty"}}
{{/if}}
- </label>
- {{#if this.deferSave}}
- <DPickFilesButton
- @acceptedFormatsOverride=".png,.jpg,.jpeg,.gif,.svg,.ico,.heic,.heif,.webp,.avif,.jxl"
- @currentUser={{hash staff=false}}
- @fileInputClass="hidden-upload-field"
- @fileInputId="deferred-avatar-upload"
- @icon="upload"
- @label="upload"
- @onFilesPicked={{this.filesPicked}}
- @showButton={{true}}
- />
- {{else}}
- <AvatarUploader
- class="avatar-uploader"
- @done={{this.uploadComplete}}
- @id="avatar-uploader"
- @uploadedAvatarId={{this.user.custom_avatar_upload_id}}
- @uploadedAvatarTemplate={{this.user.custom_avatar_template}}
- @user_id={{this.user.id}}
- />
- {{/if}}
- </div>
+ </:default>
+ <:action>
+ {{#if this.deferSave}}
+ <DPickFilesButton
+ @acceptedFormatsOverride=".png,.jpg,.jpeg,.gif,.svg,.ico,.heic,.heif,.webp,.avif,.jxl"
+ @currentUser={{hash staff=false}}
+ @fileInputClass="hidden-upload-field"
+ @fileInputId="deferred-avatar-upload"
+ @icon="upload"
+ @label="upload"
+ @onFilesPicked={{this.filesPicked}}
+ @showButton={{true}}
+ />
+ {{else}}
+ <AvatarUploader
+ class="avatar-uploader"
+ @done={{fn this.onSelectedChanged "custom"}}
+ @id="avatar-uploader"
+ @uploadedAvatarId={{this.user.custom_avatar_upload_id}}
+ @uploadedAvatarTemplate={{this.user.custom_avatar_template}}
+ @user_id={{this.user.id}}
+ />
+ {{/if}}
+ </:action>
+ </AvatarChoice>
{{/if}}
{{/if}}
</:body>
diff --git a/frontend/discourse/app/models/user.js b/frontend/discourse/app/models/user.js
index b7b30f20c85..cda0ff695f9 100644
--- a/frontend/discourse/app/models/user.js
+++ b/frontend/discourse/app/models/user.js
@@ -1271,10 +1271,14 @@ export default class User extends RestModel.extend(Evented) {
);
}
- pickAvatar(upload_id, type) {
+ pickAvatar(upload_id, type, associated_account_id) {
return ajax(userPath(`${this.username_lower}/preferences/avatar/pick`), {
type: "PUT",
- data: { upload_id, type },
+ data: {
+ upload_id,
+ type,
+ ...(associated_account_id && { associated_account_id }),
+ },
});
}
diff --git a/frontend/discourse/app/templates/preferences/account.gjs b/frontend/discourse/app/templates/preferences/account.gjs
index 74f0c9926cd..30362f5f99c 100644
--- a/frontend/discourse/app/templates/preferences/account.gjs
+++ b/frontend/discourse/app/templates/preferences/account.gjs
@@ -25,12 +25,7 @@ export default <template>
<UsernamePreference @user={{@controller.model}} />
</div>
- {{#unless
- (or
- @controller.siteSettings.discourse_connect_overrides_avatar
- @controller.siteSettings.auth_overrides_avatar
- )
- }}
+ {{#if @controller.model.can_edit_avatar}}
<div class="control-group pref-avatar" data-setting-name="user-avatar">
<label class="control-label" id="profile-picture">{{i18n
"user.avatar.title"
@@ -52,7 +47,7 @@ export default <template>
/>
</div>
</div>
- {{/unless}}
+ {{/if}}
{{#if @controller.canCheckEmails}}
<div class="control-group pref-email" data-setting-name="user-email">
diff --git a/frontend/discourse/tests/acceptance/user-preferences-account-test.js b/frontend/discourse/tests/acceptance/user-preferences-account-test.js
index edc2850764a..adbb91647ad 100644
--- a/frontend/discourse/tests/acceptance/user-preferences-account-test.js
+++ b/frontend/discourse/tests/acceptance/user-preferences-account-test.js
@@ -15,16 +15,32 @@ acceptance("User Preferences - Account", function (needs) {
let customUserProps = {};
let pickAvatarRequestData = null;
- let gravatarUploadId = 123456789;
+ const gravatarUploadId = 123456789;
+ const associatedAccountAvatars = [
+ {
+ id: 12,
+ name: "facebook",
+ upload_id: 42,
+ avatar_template: "/images/provider.png",
+ },
+ {
+ id: 13,
+ name: "github",
+ upload_id: 43,
+ avatar_template: "/images/another-provider.png",
+ },
+ ];
needs.pretender((server, helper) => {
server.get("/u/eviltrout.json", () => {
const json = cloneJSON(fixturesByUrl["/u/eviltrout.json"]);
- json.user.can_edit = true;
-
- for (const [key, value] of Object.entries(customUserProps)) {
- json.user[key] = value;
- }
+ Object.assign(json.user, {
+ can_edit: true,
+ can_edit_avatar: true,
+ can_upload_avatar: true,
+ associated_account_avatars: associatedAccountAvatars,
+ ...customUserProps,
+ });
return helper.response(json);
});
@@ -145,163 +161,176 @@ acceptance("User Preferences - Account", function (needs) {
.includesHtml("Connect");
});
- test("avatars are selectable for staff user when `selectable_avatars_mode` site setting is set to `staff`", async function (assert) {
- this.siteSettings.selectable_avatars_mode = "staff";
-
- customUserProps = {
- moderator: true,
- admin: false,
- };
+ [
+ { mode: "staff", user: { moderator: true }, access: "allowed" },
+ { mode: "staff", user: {}, access: "restricted" },
+ { mode: "no_one", user: { admin: true }, access: "restricted" },
+ { mode: "tl3", user: { trust_level: 3 }, access: "allowed" },
+ { mode: "tl3", user: { trust_level: 2 }, access: "restricted" },
+ {
+ mode: "tl3",
+ user: { trust_level: 2, moderator: true },
+ access: "allowed",
+ },
+ ].forEach(({ mode, user, access }) => {
+ test(`avatar sources are ${access} in ${mode} mode for ${JSON.stringify(user)}`, async function (assert) {
+ this.siteSettings.selectable_avatars_mode = mode;
+ customUserProps = { admin: false, moderator: false, ...user };
+
+ await visit("/u/eviltrout/preferences/account");
+ await click(".pref-avatar .btn");
+
+ assert.dom(".selectable-avatars").exists("offers the preset list");
+
+ if (access === "allowed") {
+ assert.dom("#uploaded-avatar").exists("allows uploaded pictures");
+ assert
+ .dom(".avatar-choice--associated-account")
+ .exists({ count: 2 }, "allows provider pictures");
+ } else {
+ assert
+ .dom("#uploaded-avatar")
+ .doesNotExist("restricts uploaded pictures");
+ assert
+ .dom(".avatar-choice--associated-account")
+ .doesNotExist("restricts provider pictures");
+ }
+ });
+ });
+ test("default avatar selector", async function (assert) {
await visit("/u/eviltrout/preferences/account");
await click(".pref-avatar .btn");
- assert
- .dom(".selectable-avatars")
- .exists("opens the avatar selection modal");
+ assert.dom(".avatar-choice").exists("opens the avatar selection modal");
- assert
- .dom("#uploaded-avatar")
- .exists("avatar selection modal includes option to upload");
- });
+ await click(".avatar-selector-refresh-gravatar");
- test("avatar selector handles an empty selectable avatars list", async function (assert) {
- this.siteSettings.selectable_avatars_mode = "everyone";
- this.siteSettings.selectable_avatars = [];
+ assert
+ .dom(".avatar[src='/images/gravatar_is_not_avatar.png']")
+ .exists("displays the new gravatar image");
- await visit("/u/eviltrout/preferences/account");
- await click(".pref-avatar .btn");
+ await click("#gravatar");
+ await click(".d-modal__footer .btn");
- assert
- .dom(".avatar-choice")
- .exists("opens the avatar selection modal without selectable avatars");
+ assert.deepEqual(
+ pickAvatarRequestData,
+ {
+ type: "gravatar",
+ upload_id: `${gravatarUploadId}`,
+ },
+ "includes the right pick avatar request params"
+ );
});
- test("avatars are not selectable for non-staff user when `selectable_avatars_mode` site setting is set to `staff`", async function (assert) {
- this.siteSettings.selectable_avatars_mode = "staff";
-
+ test("switching between connected accounts and an uploaded picture", async function (assert) {
+ const avatarTemplate = associatedAccountAvatars[0].avatar_template;
customUserProps = {
- moderator: false,
- admin: false,
+ avatar_template: avatarTemplate,
+ custom_avatar_template: avatarTemplate,
+ selected_user_associated_account_id: 12,
};
await visit("/u/eviltrout/preferences/account");
await click(".pref-avatar .btn");
assert
- .dom(".selectable-avatars")
- .exists("opens the avatar selection modal");
-
+ .dom("#associated-account-avatar-12")
+ .isChecked("prefers the recorded source over a matching upload");
assert
- .dom("#uploaded-avatar")
- .doesNotExist("avatar selection modal does not include option to upload");
- });
-
- test("avatars not selectable when `selectable_avatars_mode` site setting is set to `no_one`", async function (assert) {
- this.siteSettings.selectable_avatars_mode = "no_one";
-
- customUserProps = {
- admin: true,
- };
-
- await visit("/u/eviltrout/preferences/account");
- await click(".pref-avatar .btn");
-
+ .dom("#current-avatar")
+ .doesNotExist("does not duplicate the current picture");
assert
- .dom(".selectable-avatars")
- .exists("opens the avatar selection modal");
-
+ .dom('label[for="associated-account-avatar-12"]')
+ .includesText("Facebook", "identifies the provider");
assert
- .dom("#uploaded-avatar")
- .doesNotExist("avatar selection modal does not include option to upload");
- });
-
- test("avatars are selectable for user with required trust level when `selectable_avatars_mode` site setting is set to `tl3`", async function (assert) {
- this.siteSettings.selectable_avatars_mode = "tl3";
+ .dom('label[for="associated-account-avatar-12"] img')
+ .hasAttribute("src", avatarTemplate, "shows the provider preview");
+ assert
+ .dom(".avatar-choice--upload img")
+ .hasAttribute("src", avatarTemplate, "keeps the uploaded preview");
- customUserProps = {
- trust_level: 3,
- moderator: false,
- admin: false,
- };
+ await click("#associated-account-avatar-13");
+ await click(".d-modal__footer .btn-primary");
- await visit("/u/eviltrout/preferences/account");
- await click(".pref-avatar .btn");
+ assert.deepEqual(
+ pickAvatarRequestData,
+ {
+ type: "associated_account",
+ upload_id: "43",
+ associated_account_id: "13",
+ },
+ "saves the other provider and its upload"
+ );
- assert
- .dom(".selectable-avatars")
- .exists("opens the avatar selection modal");
+ await click("#uploaded-avatar");
+ await click(".d-modal__footer .btn-primary");
- assert
- .dom("#uploaded-avatar")
- .exists("avatar selection modal includes option to upload");
+ assert.deepEqual(
+ pickAvatarRequestData,
+ {
+ type: "custom",
+ upload_id: "1573",
+ },
+ "switches back to the upload without retaining the provider selection"
+ );
});
- test("avatars are not selectable for user without required trust level when `selectable_avatars_mode` site setting is set to `tl3`", async function (assert) {
- this.siteSettings.selectable_avatars_mode = "tl3";
-
+ test("saving a current picture preserves it without replacing the uploaded picture", async function (assert) {
+ this.siteSettings.selectable_avatars_mode = "everyone";
+ this.siteSettings.selectable_avatars = ["/images/current.png"];
customUserProps = {
- trust_level: 2,
- moderator: false,
- admin: false,
+ avatar_template: "/images/current.png",
+ uploaded_avatar_id: 42,
+ custom_avatar_template: "/images/uploaded.png",
+ custom_avatar_upload_id: 43,
};
await visit("/u/eviltrout/preferences/account");
await click(".pref-avatar .btn");
assert
- .dom(".selectable-avatars")
- .exists("opens the avatar selection modal");
-
+ .dom("#current-avatar")
+ .isChecked("keeps the current picture selected");
assert
- .dom("#uploaded-avatar")
- .doesNotExist("avatar selection modal does not include option to upload");
- });
+ .dom('label[for="current-avatar"] img')
+ .hasAttribute("src", "/images/current.png", "shows the current picture");
+ await click(".d-modal__footer .btn-primary");
- test("avatars are selectable for staff user when `selectable_avatars_mode` site setting is set to `tl3`", async function (assert) {
- this.siteSettings.selectable_avatars_mode = "tl3";
+ assert.deepEqual(
+ pickAvatarRequestData,
+ { type: "current", upload_id: "42" },
+ "preserves the current picture without replacing the saved upload"
+ );
+ });
+ test("a site default picture stays selected with an empty preset list", async function (assert) {
+ this.siteSettings.selectable_avatars_mode = "everyone";
+ this.siteSettings.selectable_avatars = [];
customUserProps = {
- trust_level: 2,
- moderator: true,
- admin: false,
+ avatar_template: "/images/site-default.png",
+ uploaded_avatar_id: null,
+ system_avatar_template: "/images/letter-avatar.png",
};
await visit("/u/eviltrout/preferences/account");
await click(".pref-avatar .btn");
+ assert.dom("#system-avatar").isChecked("selects the system source");
assert
- .dom(".selectable-avatars")
- .exists("opens the avatar selection modal");
-
- assert
- .dom("#uploaded-avatar")
- .exists("avatar selection modal includes option to upload");
+ .dom("#current-avatar")
+ .doesNotExist("does not offer an empty current upload");
});
- test("default avatar selector", async function (assert) {
+ test("connected account avatars require avatar upload permission", async function (assert) {
+ customUserProps = { can_upload_avatar: false };
+
await visit("/u/eviltrout/preferences/account");
await click(".pref-avatar .btn");
- assert.dom(".avatar-choice").exists("opens the avatar selection modal");
-
- await click(".avatar-selector-refresh-gravatar");
-
assert
- .dom(".avatar[src='/images/gravatar_is_not_avatar.png']")
- .exists("displays the new gravatar image");
-
- await click("#gravatar");
- await click(".d-modal__footer .btn");
-
- assert.deepEqual(
- pickAvatarRequestData,
- {
- type: "gravatar",
- upload_id: `${gravatarUploadId}`,
- },
- "includes the right pick avatar request params"
- );
+ .dom(".avatar-choice--associated-account")
+ .doesNotExist("provider choices require the same permission as uploads");
});
});
diff --git a/frontend/discourse/tests/integration/components/code-login-form-test.gjs b/frontend/discourse/tests/integration/components/code-login-form-test.gjs
index 9d8969094ef..f84eb467d74 100644
--- a/frontend/discourse/tests/integration/components/code-login-form-test.gjs
+++ b/frontend/discourse/tests/integration/components/code-login-form-test.gjs
@@ -1140,6 +1140,35 @@ module("Integration | Component | CodeLoginForm", function (hooks) {
assert.dom(".code-login-form__continue-to-site").isDisabled();
});
+ test("keeps the avatar read-only when it can't be edited", async function (assert) {
+ stubCodeRequest();
+ pretender.post("/session/login-code/verify", () =>
+ response({
+ username_required: true,
+ username: "jane",
+ avatar_template: "/letter/j.png",
+ can_upload_avatar: true,
+ can_edit_avatar: false,
+ })
+ );
+ pretender.get("/u/check_username", () =>
+ response({ available: true, avatar_template: "/letter/j.png" })
+ );
+
+ await goToCodeStep();
+ await fillIn(".d-otp-input", "123456");
+
+ assert
+ .dom(".code-login-form__avatar")
+ .isDisabled("prevents opening the picker");
+ assert
+ .dom(".code-login-form__avatar-edit")
+ .doesNotExist("hides the edit icon");
+ assert
+ .dom(".code-login-form__avatar img")
+ .exists("keeps the picture visible");
+ });
+
test("keeps a local avatar preview without uploading before account creation", async function (assert) {
stubCodeRequest();
this.siteSettings.selectable_avatars_mode = "tl2";
diff --git a/spec/requests/session_controller_spec.rb b/spec/requests/session_controller_spec.rb
index cbf921781ff..59a4d370aee 100644
--- a/spec/requests/session_controller_spec.rb
+++ b/spec/requests/session_controller_spec.rb
@@ -1597,6 +1597,17 @@ RSpec.describe SessionController do
expect(response.parsed_body["trust_level"]).to eq(1)
end
+ it "tells the deferred avatar selector whether the avatar can be edited" do
+ post "/session/login-code/verify.json", params: { email: "[email protected]", code: }
+
+ expect(response.parsed_body["can_edit_avatar"]).to eq(true)
+
+ SiteSetting.auth_overrides_avatar = true
+ post "/session/login-code/verify.json", params: { email: "[email protected]", code: }
+
+ expect(response.parsed_body["can_edit_avatar"]).to eq(false)
+ end
+
it "waits for username selection before creating or authenticating an account" do
User.set_callback(:create, :after, :ensure_in_trust_level_group)
diff --git a/spec/system/page_objects/modals/avatar_selector.rb b/spec/system/page_objects/modals/avatar_selector.rb
index 0de7de2ed02..fe4a3747ad3 100644
--- a/spec/system/page_objects/modals/avatar_selector.rb
+++ b/spec/system/page_objects/modals/avatar_selector.rb
@@ -6,28 +6,16 @@ module PageObjects
MODAL_SELECTOR = ".avatar-selector-modal"
AVATAR_UPLOAD_BUTTON_SELECTOR = ".avatar-uploader__button"
- def select_avatar_upload_option
- body.choose("avatar", option: "custom")
- end
-
def select_system_assigned_option
body.choose("avatar", option: "system")
end
- def click_avatar_upload_button
- body.find(AVATAR_UPLOAD_BUTTON_SELECTOR).click
- end
-
- def has_avatar_upload_button?
- has_css?(AVATAR_UPLOAD_BUTTON_SELECTOR)
- end
-
def has_no_avatar_upload_button?
has_no_css?(AVATAR_UPLOAD_BUTTON_SELECTOR)
end
def has_avatar_options?(*options)
- ordered_options = options.map { |o| ".avatar-choice--#{o}" }.join(" + ")
+ ordered_options = options.map { |option| ".avatar-choice--#{option}" }.join(" + ")
body.has_css?(ordered_options)
end
@@ -35,6 +23,31 @@ module PageObjects
def has_user_avatar_image_uploaded?
body.has_css?(".avatar[src*='uploads/default']")
end
+
+ def uploaded_avatar_id
+ body.find(AVATAR_UPLOAD_BUTTON_SELECTOR)["data-avatar-upload-id"]
+ end
+
+ def upload_image(file_path)
+ previous_id = uploaded_avatar_id
+ attach_file("custom-profile-upload", file_path, make_visible: true)
+ expect(body).to have_css(AVATAR_UPLOAD_BUTTON_SELECTOR) do |button|
+ upload_id = button["data-avatar-upload-id"]
+ upload_id.present? && upload_id != previous_id
+ end
+ self
+ end
+
+ def has_custom_picture_selected?
+ body.has_checked_field?("uploaded-avatar")
+ end
+
+ def has_upload_warning_below_choice?
+ body.has_css?(".avatar-choice--upload .warning") do |warning|
+ label = body.find("label[for='uploaded-avatar']").rect
+ warning.rect["y"] >= label["y"] + label["height"]
+ end
+ end
end
end
end
diff --git a/spec/system/page_objects/pages/user_preferences_account.rb b/spec/system/page_objects/pages/user_preferences_account.rb
index bc142022016..9fb566fdf57 100644
--- a/spec/system/page_objects/pages/user_preferences_account.rb
+++ b/spec/system/page_objects/pages/user_preferences_account.rb
@@ -16,8 +16,15 @@ module PageObjects
visit(user).click_edit_avatar_button
end
- def has_custom_uploaded_avatar_image?
- has_css?(".pref-avatar img.avatar[src*='user_avatar']")
+ def has_no_avatar_editor?
+ has_no_css?("#edit-avatar")
+ end
+
+ def has_custom_uploaded_avatar_image?(upload_id)
+ has_css?(".pref-avatar img.avatar[src*='/#{upload_id}_']") do |avatar|
+ avatar.evaluate_script("this.complete && this.naturalWidth") ==
+ avatar[:src].split("/")[-2].to_i
+ end
end
def has_system_avatar_image?
diff --git a/spec/system/user_page/user_preferences_account_spec.rb b/spec/system/user_page/user_preferences_account_spec.rb
index 9952fe5103f..b06b6022340 100644
--- a/spec/system/user_page/user_preferences_account_spec.rb
+++ b/spec/system/user_page/user_preferences_account_spec.rb
@@ -13,13 +13,28 @@ describe "User preferences | Account" do
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"))
- attach_file("custom-profile-upload", file_path, make_visible: true)
+ file_path = File.absolute_path(file_from_fixtures("logo.png"))
+ avatar_selector_modal.upload_image(file_path)
expect(avatar_selector_modal).to have_user_avatar_image_uploaded
+ expect(avatar_selector_modal).to have_custom_picture_selected
+ expect(avatar_selector_modal).to have_upload_warning_below_choice
+ screenshot_marker(label: "avatar-upload-warning")
+ upload_id = avatar_selector_modal.uploaded_avatar_id
+ avatar_selector_modal.click_primary_button
+ expect(avatar_selector_modal).to be_closed
+ expect(user_account_preferences_page).to have_custom_uploaded_avatar_image(upload_id)
+
+ user_account_preferences_page.open_avatar_selector_modal(user)
+ expect(avatar_selector_modal).to have_custom_picture_selected
+ file_path = File.absolute_path(file_from_fixtures("logo.jpg"))
+ avatar_selector_modal.upload_image(file_path)
+ expect(avatar_selector_modal).not_to have_content(
+ I18n.t("js.user.change_avatar.image_is_not_a_square"),
+ )
+ upload_id = avatar_selector_modal.uploaded_avatar_id
avatar_selector_modal.click_primary_button
expect(avatar_selector_modal).to be_closed
- expect(user_account_preferences_page).to have_custom_uploaded_avatar_image
+ expect(user_account_preferences_page).to have_custom_uploaded_avatar_image(upload_id)
user_account_preferences_page.open_avatar_selector_modal(user)
avatar_selector_modal.select_system_assigned_option
@@ -28,13 +43,25 @@ describe "User preferences | Account" do
expect(user_account_preferences_page).to have_system_avatar_image
end
- it "does not allow for custom pictures when the user is not in uploaded_avatars_allowed_groups" do
+ it "hides uploads when an admin edits a user outside uploaded_avatars_allowed_groups" do
SiteSetting.uploaded_avatars_allowed_groups = Group::AUTO_GROUPS[:admins]
+ sign_in(Fabricate(:admin, refresh_auto_groups: true))
user_account_preferences_page.open_avatar_selector_modal(user)
expect(avatar_selector_modal).to be_open
expect(avatar_selector_modal).to have_no_avatar_upload_button
end
+ it "hides avatar editing when authentication controls the picture" do
+ %i[auth_overrides_avatar discourse_connect_overrides_avatar].each do |setting|
+ SiteSetting.public_send("#{setting}=", true)
+
+ user_account_preferences_page.visit(user)
+ expect(user_account_preferences_page).to have_no_avatar_editor
+
+ SiteSetting.public_send("#{setting}=", false)
+ end
+ end
+
it "does not show Gravatar option when gravatars are not enabled" do
SiteSetting.gravatar_enabled = false
@@ -79,15 +106,12 @@ describe "User preferences | Account" do
after { DiscoursePluginRegistry.reset! }
- it "displays the correct name when overridden" do
+ it "shows the provider's overridden name and icon" do
user_account_preferences_page.visit(user)
name = find(".pref-associated-accounts table tr.test-auth .associated-account__name")
expect(name).not_to have_text("old pretty name")
expect(name).to have_text("new pretty name")
- end
- it "displays the correct icon when overridden" do
- user_account_preferences_page.visit(user)
icon_classes =
find(".pref-associated-accounts table tr.test-auth .associated-account__icon svg")[:class]
expect(icon_classes).not_to have_content("d-icon-flash")