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

[8.x] Enforce implicit Route Model scoping #39440

Merged
merged 3 commits into from
Nov 4, 2021
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
2 changes: 1 addition & 1 deletion src/Illuminate/Routing/ImplicitRouteBinding.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static function resolveForRoute($container, $route)
? 'resolveSoftDeletableRouteBinding'
: 'resolveRouteBinding';

if ($parent instanceof UrlRoutable && in_array($parameterName, array_keys($route->bindingFields()))) {
if ($parent instanceof UrlRoutable && ($route->enforcesScopedBindings() || array_key_exists($parameterName, $route->bindingFields()))) {
$childRouteBindingMethod = $route->allowsTrashedBindings()
? 'resolveSoftDeletableChildRouteBinding'
: 'resolveChildRouteBinding';
Expand Down
22 changes: 22 additions & 0 deletions src/Illuminate/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,28 @@ public function excludedMiddleware()
return (array) ($this->action['excluded_middleware'] ?? []);
}

/**
* Indicate that the route should enforce scoping of multiple implicit Eloquent bindings.
*
* @return bool
*/
public function scopeBindings()
{
$this->action['scope_bindings'] = true;

return $this;
}

/**
* Determine if the route should enforce scoping of multiple implicit Eloquent bindings.
*
* @return bool
*/
public function enforcesScopedBindings()
{
return (bool) ($this->action['scope_bindings'] ?? false);
}

/**
* Specify that the route should not allow concurrent requests from the same session.
*
Expand Down
12 changes: 10 additions & 2 deletions src/Illuminate/Routing/RouteRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ class RouteRegistrar
* @var string[]
*/
protected $allowedAttributes = [
'as', 'domain', 'middleware', 'name', 'namespace', 'prefix', 'where',
'as',
'domain',
'middleware',
'name',
'namespace',
'prefix',
'scopeBindings',
'where',
];

/**
Expand All @@ -65,6 +72,7 @@ class RouteRegistrar
*/
protected $aliases = [
'name' => 'as',
'scopeBindings' => 'scope_bindings',
];

/**
Expand Down Expand Up @@ -216,7 +224,7 @@ public function __call($method, $parameters)
return $this->attribute($method, is_array($parameters[0]) ? $parameters[0] : $parameters);
}

return $this->attribute($method, $parameters[0]);
return $this->attribute($method, $parameters[0] ?? true);
}

throw new BadMethodCallException(sprintf(
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,6 @@ public function __call($method, $parameters)
return (new RouteRegistrar($this))->attribute($method, is_array($parameters[0]) ? $parameters[0] : $parameters);
}

return (new RouteRegistrar($this))->attribute($method, $parameters[0]);
return (new RouteRegistrar($this))->attribute($method, $parameters[0] ?? true);
}
}
110 changes: 100 additions & 10 deletions tests/Integration/Routing/ImplicitRouteBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,15 @@ protected function defineDatabaseMigrations(): void
$table->softDeletes();
});

Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->timestamps();
});

$this->beforeApplicationDestroyed(function () {
Schema::dropIfExists('users');
Schema::dropIfExists('posts');
});
}

Expand All @@ -60,14 +67,14 @@ public function testWithRouteCachingEnabled()
$this->defineCacheRoutes(<<<PHP
<?php

use Illuminate\Tests\Integration\Routing\ImplicitBindingModel;
use Illuminate\Tests\Integration\Routing\ImplicitBindingUser;

Route::post('/user/{user}', function (ImplicitBindingModel \$user) {
Route::post('/user/{user}', function (ImplicitBindingUser \$user) {
return \$user;
})->middleware('web');
PHP);

$user = ImplicitBindingModel::create(['name' => 'Dries']);
$user = ImplicitBindingUser::create(['name' => 'Dries']);

$response = $this->postJson("/user/{$user->id}");

Expand All @@ -79,11 +86,11 @@ public function testWithRouteCachingEnabled()

public function testWithoutRouteCachingEnabled()
{
$user = ImplicitBindingModel::create(['name' => 'Dries']);
$user = ImplicitBindingUser::create(['name' => 'Dries']);

config(['app.key' => str_repeat('a', 32)]);

Route::post('/user/{user}', function (ImplicitBindingModel $user) {
Route::post('/user/{user}', function (ImplicitBindingUser $user) {
return $user;
})->middleware(['web']);

Expand All @@ -97,13 +104,13 @@ public function testWithoutRouteCachingEnabled()

public function testSoftDeletedModelsAreNotRetrieved()
{
$user = ImplicitBindingModel::create(['name' => 'Dries']);
$user = ImplicitBindingUser::create(['name' => 'Dries']);

$user->delete();

config(['app.key' => str_repeat('a', 32)]);

Route::post('/user/{user}', function (ImplicitBindingModel $user) {
Route::post('/user/{user}', function (ImplicitBindingUser $user) {
return $user;
})->middleware(['web']);

Expand All @@ -114,13 +121,13 @@ public function testSoftDeletedModelsAreNotRetrieved()

public function testSoftDeletedModelsCanBeRetrievedUsingWithTrashedMethod()
{
$user = ImplicitBindingModel::create(['name' => 'Dries']);
$user = ImplicitBindingUser::create(['name' => 'Dries']);

$user->delete();

config(['app.key' => str_repeat('a', 32)]);

Route::post('/user/{user}', function (ImplicitBindingModel $user) {
Route::post('/user/{user}', function (ImplicitBindingUser $user) {
return $user;
})->middleware(['web'])->withTrashed();

Expand All @@ -131,13 +138,96 @@ public function testSoftDeletedModelsCanBeRetrievedUsingWithTrashedMethod()
'name' => $user->name,
]);
}

public function testEnforceScopingImplicitRouteBindings()
{
$user = ImplicitBindingUser::create(['name' => 'Dries']);
$post = ImplicitBindingPost::create(['user_id' => 2]);
$this->assertEmpty($user->posts);

config(['app.key' => str_repeat('a', 32)]);

Route::scopeBindings()->group(function () {
Route::get('/user/{user}/post/{post}', function (ImplicitBindingUser $user, ImplicitBindingPost $post) {
return [$user, $post];
})->middleware(['web']);
});

$response = $this->getJson("/user/{$user->id}/post/{$post->id}");

$response->assertNotFound();
}

public function testEnforceScopingImplicitRouteBindingsWithRouteCachingEnabled()
{
$user = ImplicitBindingUser::create(['name' => 'Dries']);
$post = ImplicitBindingPost::create(['user_id' => 2]);
$this->assertEmpty($user->posts);

$this->defineCacheRoutes(<<<PHP
<?php

use Illuminate\Tests\Integration\Routing\ImplicitBindingUser;
use Illuminate\Tests\Integration\Routing\ImplicitBindingPost;

Route::group(['scoping' => true], function () {
Route::get('/user/{user}/post/{post}', function (ImplicitBindingUser \$user, ImplicitBindingPost \$post) {
return [\$user, \$post];
})->middleware(['web']);
});
PHP);

$response = $this->getJson("/user/{$user->id}/post/{$post->id}");

$response->assertNotFound();
}

public function testWithoutEnforceScopingImplicitRouteBindings()
{
$user = ImplicitBindingUser::create(['name' => 'Dries']);
$post = ImplicitBindingPost::create(['user_id' => 2]);
$this->assertEmpty($user->posts);

config(['app.key' => str_repeat('a', 32)]);

Route::group(['scoping' => false], function () {
Route::get('/user/{user}/post/{post}', function (ImplicitBindingUser $user, ImplicitBindingPost $post) {
return [$user, $post];
})->middleware(['web']);
});

$response = $this->getJson("/user/{$user->id}/post/{$post->id}");
$response->assertOk();
$response->assertJson([
[
'id' => $user->id,
'name' => $user->name,
],
[
'id' => 1,
'user_id' => 2,
],
]);
}
}

class ImplicitBindingModel extends Model
class ImplicitBindingUser extends Model
{
use SoftDeletes;

public $table = 'users';

protected $fillable = ['name'];

public function posts()
{
return $this->hasMany(ImplicitBindingPost::class, 'user_id');
}
}

class ImplicitBindingPost extends Model
{
public $table = 'posts';

protected $fillable = ['user_id'];
}