spatie / laravel-authorize
用于检查授权的中间件
Requires
- php: >=5.5.0
- illuminate/contracts: ~5.1.11|~5.2.0
- laravel/framework: ~5.1.11|~5.2.0
Requires (Dev)
- orchestra/testbench: ~3.1.0|~3.2.0
- phpunit/phpunit: 4.*
- scrutinizer/ocular: ~1.1
This package is auto-updated.
Last update: 2024-09-21 18:31:31 UTC
README
用于检查授权的中间件
本包提供了一种路由中间件,用于保护路由免受未经授权的访问。它挂钩到Laravel 5.1.11中引入的授权功能。
可以通过向路由添加中间件来保护路由
Route::get('/top-secret-page', [ 'middleware' => 'can:viewTopSecretPage', 'uses' => 'TopSecretController@index', ]);
当然,此中间件也可以应用于多个路由
Route::group(['prefix' => 'admin', 'middleware' => 'can:viewAdmin'], function() { //all the controllers of your admin section ... });
此外,中间件可以使用路由模型绑定
Route::get('/post/{post}', [ 'middleware' => 'can:editPost,post', 'uses' => 'PostController@edit', ]);
Spatie是一家位于比利时的安特卫普的网络设计公司。您可以在我们的网站上找到我们所有开源项目的概述在这里。
支持我们
我们投入了大量资源来创建最佳开源包。您可以通过购买我们的付费产品之一来支持我们。
我们非常感谢您从您的家乡寄给我们明信片,说明您正在使用我们的哪个包。您可以在我们的联系页面上找到我们的地址。我们将所有收到的明信片发布在我们的虚拟明信片墙上。
明信片软件
您可以自由使用此包(它是MIT许可的),但如果它进入您的生产环境,您需要向我们寄送一张来自您家乡的明信片,说明您正在使用我们的哪个包。
我们的地址是:Spatie,Kruikstraat 22,2018 安特卫普,比利时。
最佳明信片将在我们的网站上开源页面发布。
不要在Laravel 5.2.28及以上版本中使用
Laravel 5.2.28或更高版本包含本包提供的中间件作为默认功能。在这些版本的Laravel中不需要安装此包。
安装
您可以通过composer安装此包
$ composer require spatie/laravel-authorize
接下来,您必须安装服务提供者
// config/app.php 'providers' => [ ... Spatie\Authorize\AuthorizeServiceProvider::class, ];
接下来,必须在kernel中注册\Spatie\Authorize\Middleware\Authorize::class
中间件
//app/Http/Kernel.php protected $routeMiddleware = [ ... 'can' => \Spatie\Authorize\Middleware\Authorize::class, ];
将中间件命名为can
只是一个建议。您可以给它任何您喜欢的名字。
authorize
中间件包括了标准auth
中间件提供的一切功能。因此,您也可以选择用本包提供的中间件替换App\Http\Middleware\Authenticate
中间件。
//app/Http/Kernel.php protected $routeMiddleware = [ 'auth' => 'Spatie\Authorize\Middleware\Authorize', ... ];
您可以使用以下命令发布配置文件
php artisan vendor:publish --provider="Spatie\Authorize\AuthorizeServiceProvider"
这是发布配置文件的内容
return [ /* * The path to redirect for login. */ 'login_url' => 'auth/login' ];
用法
检查认证
当中间件没有任何参数使用时,它只会允许登录用户使用路由。如果您打算这样使用中间件,我建议您用本包提供的中间件替换标准auth
中间件。
//only logged in users will be able to see this Route::get('/top-secret-page', ['middleware' => 'auth', 'uses' => 'TopSecretController@index']);
检查授权
中间件接受您定义的能力名称作为第一个参数
//only users with the viewTopSecretPage-ability be able to see this Route::get('/top-secret-page', [ 'middleware' => 'can:viewTopSecretPage', 'uses' => 'TopSecretController@index', ]);
使用表单模型绑定
假设您已经设置了如下能力
//inside the boot method of AuthServiceProvider $gate->define('update-post', function ($user, $post) { return $user->id === $post->user_id; });
中间件接受绑定模型的名称作为第二个参数。
Route::get('/post/{post}', [ 'middleware' => 'can:editPost,post', 'uses' => 'PostController@edit', ]);
在幕后,中间件会将绑定到当前轮次的模型绑定传递给定义的 update-post
能力。
未授权请求会发生什么?
默认行为
这是中间件中定义的默认行为。
use Symfony\Component\HttpKernel\Exception\HttpException; ... protected function handleUnauthorizedRequest($request, $ability = null, $model = null) { if ($request->ajax()) { return response('Unauthorized.', Response::HTTP_UNAUTHORIZED); } if (!$request->user()) { return redirect()->guest(config('laravel-authorize.login_url')); } throw new HttpException(Response::HTTP_UNAUTHORIZED, 'This action is unauthorized.'); }
因此,访客将被重定向到默认登录页面,已登录用户将收到状态为 HTTP_UNAUTHORIZED
即 401 的响应。
自定义行为
要自定义默认行为,您可以轻松扩展默认中间件并重写 handleUnauthorizedRequest
方法。别忘了在内核中注册您的类。
如果您想通知所有未授权用户您实际上是一个茶壶,您可以这样做。
//app/Http/Middleware/Authorize.php namespace App\Http\Middleware; use Spatie\Authorize\Middleware\Authorize as BaseAuthorize; use Symfony\Component\HttpFoundation\Response; class Authorize extends BaseAuthorize { protected function handleUnauthorizedRequest($request, $ability = null, $model = null) { return reponse('I am a teapot.', Response::HTTP_I_AM_A_TEAPOT); } }
在内核中
//app/Http/Kernel.php protected $routeMiddleware = [ 'can' => 'App\Http\Middleware\Authorize', ... ];
变更日志
有关最近更改的详细信息,请参阅 CHANGELOG。
测试
此包包含由 orchestral/testbench 驱动的集成测试。
您可以使用以下命令运行所有测试
$ composer test
贡献
有关详细信息,请参阅 CONTRIBUTING。
安全性
如果您发现与安全相关的错误,请发送电子邮件至 security@spatie.be 而不是使用问题跟踪器。
鸣谢
感谢 Joseph Silber 在此包创建过程中提供的所有优秀反馈。
关于Spatie
Spatie 是一家位于比利时安特卫普的网页设计公司。您可以在我们的网站上找到我们所有开源项目的概述 在这里。
许可证
MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件。