### Install Banhammer Package Source: https://github.com/mchev/banhammer/blob/2.x/README.md Install the Banhammer package using Composer and publish/run migrations. ```bash composer require mchev/banhammer php artisan vendor:publish --provider="Mchev\Banhammer\BanhammerServiceProvider" --tag="migrations" php artisan migrate ``` -------------------------------- ### Setup Cron Job for Scheduler Source: https://github.com/mchev/banhammer/blob/2.x/README.md Ensure a cron job is running Laravel's scheduler to automatically delete expired bans. This example shows the command to add to your crontab. ```bash * * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1 ``` -------------------------------- ### Listen to ModelWasUnbanned Event Source: https://github.com/mchev/banhammer/blob/2.x/README.md Listen for the ModelWasUnbanned event to perform actions when a model is unbanned. This example logs the unban event. ```php Event::listen(ModelWasUnbanned::class, function ($event) { // User was unbanned Log::info("User {$event->ban->bannable->id} was unbanned"); }); ``` -------------------------------- ### Listen to ModelWasBanned Event Source: https://github.com/mchev/banhammer/blob/2.x/README.md Listen for the ModelWasBanned event to perform actions when a model is banned. This example logs the ban event. ```php use Mchev\Banhammer\Events\ModelWasBanned; use Mchev\Banhammer\Events\ModelWasUnbanned; Event::listen(ModelWasBanned::class, function ($event) { // User was banned Log::info("User {$event->ban->bannable->id} was banned"); }); ``` -------------------------------- ### Run Banhammer tests Source: https://github.com/mchev/banhammer/blob/2.x/README.md Execute the test suite for the Banhammer package using Composer. ```bash composer test ``` -------------------------------- ### Enable Country Blocking Configuration Source: https://github.com/mchev/banhammer/blob/2.x/README.md Enable country blocking by setting 'block_by_country' to true in your config/ban.php file. ```php 'block_by_country' => true, ``` -------------------------------- ### Publish Migrations for UUIDs Source: https://github.com/mchev/banhammer/blob/2.x/README.md Publish the Banhammer migrations using the vendor:publish Artisan command to prepare for UUID usage. ```bash php artisan vendor:publish --provider="Mchev\Banhammer\BanhammerServiceProvider" --tag="migrations" ``` -------------------------------- ### Specify Blocked Countries Configuration Source: https://github.com/mchev/banhammer/blob/2.x/README.md Define the list of countries to block by setting 'blocked_countries' in config/ban.php. ```php 'blocked_countries' => ['FR', 'ES', 'US'], ``` -------------------------------- ### Republish Banhammer configuration Source: https://github.com/mchev/banhammer/blob/2.x/README.md After updating the package, republish the configuration file. Backup your current config first and review/merge any custom settings after republishing. ```bash # Backup your current config cp config/ban.php config/ban.php.backup # Republish config php artisan vendor:publish --provider="Mchev\Banhammer\BanhammerServiceProvider" --tag="config" --force # Review and merge any custom settings ``` -------------------------------- ### Ban with Metas Configuration Source: https://github.com/mchev/banhammer/blob/2.x/README.md When banning a user or IP, you can include additional meta data such as the route name or user agent. ```php // When banning $user->ban([ 'metas' => [ 'route' => request()->route()->getName(), 'user_agent' => request()->header('user-agent') ] ]); IP::ban("8.8.8.8", [ 'reason' => 'spam', 'severity' => 'high' ]); ``` -------------------------------- ### Set Meta Data for Bans Source: https://github.com/mchev/banhammer/blob/2.x/README.md Use setMeta to store additional data with a ban. This data can be retrieved later using getMeta or checked with hasMeta. ```php // Set meta $ban->setMeta('username', 'Jane'); $ban->setMeta('reason', 'spam'); // Get meta $ban->getMeta('username'); // 'Jane' // Check if meta exists $ban->hasMeta('username'); // true // Remove meta $ban->forgetMeta('username'); ``` -------------------------------- ### Ban IP with Metadata Source: https://github.com/mchev/banhammer/blob/2.x/README.md Ban an IP address and associate metadata with the ban. ```php use Mchev\Banhammer\IP; // Ban with metadata IP::ban("8.8.8.8", [ 'reason' => 'spam', 'severity' => 'high' ]); ``` -------------------------------- ### Create Custom Ban Model with UUIDs Source: https://github.com/mchev/banhammer/blob/2.x/README.md Create a custom Ban model that extends BanhammerBan and uses the HasUuids trait to support UUIDs. ```php namespace App\Models; use Illuminate\Database\Eloquent\Concerns\HasUuids; use Mchev\Banhammer\Models\Ban as BanhammerBan; class Ban extends BanhammerBan { use HasUuids; } ``` -------------------------------- ### Register IP Banned Middleware in Kernel Source: https://github.com/mchev/banhammer/blob/2.x/README.md Register the IPBanned middleware globally in app/Http/Kernel.php to block IPs on all routes. ```php // Add to app/Http/Kernel.php: protected $middleware = [ // ... \Mchev\Banhammer\Middleware\IPBanned::class, ]; ``` -------------------------------- ### List Bans for a Model Source: https://github.com/mchev/banhammer/blob/2.x/README.md Retrieve all bans, expired bans, or active bans for a specific model. ```php // All bans for a model $bans = $user->bans()->get(); // Only expired bans $expired = $user->bans()->expired()->get(); // Active bans $active = $user->bans()->notExpired()->get(); ``` -------------------------------- ### Apply Auth Banned Middleware to Single Route Source: https://github.com/mchev/banhammer/blob/2.x/README.md Protect a single route by applying the 'auth.banned' middleware. ```php Route::get('/dashboard', [DashboardController::class, 'index']) ->middleware('auth.banned'); ``` -------------------------------- ### Ban Multiple IP Addresses Source: https://github.com/mchev/banhammer/blob/2.x/README.md Ban multiple IP addresses simultaneously using the IP facade. ```php use Mchev\Banhammer\IP; // Ban multiple IPs IP::ban(["8.8.8.8", "4.4.4.4", "1.1.1.1"]); ``` -------------------------------- ### Manually delete expired bans Source: https://github.com/mchev/banhammer/blob/2.x/README.md Run this Artisan command to manually delete expired bans from the system. ```bash php artisan banhammer:unban ``` -------------------------------- ### Unban Multiple IP Addresses Source: https://github.com/mchev/banhammer/blob/2.x/README.md Remove bans from multiple IP addresses simultaneously using the IP facade. ```php use Mchev\Banhammer\IP; // Unban multiple IPs IP::unban(["8.8.8.8", "4.4.4.4"]); ``` -------------------------------- ### Ban IP with Expiration - Banhammer Source: https://github.com/mchev/banhammer/blob/2.x/CHANGELOG.md Use this to ban an IP address for a specified duration. Ensure the `now()` helper is available and the duration is correctly formatted. ```php IP::ban("8.8.8.8", [], now()->addMinutes(10)); ``` -------------------------------- ### List All Banned IPs Source: https://github.com/mchev/banhammer/blob/2.x/README.md Retrieve a collection or array of all currently banned IP addresses. ```php use Mchev\Banhammer\IP; // Get all banned IPs $ips = IP::banned()->get(); // Collection $ips = IP::banned()->pluck('ip')->toArray(); // Array ``` -------------------------------- ### Permanently delete all expired bans Source: https://github.com/mchev/banhammer/blob/2.x/README.md Use this Artisan command to permanently delete all expired bans. This action is irreversible. ```bash php artisan banhammer:clear ``` -------------------------------- ### Apply Auth Banned Middleware to Route Group Source: https://github.com/mchev/banhammer/blob/2.x/README.md Apply the 'auth.banned' middleware to a group of routes. ```php Route::middleware(['auth.banned'])->group(function () { Route::get('/profile', [ProfileController::class, 'index']); Route::get('/settings', [SettingsController::class, 'index']); }); ``` -------------------------------- ### Filter Bans by Meta Data Source: https://github.com/mchev/banhammer/blob/2.x/README.md Filter bans based on specific meta data using whereMeta or the shorthand whereBansMeta. This allows for targeted retrieval of ban records. ```php // Find bans with specific meta IP::banned()->whereMeta('username', 'Jane')->get(); $user->bans()->whereMeta('reason', 'spam')->get(); User::whereBansMeta('username', 'Jane')->get(); ``` -------------------------------- ### Update Ban Model Configuration for UUIDs Source: https://github.com/mchev/banhammer/blob/2.x/README.md Update the 'model' configuration in config/ban.php to point to your custom Ban model that supports UUIDs. ```php // config/ban.php 'model' => \App\Models\Ban::class, ``` -------------------------------- ### Ban IP with Expiration Source: https://github.com/mchev/banhammer/blob/2.x/README.md Ban an IP address with a specified expiration time. ```php use Mchev\Banhammer\IP; // Ban with expiration IP::ban("8.8.8.8", [], now()->addMinutes(10)); ``` -------------------------------- ### Advanced User Ban Options Source: https://github.com/mchev/banhammer/blob/2.x/README.md Ban a user with additional details like comment, IP, expiration, creator, and metadata. ```php $user->ban([ 'comment' => "You've been evil", 'ip' => "8.8.8.8", 'expired_at' => Carbon::now()->addDays(7), 'created_by_type' => 'App\Models\Admin', 'created_by_id' => auth()->id(), 'metas' => [ 'route' => request()->route()->getName(), 'user_agent' => request()->header('user-agent') ] ]); ``` -------------------------------- ### Ban Single IP Address Source: https://github.com/mchev/banhammer/blob/2.x/README.md Ban a single IP address using the IP facade. ```php use Mchev\Banhammer\IP; // Ban single IP IP::ban("8.8.8.8"); ``` -------------------------------- ### Ban a User Source: https://github.com/mchev/banhammer/blob/2.x/README.md Ban a user model. Bans are permanent by default. ```php $user->ban(); ``` -------------------------------- ### Update Banhammer package Source: https://github.com/mchev/banhammer/blob/2.x/README.md After updating composer.json, run this command to update the Banhammer package. ```bash composer update mchev/banhammer ``` -------------------------------- ### Ban User with Expiration Source: https://github.com/mchev/banhammer/blob/2.x/README.md Ban a user model until a specified time. ```php $user->banUntil('2 days'); ``` -------------------------------- ### Retrieve Unbanned Models - Banhammer Source: https://github.com/mchev/banhammer/blob/2.x/CHANGELOG.md When retrieving banned models, you can now pass `false` as a boolean parameter to include unbanned models in the results. This is useful for checking the status of all models. ```php Model::banned(false) ``` -------------------------------- ### Programmatically permanently delete expired bans Source: https://github.com/mchev/banhammer/blob/2.x/README.md Use this static method to permanently delete expired bans programmatically. Import the Banhammer facade before use. ```php use Mchev\Banhammer\Banhammer; // Permanently delete expired bans Banhammer::clear(); ``` -------------------------------- ### Change Scheduler Frequency Configuration Source: https://github.com/mchev/banhammer/blob/2.x/README.md Customize the frequency of the banhammer:unban command by setting 'scheduler_periodicity' in config/ban.php or via environment variable. Available frequencies include 'everyMinute', 'hourly', 'daily', etc. ```php // config/ban.php 'scheduler_periodicity' => 'everyFiveMinutes', // or 'hourly', 'daily', etc. ``` ```env BANHAMMER_SCHEDULER_PERIODICITY=everyFiveMinutes ``` -------------------------------- ### Unban a User Source: https://github.com/mchev/banhammer/blob/2.x/README.md Remove the ban from a user model. ```php $user->unban(); ``` -------------------------------- ### Update composer.json for Banhammer v2.0 Source: https://github.com/mchev/banhammer/blob/2.x/README.md To upgrade to Banhammer version 2.0, update your composer.json file to require the new version. ```json { "require": { "mchev/banhammer": "^2.0" } } ``` -------------------------------- ### Unban Single IP Address Source: https://github.com/mchev/banhammer/blob/2.x/README.md Remove a ban from a single IP address using the IP facade. ```php use Mchev\Banhammer\IP; // Unban single IP IP::unban("8.8.8.8"); ``` -------------------------------- ### Edit Migration for UUIDs Source: https://github.com/mchev/banhammer/blob/2.x/README.md Modify the published migration file to use 'uuid' instead of 'id' for the primary key. ```diff - $table->id(); + $table->uuid('id'); ``` -------------------------------- ### Query Banned Users Source: https://github.com/mchev/banhammer/blob/2.x/README.md Retrieve all banned users using the `banned` query scope. ```php // Get banned users $bannedUsers = User::banned()->get(); // Get non-banned users $activeUsers = User::notBanned()->get(); // Alternative syntax $activeUsers = User::banned(false)->get(); ``` -------------------------------- ### Programmatically delete expired bans Source: https://github.com/mchev/banhammer/blob/2.x/README.md Call this static method in your PHP code to delete expired bans. Ensure the Banhammer facade is imported. ```php use Mchev\Banhammer\Banhammer; // Delete expired bans Banhammer::unbanExpired(); ``` -------------------------------- ### Make Model Bannable Source: https://github.com/mchev/banhammer/blob/2.x/README.md Add the Bannable trait to your Eloquent model to enable banning functionality. ```php use Mchev\Banhammer\Traits\Bannable; class User extends Model { use Bannable; } ``` -------------------------------- ### Permanently delete all expired bans Source: https://github.com/mchev/banhammer/blob/2.x/README.md This command permanently deletes all expired bans from the system. ```APIDOC ## php artisan banhammer:clear ### Description Permanently deletes all expired bans. ### Command ```bash php artisan banhammer:clear ``` ``` -------------------------------- ### Manually delete expired bans Source: https://github.com/mchev/banhammer/blob/2.x/README.md This command manually deletes expired bans from the system. ```APIDOC ## php artisan banhammer:unban ### Description Manually deletes expired bans. ### Command ```bash php artisan banhammer:unban ``` ``` -------------------------------- ### Disable Automatic Scheduler Configuration Source: https://github.com/mchev/banhammer/blob/2.x/README.md Disable the automatic execution of the banhammer:unban command by setting 'scheduler_enabled' to false in config/ban.php or via environment variable. ```php // config/ban.php 'scheduler_enabled' => false, ``` ```env BANHAMMER_SCHEDULER_ENABLED=false ``` -------------------------------- ### Check if User is Banned Source: https://github.com/mchev/banhammer/blob/2.x/README.md Check if a user model is currently banned. ```php $user->isBanned(); ``` -------------------------------- ### Check if IP is Banned Source: https://github.com/mchev/banhammer/blob/2.x/README.md Check if a given IP address is currently banned. ```php use Mchev\Banhammer\IP; // Check if IP is banned IP::isBanned("8.8.8.8"); // bool ``` -------------------------------- ### Permanently delete expired bans (Programmatic) Source: https://github.com/mchev/banhammer/blob/2.x/README.md This method programmatically and permanently deletes expired bans. ```APIDOC ## Banhammer::clear() ### Description Permanently deletes expired bans programmatically. ### Method ```php Banhammer::clear(); ``` ``` -------------------------------- ### Delete expired bans (Programmatic) Source: https://github.com/mchev/banhammer/blob/2.x/README.md This method programmatically deletes expired bans. ```APIDOC ## Banhammer::unbanExpired() ### Description Deletes expired bans programmatically. ### Method ```php Banhammer::unbanExpired(); ``` ``` -------------------------------- ### Check if User is Not Banned Source: https://github.com/mchev/banhammer/blob/2.x/README.md Check if a user model is not currently banned. ```php $user->isNotBanned(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.