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

Conversation

shulard
Copy link
Contributor

@shulard shulard commented Feb 12, 2017

Related to #17848 issue.

This will help extending Middleware capabilities by giving it directly the Model to check.

Implementation example :

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Middleware\Authorize;

class AuthorizeCommand extends Authorize
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next, $ability = null, ...$models)
    {
        if ((null !== $command = $request->route('command')) &&
            $command instanceof Model
        ) {
            return parent::handle($request, $next, 'access', $command);
        }

        return $next($request);
    }
}

This will help extending Middleware capabilities by giving it directly the Model to check.

Implementation example :

```php
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Middleware\Authorize;

class AuthorizeCommand extends Authorize
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next, $ability = null, ...$models)
    {
        if ((null !== $command = $request->route('command')) &&
            $command instanceof Model
        ) {
            return parent::handle($request, $next, 'access', $command);
        }

        return $next($request);
    }
}
```
@taylorotwell taylorotwell merged commit 89b296b into laravel:5.4 Feb 12, 2017
@GrahamCampbell GrahamCampbell changed the title Handle model instance in Authorize middleware. [5.4] Handle model instance in Authorize middleware Feb 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants