cata / laravel-prohibition
laravel 包,用于在您的应用程序中添加禁止系统
Requires
- php: ^8.1
- laravel/framework: ^10.0|^9.0
This package is not auto-updated.
Last update: 2024-09-25 02:47:44 UTC
README
laravel 包,用于在您的应用程序中添加禁止系统
安装
composer require cata/laravel-prohibition
配置
将此中间件添加到您的 app/Http/Kernel.php
中的 web 中间件组
Cata\Prohibition\Middleware\ProhibitionMiddleware::class;
确保将其添加到 app/Http/Kernel::$middlewareGroup
属性中的 web 键,否则将不起作用
使用以下命令发布配置
php artisan vendor:publish --provider="Cata\Prohibition\ServiceProvider" --tag="config"
迁移表
php artisan migrate
用法
此包提供两个禁止阶段,您可以选择禁止 User
模型或使用 ip
地址,两者都使用相同的语法和功能
用户模型
要启用模型禁止,您应该在您的 User 模型上使用 Bannable
特性,如下所示
use Cata\Prohibition\Bannable; class User extends Authenticatable { use Bannable; }
这将为您提供禁止用户所需的所有功能,以下是一个示例
use App\Models\User; $user = new User(['name' => 'foo', 'email' => 'baz']); $user->ban()
这样用户将被永久禁止,如果尝试访问您的应用程序,将收到 403 禁止错误,您可以根据需要自定义此功能,有关详细信息,请参阅 限制部分
永久禁止您的注册用户很少见,通常您只想暂时禁止用户,可以使用以下可用方法之一
$user->banForSeconds($seconds = 1); $user->banForMinutes($minutes = 1); $user->banForHours($hours = 1); $user->banForDays($days = 1); $user->banForWeeks($weeks = 1); $user->banForMonths($months = 1); $user->banForYears($years = 1);
您还可以检查用户是否被禁止
$user->banned() // bool
您还可以随时使用以下方法取消用户的禁止
$user->unban()
IP 地址
您可以使用 IP
地址禁止未注册用户,所有这些都在 \Illuminate\Http\Request
类上,以下是方法
使用依赖注入
use Illuminate\Http\Request; /* ... */ public function example_method(Request $request): View { $request->ban(); } /* ... */
使用辅助方法
request()->ban();
这将永久禁止当前 IP 地址,但您可以像以下那样自定义期限
$request->banForSeconds($seconds = 1); $request->banForMinutes($minutes = 1); $request->banForHours($hours = 1); $request->banForDays($days = 1); $request->banForWeeks($weeks = 1); $request->banForMonths($months = 1); $request->banForYears($years = 1);
您还可以检查 IP 地址是否被禁止
$request->banned() // bool
您还可以随时取消 IP 的禁止
$request->unban()
. . .
使用外观
对于模型
您可以使用 Prohibition
外观来实现相同的结果,它提供相同的功能,但语法不同,以下示例显示了如何使用外观禁止一个或多个用户
use Cata\Prohibition\Facades\Prohibition; Prohibition::banModel($user, now()->addMinute() );
banModel
接受两个参数,第一个参数可能是 User
实例、int
或 Collection
,第二个参数是可选的 \Illuminate\Support\Carbon
实例。
use Cata\Prohibition\Facades\Prohibition; use App\Models\User; $users = User::take(5)->get(); // you can ban collection of users Prohibition::banModel($users, now()->addHour() ); $user = $users->first(); // you can ban user by passing his model Prohibition::banModel($user, now()->addHour() ); // you can ban user by passing his id Prohibition::banModel($user->id, now()->addHour() ); // user id
如果第二个参数未提供或等于 null
,用户(们)将被永久禁止。
您还可以像以下那样检查用户是否被禁止
use Cata\Prohibition\Facades\Prohibition; // check if the user banned by passing his model Prohibition::banned(user: $user); // check if the user banned by passing his id Prohibition::banned(user: $user->id);
或取消一个或多个用户的禁止
use Cata\Prohibition\Facades\Prohibition; use App\Models\User; $user = User::first(); // you can unban user by passing his model Prohibition::unbanModel(user: $user); // you can unban user by passing his id Prohibition::unbanModel(user: $user->id); $users = User::take(5)->get(); // you can unban multiple users by passing a collection Prohibition::unbanModel(user: $users);
对于 IP
您可以使用 Prohibition
外观像以下那样禁止 IP 或多个 IP
use Cata\Prohibition\Facades\Prohibition; $ip = request()->ip(); Prohibition::banIP($ip, now()->addMinute() );
banIP
接受两个参数,第一个参数是 IP string
或 array
,第二个参数是可选的 \Illuminate\Support\Carbon
实例。
这意味着您可以进一步禁止多个 IP 地址
use Cata\Prohibition\Facades\Prohibition; $ips = ["123.45.6.7","123.45.6.7","123.45.6.7","123.45.6.7"]; Prohibition::banIP($ips, now()->addHour() );
对于这两种情况,如果第二个参数未提供或等于 null
,IP(们)将被永久禁止。
您还可以使用前面的方法检查 IP 是否被禁止
use Cata\Prohibition\Facades\Prohibition; Prohibition::banned(ip: $ip);
你可能已经注意到我们使用了相同的方法,因为禁止
方法接受两个参数,第一个是User
实例,第二个是IP地址,如果你想同时检查这两个参数,或者只检查其中一个,就像我们在示例中那样,你可以传递这两个参数。
你可以这样解除IP或IP数组的禁止:
use Cata\Prohibition\Facades\Prohibition; use App\Models\User; $ip = "123.45.6.7"; Prohibition::unbanIP($ip); // or array $ips = ["123.45.6.7","123.45.6.7","123.45.6.7","123.45.6.7"]; Prohibition::unbanIP($ips);
测试
你可以使用测试包来测试功能,要这样做,如果你还没有发布测试,你需要发布它们。
php artisan vendor:publish --tag="prohibition-tests"
这将把一个ProhibitionTest
克隆到你的测试目录中,并使用默认的Laravel语法来运行它。
限制
无论使用模型还是IP禁止的用户,在尝试访问你的应用程序时,默认情况下会收到403 禁止
错误,但你可以在config/prohibition.php
配置文件中使用error
键来自定义它,如下所示:
/* ... */ "error" => [ "code" => 403, "message" => "Forbidden !" ] /* ... */
或者,你可以在config/prohibition.php
配置文件中将restriction
设置为false
来禁用所有的中止操作,
/* ... */ "restrict" => true, /* ... */
这样,你应该使用我们之前提到的方法来检查他们是否被禁止。
许可
这个包是在MIT许可证下的开源项目,
安全性
你可以随时将任何漏洞或安全漏洞报告给yassinebenaide3@gmail.com
贡献
欢迎所有反馈,如果你注意到任何需要改进的问题或需要一些新的功能,你认为这将提高我们包的质量,你可以提交一个拉取请求,或者在这里的GitHub上或者通过我的邮箱yassinebenaide3@gmail.com 提出一个新问题,我将确保审查所有这些反馈。