Laravel Ban 簡化了 Eloquent 模型禁令的管理。只需幾分鐘即可禁止任何模型!
用例不限於使用者模型,任何 Eloquent 模型都可能被禁止:組織、團隊、群組等。
BanService
處理。User
模型,任何 Eloquent 模型都可能被禁止。ban
並unban
模特兒的事件。首先,透過 Composer 拉入包:
composer require cybercog/laravel-ban
該包將自動註冊。僅 Laravel 5.4 或更早版本需要此步驟。
將服務提供者包含在app/config/app.php
中:
' providers ' => [
Cog Laravel Ban Providers BanServiceProvider::class,
],
最後,您需要發布並運行資料庫遷移:
php artisan vendor:publish --provider= " CogLaravelBanProvidersBanServiceProvider " --tag= " migrations "
php artisan migrate
use Cog Contracts Ban Bannable as BannableInterface ;
use Cog Laravel Ban Traits Bannable ;
use Illuminate Foundation Auth User as Authenticatable ;
class User extends Authenticatable implements BannableInterface
{
use Bannable;
}
Bannable 模型必須具有名為banned_at
的nullable timestamp
列。該值用作標誌並簡化檢查使用者是否被禁止。如果您嘗試將預設的 Laravel 使用者模型設為可禁止,您可以使用下面的範例。
php artisan make:migration add_banned_at_column_to_users_table
然後將以下程式碼插入到遷移文件中:
<?php
use Illuminate Database Migrations Migration ;
use Illuminate Database Schema Blueprint ;
use Illuminate Support Facades Schema ;
return new class extends Migration
{
public function up (): void
{
Schema:: table ( ' users ' , function ( Blueprint $ table ) {
$ table -> timestamp ( ' banned_at ' )-> nullable ();
});
}
public function down (): void
{
Schema:: table ( ' users ' , function ( Blueprint $ table ) {
$ table -> dropColumn ( ' banned_at ' );
});
}
};
$ user -> ban ();
$ user -> ban ([
' comment ' => ' Enjoy your ban! ' ,
]);
$ user -> ban ([
' expired_at ' => ' 2086-03-28 00:00:00 ' ,
]);
expired_at
屬性可以是CarbonCarbon
實例或任何可以透過CarbonCarbon::parse($string)
方法解析的字串:
$ user -> ban ([
' expired_at ' => ' +1 month ' ,
]);
$ user -> unban ();
unban
時所有相關封鎖模型都是軟刪除。
$ user -> isBanned ();
$ user -> isNotBanned ();
app ( Cog Contracts Ban BanService::class)-> deleteExpiredBans ();
$ ban = $ user -> ban ();
$ ban -> isPermanent (); // true
或傳遞null
值。
$ ban = $ user -> ban ([
' expired_at ' => null ,
]);
$ ban -> isPermanent (); // true
$ ban = $ user -> ban ([
' expired_at ' => ' 2086-03-28 00:00:00 ' ,
]);
$ ban -> isTemporary (); // true
$ users = User:: withoutBanned ()-> get ();
$ users = User:: withBanned ()-> get ();
$ users = User:: onlyBanned ()-> get ();
若要始終套用查詢範圍,您可以在可禁止模型中定義shouldApplyBannedAtScope
方法。如果方法傳回true
所有被禁止的模型將預設為隱藏。
use Cog Contracts Ban Bannable as BannableInterface ;
use Cog Laravel Ban Traits Bannable ;
use Illuminate Foundation Auth User as Authenticatable ;
class User extends Authenticatable implements BannableInterface
{
use Bannable;
/**
* Determine if BannedAtScope should be applied by default.
*
* @return bool
*/
public function shouldApplyBannedAtScope ()
{
return true ;
}
}
如果實體被禁止,則會觸發CogLaravelBanEventsModelWasBanned
事件。
實體是否被解除禁止CogLaravelBanEventsModelWasUnbanned
事件被觸發。
該軟體包具有路由中間件,旨在防止被禁止的用戶存取受保護的路由。
要使用它,請在app/Http/Kernel.php
檔案的$routeMiddleware
陣列中定義新的中間件:
protected $ routeMiddleware = [
' forbid-banned-user ' => Cog Laravel Ban Http Middleware ForbidBannedUser::class,
]
然後在您需要保護的任何路由和路由組中使用它:
Route:: get ( ' / ' , [
' uses ' => ' UsersController@profile ' ,
' middleware ' => ' forbid-banned-user ' ,
]);
如果您想在受保護的路由存取中強制註銷被禁止的用戶,請改用LogsOutBannedUser
中間件:
protected $ routeMiddleware = [
' logs-out-banned-user ' => Cog Laravel Ban Http Middleware LogsOutBannedUser::class,
]
執行基本安裝後,您可以開始使用ban:delete-expired
指令。在大多數情況下,您需要安排這些命令,這樣您就不必每次需要刪除過期的禁令和取消禁令模型時手動執行它。
該命令可以在 Laravel 的控制台核心中調度,就像任何其他命令一樣。
// app/Console/Kernel.php
protected function schedule ( Schedule $ schedule )
{
$ schedule -> command ( ' ban:delete-expired ' )-> everyMinute ();
}
當然,上面程式碼中使用的時間只是一個範例。調整它以適合您自己的喜好。
請參閱變更日誌以了解有關最近更改內容的更多資訊。
請參閱升級以取得詳細的升級說明。
詳細資訊請參閱貢獻。
使用以下命令執行測試:
vendor/bin/phpunit
如果您發現任何與安全相關的問題,請發送電子郵件至 [email protected],而不是使用問題追蹤器。
安東·科馬列夫 | 巴德爾·阿爾丁·謝克·薩利姆 | 里克·麥克·吉利斯 | 安塞爾C | 喬·阿切爾 |
---|---|---|---|---|
弗朗西斯科·索利斯 | 雅庫布·阿達梅克 | 伊利亞·拉扎列夫 | 澤歐奈特 |
Laravel Ban 貢獻者列表
Laravel Ban
套件是由 Anton Komarev 根據 MIT 許可證授權的開源軟體。Fat Boss In Jail
圖像由 Gan Khoon Lay 獲得 Creative Commons 3.0 許可。 CyberCog 是愛好者的社交聯盟。研究產品和軟體開發的最佳解決方案是我們的熱情。