From a74b5530a195b138fa81746c9e35b45b19b91b0b Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Fri, 15 Feb 2019 11:14:47 -0800 Subject: [PATCH] Fixed #3849 --- CHANGELOG-v3.md | 1 + src/elements/User.php | 20 ++++++++------------ src/services/Users.php | 4 ++-- src/templates/users/_photo.html | 2 +- 4 files changed, 12 insertions(+), 15 deletions(-) diff --git a/CHANGELOG-v3.md b/CHANGELOG-v3.md index 146436ec3c6..44481e986fd 100644 --- a/CHANGELOG-v3.md +++ b/CHANGELOG-v3.md @@ -6,6 +6,7 @@ - Fixed a bug where the `relatedTo` element query param could include results for elements that were related via soft-deleted Matrix blocks. ([#3846](https://github.com/craftcms/cms/issues/3846)) - Fixed a bug where some search queries were not returning results when they should, if using MySQL. - Fixed an error that could occur when syncing `project.yaml` changes if the `allowAdminChanges` config setting was disabled. ([#3823](https://github.com/craftcms/cms/issues/3823)) +- Fixed an `InvalidConfigException` that was thrown if a user’s photo was soft-deleted. ([#3849](https://github.com/craftcms/cms/issues/3849)) ## 3.1.11 - 2019-02-14 diff --git a/src/elements/User.php b/src/elements/User.php index 6a510db24d0..1162cf506a1 100644 --- a/src/elements/User.php +++ b/src/elements/User.php @@ -527,7 +527,7 @@ public static function findIdentityByAccessToken($token, $type = null) public $inheritorOnDelete; /** - * @var Asset|null user photo + * @var Asset|false|null user photo */ private $_photo; @@ -1191,23 +1191,18 @@ public function setEagerLoadedElements(string $handle, array $elements) * Returns the user's photo. * * @return Asset|null - * @throws InvalidConfigException if [[photoId]] is set but invalid */ public function getPhoto() { - if ($this->_photo !== null) { - return $this->_photo; - } - - if (!$this->photoId) { - return null; - } + if ($this->_photo === null) { + if (!$this->photoId) { + return null; + } - if (($this->_photo = Craft::$app->getAssets()->getAssetById($this->photoId)) === null) { - throw new InvalidConfigException('Invalid photo ID: ' . $this->photoId); + $this->_photo = Craft::$app->getAssets()->getAssetById($this->photoId) ?? false; } - return $this->_photo; + return $this->_photo ?: null; } /** @@ -1218,6 +1213,7 @@ public function getPhoto() public function setPhoto(Asset $photo = null) { $this->_photo = $photo; + $this->photoId = $photo->id ?? null; } // Indexes, etc. diff --git a/src/services/Users.php b/src/services/Users.php index d7813008aa9..60cfa9d36bc 100644 --- a/src/services/Users.php +++ b/src/services/Users.php @@ -437,7 +437,7 @@ public function saveUserPhoto(string $fileLocation, User $user, string $filename $assetsService = Craft::$app->getAssets(); // If the photo exists, just replace the file. - if ($user->photoId) { + if ($user->photoId && $user->getPhoto() !== null) { // No longer a new file. $assetsService->replaceAssetFile($assetsService->getAssetById($user->photoId), $fileLocation, $filenameToUse); } else { @@ -455,7 +455,7 @@ public function saveUserPhoto(string $fileLocation, User $user, string $filename $elementsService = Craft::$app->getElements(); $elementsService->saveElement($photo); - $user->photoId = $photo->id; + $user->setPhoto($photo); $elementsService->saveElement($user, false); } } diff --git a/src/templates/users/_photo.html b/src/templates/users/_photo.html index 26b5232218d..9646c32a2c8 100644 --- a/src/templates/users/_photo.html +++ b/src/templates/users/_photo.html @@ -6,7 +6,7 @@
- {% if user.photoId %} + {% if user.photo %}
{{ "Change photo"|t('app') }}
{{ "Delete photo"|t('app') }}


{{ "Edit photo"|t('app') }}