Skip to content

Commit

Permalink
Fixed #3849
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Feb 15, 2019
1 parent c20adf2 commit a74b553
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 8 additions & 12 deletions src/elements/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -1218,6 +1213,7 @@ public function getPhoto()
public function setPhoto(Asset $photo = null)
{
$this->_photo = $photo;
$this->photoId = $photo->id ?? null;
}

// Indexes, etc.
Expand Down
4 changes: 2 additions & 2 deletions src/services/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/templates/users/_photo.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</div>
<div class="user-photo-controls">
<input type="file" name="photo" class="hidden" />
{% if user.photoId %}
{% if user.photo %}
<div class="btn upload-photo">{{ "Change photo"|t('app') }}</div>
<div class="btn delete-photo">{{ "Delete photo"|t('app') }}</div><br /><br />
<div class="btn edit-photo" data-photoid="{{ user.photoId }}">{{ "Edit photo"|t('app') }}</div>
Expand Down

0 comments on commit a74b553

Please sign in to comment.