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
8 changes: 2 additions & 6 deletions src/Fields/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Closure;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;

class BelongsToMany extends EagerField
{
Expand Down Expand Up @@ -64,11 +63,8 @@ public function resolve($repository, $attribute = null)
->resolveFromPivot($item->pivot)
)
->eagerState();
} catch (AuthorizationException $e) {
$class = get_class($item);
$policy = get_class(Gate::getPolicyFor($item));

abort(403, "You are not authorized to see the [{$class}] relationship from the HasMany field from the BelongsTo field. Check the [show] method from the [$policy]");
} catch (AuthorizationException) {
return null;
}
});

Expand Down
8 changes: 2 additions & 6 deletions src/Fields/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Binaryk\LaravelRestify\Repositories\Repository;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;

class HasMany extends EagerField
{
Expand Down Expand Up @@ -46,11 +45,8 @@ public function resolve($repository, $attribute = null)
return $this->repositoryClass::resolveWith($item)
->allowToShow(app(Request::class))
->eagerState();
} catch (AuthorizationException $e) {
$class = get_class($item);
$policy = get_class(Gate::getPolicyFor($item));

abort(403, "You are not authorized to see the [{$class}] relationship from the HasMany field from the BelongsTo field. Check the [show] method from the [$policy]");
} catch (AuthorizationException) {
return null;
}
});

Expand Down
12 changes: 9 additions & 3 deletions src/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,15 @@ public function resolveRelationships($request): array
return $collection->forIndex($request, $this);
})
->mapIntoRelated($request)
->map(function (Related $related) use ($request) {
return $related->resolve($request, $this)->getValue();
})->all();
->map(fn (Related $related) => $related->resolve($request, $this)->getValue())
->map(function (mixed $items) {
if ($items instanceof Collection) {
return $items->filter();
}

return $items;
})
->all();
}

/**
Expand Down
22 changes: 12 additions & 10 deletions tests/Fields/HasManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Binaryk\LaravelRestify\Tests\IntegrationTest;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Gate;
use Illuminate\Testing\Fluent\AssertableJson;

class HasManyTest extends IntegrationTest
{
Expand All @@ -36,7 +37,7 @@ protected function tearDown(): void
Repository::clearResolvedInstances();
}

public function test_has_many_present_on_relations()
public function test_has_many_present_on_relations(): void
{
$user = User::factory()->create();

Expand All @@ -59,7 +60,7 @@ public function test_has_many_present_on_relations()
]);
}

public function test_has_many_paginated_on_relation()
public function test_has_many_paginated_on_relation(): void
{
$user = tap($this->mockUsers()->first(), function ($user) {
$this->mockPosts($user->id, 22);
Expand All @@ -69,7 +70,7 @@ public function test_has_many_paginated_on_relation()
->assertJsonCount(20, 'data.relationships.posts');
}

public function test_has_many_unauthorized_see_relationship_posts()
public function test_has_many_filter_unauthorized_to_see_relationship_posts(): void
{
$_SERVER['restify.post.show'] = false;

Expand All @@ -79,7 +80,8 @@ public function test_has_many_unauthorized_see_relationship_posts()
});

$this->getJson(UserWithPosts::uriKey()."/$user->id?related=posts")
->assertForbidden();
->assertOk()
->assertJson(fn (AssertableJson $json) => $json->count('data.relationships.posts', 0)->etc());
}

public function test_field_ignored_when_storing()
Expand Down Expand Up @@ -107,7 +109,7 @@ public function test_can_display_other_pages()
field('email'),
field('password'),

HasMany::make('posts', PostRepository::class),
HasMany::make('posts', PostRepository::class),
]);

$this->getJson(UserWithPosts::uriKey()."/{$u->id}/posts?perPage=5")
Expand All @@ -131,7 +133,7 @@ public function test_can_apply_filters(): void
field('email'),
field('password'),

HasMany::make('posts', PostRepository::class),
HasMany::make('posts', PostRepository::class),
]);

$this->getJson(UserWithPosts::uriKey()."/{$u->id}/posts?title=wew")
Expand All @@ -155,7 +157,7 @@ public function test_filter_unauthorized_posts()
field('email'),
field('password'),

HasMany::make('posts', PostRepository::class),
HasMany::make('posts', PostRepository::class),
]);

$this->getJson(UserWithPosts::uriKey()."/{$u->id}/posts")
Expand Down Expand Up @@ -183,7 +185,7 @@ public function test_can_store()
field('email'),
field('password'),

HasMany::make('posts', PostRepository::class),
HasMany::make('posts', PostRepository::class),
]);

$this->postJson(UserWithPosts::uriKey()."/{$u->id}/posts", [
Expand All @@ -204,7 +206,7 @@ public function test_can_show(): void
field('email'),
field('password'),

HasMany::make('posts', PostRepository::class),
HasMany::make('posts', PostRepository::class),
]);

$this->getJson(UserWithPosts::to("$userId/posts/$post->id"), [
Expand Down Expand Up @@ -300,7 +302,7 @@ class UserWithPosts extends Repository
public static function related(): array
{
return [
'posts' => HasMany::make('posts', PostRepository::class),
'posts' => HasMany::make('posts', PostRepository::class),
];
}

Expand Down
6 changes: 4 additions & 2 deletions tests/Fields/MorphOneFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ protected function tearDown(): void
Repository::clearResolvedInstances();
}

public function test_morph_one_present_on_show_when_specified_related()
public function test_morph_one_present_on_show_when_specified_related(): void
{
$post = Post::factory()->create([
'user_id' => User::factory(),
]);

$relationships = $this->getJson(PostWithMophOneRepository::uriKey()."/$post->id?related=user")
$relationships = $this
->withoutExceptionHandling()
->getJson(PostWithMophOneRepository::uriKey()."/$post->id?related=user")
->assertJsonStructure([
'data' => [
'relationships' => [
Expand Down