Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/Auth/HasAvatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Statamic\Auth;

use Statamic\Contracts\Query\Builder;
use Statamic\Facades\URL;
use Statamic\Fields\Value;

Expand Down Expand Up @@ -42,20 +43,30 @@ public function avatarFieldValue()
);
}

/**
* The avatar asset from the asset field.
*/
public function avatarFieldAsset()
{
$value = $this->avatarFieldValue()->value();

return $value instanceof Builder ? $value->first() : $value;
}

/**
* The URL of the avatar from the asset field.
*/
public function avatarFieldUrl()
{
return optional($this->avatarFieldValue()->value())->url();
return optional($this->avatarFieldAsset())->url();
}

/**
* Square thumbnail URL of the avatar from the asset field.
*/
public function avatarFieldSquareThumbnailUrl()
{
$assetId = optional($this->avatarFieldValue()->value())->id();
$assetId = optional($this->avatarFieldAsset())->id();

if (! $assetId) {
return null;
Expand Down
1 change: 1 addition & 0 deletions src/Http/View/Composers/JavascriptComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ protected function user($user)
}

return $user->toAugmentedCollection()->merge([
'avatar' => $user->avatar(),
'preferences' => Preference::all(),
'permissions' => $user->permissions()->all(),
'color_mode' => $user->preferredColorMode(),
Expand Down
11 changes: 11 additions & 0 deletions tests/Auth/HasAvatarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ public function it_gets_the_avatar_if_theres_a_field_defined_in_the_blueprint_an
$this->assertEquals('https://www.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=128', $user->gravatarUrl(128));
}

#[Test]
public function it_gets_the_avatar_if_the_field_augments_to_a_query()
{
config(['statamic.system.always_augment_to_query' => true]);

$user = $this->withAvatarField()->withGravatar()->userWithUploadedAvatar();

$this->assertEquals('/avatars/john.jpg', $user->avatarFieldUrl());
$this->assertEquals('http://localhost/cp/thumbnails/YXZhdGFyczo6am9obi5qcGc=/small/square', $user->avatar());
}

/**
* @see https://github.com/statamic/cms/issues/3207
**/
Expand Down
29 changes: 29 additions & 0 deletions tests/Http/View/Composers/JavascriptComposerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

namespace Tests\Http\View\Composers;

use Facades\Statamic\Fields\BlueprintRepository;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Storage;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\AssetContainer;
use Statamic\Facades\Blueprint;
use Statamic\Facades\User;
use Statamic\Http\View\Composers\JavascriptComposer;
use Statamic\Statamic;
Expand Down Expand Up @@ -76,4 +81,28 @@ public function it_still_uses_fallback_locale_json_translations_for_non_english_

$this->assertSame('Nur im Fallback', $json['translations']['*.Only in fallback']);
}

#[Test]
public function it_provides_the_avatar_thumbnail_url_for_assets_in_a_private_container()
{
Storage::fake('avatars');
Storage::disk('avatars')->putFileAs('', UploadedFile::fake()->image('john.jpg'), 'john.jpg');
AssetContainer::make('avatars')->disk('avatars')->save();

$userBlueprint = Blueprint::makeFromFields(['avatar' => ['type' => 'assets', 'container' => 'avatars', 'max_files' => 1]]);
$assetBlueprint = Blueprint::makeFromFields([]);
BlueprintRepository::shouldReceive('find')->with('user')->andReturn($userBlueprint);
BlueprintRepository::shouldReceive('find')->with('assets/avatars')->andReturn($assetBlueprint);

$user = User::make()->email('john@example.com')->makeSuper()->set('avatar', 'john.jpg')->save();
$this->actingAs($user);

$view = app('view')->make('statamic::partials.scripts');

(new JavascriptComposer)->compose($view);

$json = Statamic::jsonVariables(request());

$this->assertSame('http://localhost/cp/thumbnails/YXZhdGFyczo6am9obi5qcGc=/small/square', $json['user']['avatar']);
}
}
Loading