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

[5.4] Handle model instance in Authorize middleware #17898

Merged
merged 1 commit into from
Feb 12, 2017
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
5 changes: 4 additions & 1 deletion src/Illuminate/Auth/Middleware/Authorize.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Factory as Auth;

class Authorize
Expand Down Expand Up @@ -70,7 +71,9 @@ protected function getGateArguments($request, $models)
}

return collect($models)->map(function ($model) use ($request) {
return $this->getModel($request, $model);
return $model instanceof Model
?$model
:$this->getModel($request, $model);
})->all();
}

Expand Down
22 changes: 22 additions & 0 deletions tests/Auth/AuthorizeMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,28 @@ public function testModelAuthorized()
$this->assertEquals($response->content(), 'success');
}

public function testModelInstanceAsParameter()
{
$instance = m::mock(\Illuminate\Database\Eloquent\Model::class);

$this->gate()->define('success', function ($user, $model) use ($instance) {
$this->assertSame($model, $instance);

return true;
});

$request = m::mock(Request::class);

$nextParam = null;

$next = function ($param) use (&$nextParam) {
$nextParam = $param;
};

(new Authorize($this->container->make(Auth::class), $this->gate()))
->handle($request, $next, 'success', $instance);
}

/**
* Get the Gate instance from the container.
*
Expand Down