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 ();
항상 쿼리 범위를 적용하려면 bannable 모델에서 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 ();
}
물론, 위 코드에서 사용된 시간은 예시일 뿐입니다. 자신의 취향에 맞게 조정하세요.
최근 변경된 사항에 대한 자세한 내용은 변경 로그를 참조하세요.
자세한 업그레이드 지침은 업그레이드를 참조하세요.
자세한 내용은 CONTRIBUTING을 참조하세요.
다음을 사용하여 테스트를 실행하세요.
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는 매니아들의 사회적 연합입니다. 제품 및 소프트웨어 개발 분야에서 최고의 솔루션을 연구하는 것이 우리의 열정입니다.