Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for UUIDs and for configuring the model #14

Merged
merged 20 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .phpunit.cache/test-results
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":1,"defects":[],"times":{"Mchev\\Banhammer\\Tests\\Unit\\IPBannedMiddlewareTest::it_blocks_the_banned_ip":0.002,"Mchev\\Banhammer\\Tests\\Unit\\IPBanTest::test_ip_ban_with_metas":0.003,"Mchev\\Banhammer\\Tests\\Unit\\IPBanTest::test_ip_is_unbanned":0.003,"Mchev\\Banhammer\\Tests\\Unit\\IPBanTest::test_ip_is_banned":0.003,"Mchev\\Banhammer\\Tests\\Unit\\IPBanTest::test_multiple_ip_are_banned":0.002,"Mchev\\Banhammer\\Tests\\Unit\\IPBanTest::test_ip_ban_can_be_created":0.05,"Mchev\\Banhammer\\Tests\\Unit\\BlockByCountryMiddlewareTest::it_allows_request_from_non_blocked_country":0.001,"Mchev\\Banhammer\\Tests\\Unit\\BlockByCountryMiddlewareTest::it_blocks_request_from_blocked_country":0.001,"Mchev\\Banhammer\\Tests\\Unit\\BlockByCountryMiddlewareTest::it_allows_request_when_cache_is_present":0.007,"Mchev\\Banhammer\\Tests\\Unit\\BlockByCountryMiddlewareTest::it_allows_request_when_country_check_fails":0.023}}
{"version":1,"defects":{"Mchev\\Banhammer\\Tests\\Unit\\IPBannedMiddlewareTest::it_blocks_the_banned_ip":8,"Mchev\\Banhammer\\Tests\\Unit\\IPBanTest::test_ip_is_banned":8,"Mchev\\Banhammer\\Tests\\Unit\\IPBanTest::test_ip_ban_with_metas":8,"Mchev\\Banhammer\\Tests\\Unit\\IPBanTest::test_multiple_ip_are_banned":8,"Mchev\\Banhammer\\Tests\\Unit\\IPBanTest::test_ip_ban_can_be_created":8,"Mchev\\Banhammer\\Tests\\Unit\\IPBanTest::test_ip_is_unbanned":8},"times":{"Mchev\\Banhammer\\Tests\\Unit\\IPBannedMiddlewareTest::it_blocks_the_banned_ip":0.001,"Mchev\\Banhammer\\Tests\\Unit\\IPBanTest::test_ip_ban_with_metas":0,"Mchev\\Banhammer\\Tests\\Unit\\IPBanTest::test_ip_is_unbanned":0,"Mchev\\Banhammer\\Tests\\Unit\\IPBanTest::test_ip_is_banned":0.021,"Mchev\\Banhammer\\Tests\\Unit\\IPBanTest::test_multiple_ip_are_banned":0,"Mchev\\Banhammer\\Tests\\Unit\\IPBanTest::test_ip_ban_can_be_created":0,"Mchev\\Banhammer\\Tests\\Unit\\BlockByCountryMiddlewareTest::it_allows_request_from_non_blocked_country":0.007,"Mchev\\Banhammer\\Tests\\Unit\\BlockByCountryMiddlewareTest::it_blocks_request_from_blocked_country":0.001,"Mchev\\Banhammer\\Tests\\Unit\\BlockByCountryMiddlewareTest::it_allows_request_when_cache_is_present":0,"Mchev\\Banhammer\\Tests\\Unit\\BlockByCountryMiddlewareTest::it_allows_request_when_country_check_fails":0.006}}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `banhammer` will be documented in this file.

## v2.3.0 - 2024-05-24

- Added UUID support

## v2.2.0 - 2024-03-12

- Laravel 11 support
Expand Down
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ php artisan vendor:publish --tag="banhammer-config"

It is possible to define the table name and the fallback_url in the `config/ban.php` file.

You can publish the migration files with:

```bash
php artisan vendor:publish --tag="banhammer-migrations"
```

## Upgrading To 2.0 from 1.x

To upgrade to Banhammer version 2.0, follow these simple steps:
Expand Down Expand Up @@ -108,6 +114,50 @@ class User extends Authenticatable
```
> You can add the Bannable trait on as many models as you want (Team, Group, User, etc.).

## Using UUIDs

To use UUIDs make sure you publish and edit the migration files.

```diff
- $table->id();
+ $table->uuid('id');
```

You will then need to make a model that extends `Mchev\Banhammer\Models\Ban`:

```php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Mchev\Banhammer\Models\Ban as BanhammerBan;

class Ban extends BanhammerBan
{
use HasUuids;

protected $keyType = 'string';
}
```
> Although most of the methods needed are already available from the base model, you can add any additional methods here.

Finally update the published `ban.php` config file to load the model you have created:

```diff
/*
|--------------------------------------------------------------------------
| Model Name
|--------------------------------------------------------------------------
|
| Specify the model which you want to use as your Ban model.
|
*/

- 'model' => \Mchev\Banhammer\Models\Ban::class,
+ 'model' => \App\Models\YouBanClass::class,
```

### Ban / Unban

Simple ban
Expand Down
11 changes: 11 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@

'table' => 'bans',

/*
|--------------------------------------------------------------------------
| Model Name
|--------------------------------------------------------------------------
|
| Specify the model which you want to use as your Ban model.
|
*/

'model' => \Mchev\Banhammer\Models\Ban::class,

/*
|--------------------------------------------------------------------------
| Where to Redirect Banned Users
Expand Down
4 changes: 1 addition & 3 deletions database/migrations/create_bans_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
return new class extends Migration {
/**
* Run the migrations.
*/
Expand All @@ -20,7 +19,6 @@ public function up(): void
$table->timestamp('expired_at')->nullable();
$table->softDeletes();
$table->timestamps();

$table->index('ip');
$table->index('expired_at');
});
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" executionOrder="random" failOnWarning="true" failOnRisky="true" failOnEmptyTestSuite="true" beStrictAboutOutputDuringTests="true" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="Mchev Test Suite">
<testsuite name="uuid">
<directory>tests</directory>
</testsuite>
</testsuites>
Expand Down
4 changes: 2 additions & 2 deletions src/Banhammer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ class Banhammer
{
public static function unbanExpired(): void
{
Ban::expired()->delete();
config('ban.model')::expired()->delete();
Cache::put('banned-ips', IP::banned()->pluck('ip')->toArray());
}

public static function clear(): void
{
Ban::onlyTrashed()->forceDelete();
config('ban.model')::onlyTrashed()->forceDelete();
}
}
10 changes: 4 additions & 6 deletions src/BanhammerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ class BanhammerServiceProvider extends ServiceProvider
*/
public function boot(): void
{
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');

Ban::observe(BanObserver::class);
config('ban.model')::observe(BanObserver::class);

$router = $this->app->make(Router::class);
$router->aliasMiddleware('auth.banned', AuthBanned::class);
Expand All @@ -41,9 +39,9 @@ public function boot(): void
], 'banhammer-config');

// Publishing migrations
// $this->publishes([
// __DIR__.'/../database/migrations' => database_path('migrations'),
// ], 'banhammer-migrations');
$this->publishes([
__DIR__.'/../database/migrations' => database_path('migrations'),
], 'banhammer-migrations');

// Registering package commands.
$this->commands([
Expand Down
3 changes: 1 addition & 2 deletions src/Events/ModelWasBanned.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
namespace Mchev\Banhammer\Events;

use Illuminate\Contracts\Queue\ShouldQueue;
use Mchev\Banhammer\Models\Ban;

class ModelWasBanned implements ShouldQueue
{
public function __construct(public $model, public Ban $ban)
public function __construct(public $model, public $ban)
{
}
}
8 changes: 4 additions & 4 deletions src/IP.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static function ban(string|array $ips, array $metas = [], string $date =

foreach ((array) $ips as $ip) {
if (! in_array($ip, $bannedIps)) {
Ban::create([
config('ban.model')::create([
'ip' => $ip,
'metas' => count($metas) ? $metas : null,
'expired_at' => $date,
Expand All @@ -26,20 +26,20 @@ public static function ban(string|array $ips, array $metas = [], string $date =
public static function unban(string|array $ips): void
{
$ips = (array) $ips;
Ban::whereIn('ip', $ips)->delete();
config('ban.model')::whereIn('ip', $ips)->delete();
Cache::put('banned-ips', self::banned()->pluck('ip')->toArray());
}

public static function isBanned(string $ip): bool
{
return Ban::where('ip', $ip)
return config('ban.model')::where('ip', $ip)
->notExpired()
->exists();
}

public static function banned(): Builder
{
return Ban::whereNotNull('ip')
return config('ban.model')::whereNotNull('ip')
->with('createdBy')
->notExpired();
}
Expand Down
8 changes: 4 additions & 4 deletions src/Observers/BanObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class BanObserver
{
public function creating(Ban $ban): void
public function creating($ban): void
{
$user = auth()->user();
if ($user && is_null($ban->created_by_type) && is_null($ban->created_by_id)) {
Expand All @@ -21,19 +21,19 @@ public function creating(Ban $ban): void
}
}

public function created(Ban $ban): void
public function created($ban): void
{
event(new ModelWasBanned($ban->bannable(), $ban));
$this->updateCachedIps($ban);
}

public function deleted(Ban $ban): void
public function deleted($ban): void
{
event(new ModelWasUnbanned($ban->bannable()));
$this->updateCachedIps($ban);
}

public function updateCachedIps(Ban $ban): void
public function updateCachedIps($ban): void
{
if ($ban->ip) {
Cache::put('banned-ips', IP::banned()->pluck('ip')->unique()->toArray());
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/Bannable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait Bannable
*/
public function bans(): MorphMany
{
return $this->morphMany(Ban::class, 'bannable');
return $this->morphMany(config('ban.model'), 'bannable');
}

/**
Expand Down
Loading