易瑟恩-开发 / laravel-admin-users
在您的应用程序中定义和检查管理员用户!
v1.8.0
2024-03-25 07:26 UTC
Requires
- php: ^8.2
- illuminate/support: ^10.0|^11.0
Requires (Dev)
- orchestra/testbench: ^v9.0.1
- phpunit/phpunit: ^11.0.8
- spatie/macroable: ^2.0.0
README
这个小包添加了一个配置和用户模型特性,用于在您的应用程序中定义管理员。
安装
使用 Composer 安装此包
composer require snoeren-development/laravel-admin-users
要求
此包需要至少 PHP 8.2 和 Laravel 10。
使用
将 SnoerenDevelopment\AdminUsers\Adminable
特性添加到您的用户模型中,并使用 ADMINS=
环境变量定义管理员用户。该变量应包含您应用程序管理员的电子邮件地址列表,用逗号分隔。
使用中间件
此包附带中间件。该中间件检查用户是否是管理员。如果不是,中间件会抛出一个 401 未授权异常。
将中间件添加到 HTTP 内核中的 $routeMiddleware
列表以使用。
// Kernel.php 'admin' => \SnoerenDevelopment\AdminUsers\AdminMiddleware::class,
授予管理员所有权限
// AuthServiceProvider.php use Illuminate\Support\Facades\Gate; public function boot() { $this->registerPolicies(); Gate::before(function ($user, $ability) { if ($user->isAdmin()) { return true; } }); }
通过 Blade 使用权限门
{{-- Using the gate via the can-statement. --}} @can('admin') The current user is an admin! @else The current user is not an admin! @endcan {{-- Using the custom if-statement --}} @admin The current user is an admin! @else The current user is not an admin! @endadmin
发布配置文件
您可以使用 php artisan vendor:publish
命令将配置文件发布到您的项目。然后选择管理员包。
使用 MySQL 驱动器
发布并运行迁移,或者手动将 is_admin
列添加到您的用户表中。在您的 .env
文件中将驱动程序设置为 mysql
,使用 ADMINS_DRIVER=mysql
。
测试
composer test
致谢
许可证
MIT 许可证。有关更多信息,请参阅 LICENSE。