Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEXT-27720 - Fix missing user avatar media #3678

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions changelog/_unreleased/2024-04-22-fix-missing-user-avatar-media.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: Fix missing user avatar media
issue: NEXT-00000
author: Benjamin Wittwer
author_email: dev@a-k-f.de
author_github: akf-bw
---
# Core
* Changed `UserController` to add the missing `avatarMedia` association
___
# Administration
* Changed `sw-profile-index` to load the missing user avatar on page load
* Changed `sw-users-permissions-user-listing` to add the missing `avatarMedia` association
* Changed `sw-users-permissions-user-detail` to load the missing user avatar on page load & provide media modal methods
* Changed `sw-users-permissions-user-detail` twig template to include media modal content
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ export default {
beforeMountComponent() {
this.userPromise.then((user) => {
this.user = user;

if (this.user.avatarId) {
this.loadMediaItem(this.user.avatarId);
}
});
},

Expand Down Expand Up @@ -361,11 +365,15 @@ export default {
});
},

setMediaItem({ targetId }) {
this.mediaRepository.get(targetId).then((response) => {
this.avatarMediaItem = response;
loadMediaItem(targetId) {
this.mediaRepository.get(targetId).then((media) => {
this.avatarMediaItem = media;
});
},

setMediaItem({ targetId }) {
this.user.avatarId = targetId;
this.loadMediaItem(targetId);
},

onDropMedia(mediaItem) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export default {
}

criteria.addAssociation('aclRoles');
criteria.addAssociation('avatarMedia');

return criteria;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ async function createWrapper(privileges = []) {
userService: {
getUser: () => Promise.resolve({ data: {} }),
},
mediaDefaultFolderService: {
getDefaultFolderId: (folder) => Promise.resolve(folder),
},
userValidationService: {},
integrationService: {},
repositoryFactory: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default {
inject: [
'userService',
'loginService',
'mediaDefaultFolderService',
'userValidationService',
'integrationService',
'repositoryFactory',
Expand Down Expand Up @@ -59,6 +60,8 @@ export default {
skeletonItemAmount: 3,
confirmPasswordModal: false,
timezoneOptions: [],
mediaDefaultFolderId: null,
showMediaModal: false,
};
},

Expand Down Expand Up @@ -208,6 +211,14 @@ export default {
return;
}

this.getMediaDefaultFolderId()
.then((id) => {
this.mediaDefaultFolderId = id;
})
.catch(() => {
this.mediaDefaultFolderId = null;
});

this.timezoneOptions = Shopware.Service('timezoneService').getTimezoneOptions();
const languagePromise = new Promise((resolve) => {
Shopware.State.commit('context/setApiLanguageId', this.languageId);
Expand Down Expand Up @@ -245,7 +256,7 @@ export default {
this.user = user;

if (this.user.avatarId) {
this.mediaItem = this.user.avatarMedia;
this.loadMediaItem(this.user.avatarId);
}

this.keyRepository = this.repositoryFactory.create(user.accessKeys.entity, this.user.accessKeys.source);
Expand Down Expand Up @@ -303,20 +314,42 @@ export default {
});
},

setMediaItem({ targetId }) {
loadMediaItem(targetId) {
this.mediaRepository.get(targetId).then((media) => {
this.mediaItem = media;
this.user.avatarMedia = media;
this.user.avatarId = targetId;
});
},

setMediaItem({ targetId }) {
this.user.avatarId = targetId;
this.loadMediaItem(targetId);
},

onUnlinkLogo() {
this.mediaItem = null;
this.user.avatarMedia = null;
this.user.avatarId = null;
},

onDropMedia(mediaItem) {
this.setMediaItem({ targetId: mediaItem.id });
},

onOpenMedia() {
this.showMediaModal = true;
},

onMediaSelectionChange([mediaEntity]) {
this.mediaItem = mediaEntity;
this.user.avatarMedia = mediaEntity;
this.user.avatarId = mediaEntity.id;
},

getMediaDefaultFolderId() {
return this.mediaDefaultFolderService.getDefaultFolderId('user');
},

onSearch(value) {
this.term = value;
this.clearSelection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@
:allow-multi-select="false"
:source-context="user"
:disabled="!acl.can('users_and_permissions.editor')"
variant="regular"
default-folder="user"
@media-drop="onDropMedia"
@media-upload-sidebar-open="onOpenMedia"
@media-upload-remove-image="onUnlinkLogo"
/>
{% endblock %}
Expand Down Expand Up @@ -326,6 +327,18 @@
{% endblock %}
</sw-card-view>

<!-- eslint-disable-next-line sw-deprecation-rules/no-twigjs-blocks -->
{% block sw_settings_user_detail_grid_inner_slot_media_modal %}
<sw-media-modal-v2
v-if="showMediaModal"
:allow-multi-select="false"
:initial-folder-id="mediaDefaultFolderId"
entity-context="user"
@modal-close="showMediaModal = false"
@media-modal-selection-change="onMediaSelectionChange"
/>
{% endblock %}

<!-- eslint-disable-next-line sw-deprecation-rules/no-twigjs-blocks -->
{% block sw_settings_user_detail_grid_inner_slot_delete_modal %}
<sw-modal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ async function createWrapper(privileges = [], options = {
userService: {
getUser: () => Promise.resolve({ data: {} }),
},
mediaDefaultFolderService: {
getDefaultFolderId: (folder) => Promise.resolve(folder),
},
userValidationService: {},
integrationService: {},
repositoryFactory: {
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Framework/Api/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function me(Context $context, Request $request, ResponseFactoryInterface
throw new ExpectedUserHttpException();
}
$criteria = new Criteria([$userId]);
$criteria->addAssociation('aclRoles');
$criteria->addAssociations(['aclRoles', 'avatarMedia']);

$user = $this->userRepository->search($criteria, $context)->first();
if (!$user) {
Expand Down