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 all 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
43 changes: 18 additions & 25 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,43 @@ jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: true
fail-fast: false
matrix:
php: [8.3, 8.2, 8.1, 8.0]
laravel: [11.*, 10.*, 9.*]
dependency-version: [prefer-lowest, prefer-stable]
laravel: ["^11.0", "^10.0", "^9.0"]
dependency-version: [prefer-stable]
include:
- laravel: 11.*
testbench: ^9.0
- laravel: 10.*
testbench: ^8.0
- laravel: 9.*
testbench: ^7.0
- laravel: "^11.0"
testbench: 9.*
- laravel: "^10.0"
testbench: 8.*
- laravel: "^9.0"
testbench: 7.*
exclude:
- laravel: 9.*
php: 8.3
- laravel: 10.*
- laravel: "^11.0"
php: 8.1
- laravel: "^11.0"
php: 8.0
- laravel: 11.*
- laravel: "^10.0"
php: 8.0
- laravel: 11.*
php: 8.1

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
extensions: curl, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, iconv
coverage: none

- name: Setup problem matchers
run: |
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "nesbot/carbon:>=2.62.1" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-progress
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "symfony/console:>=4.3.4" "mockery/mockery:^1.3.2" "nesbot/carbon:>=2.62.1" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction

- name: Execute tests
run: vendor/bin/phpunit
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea
.phpunit.result.cache
.phpunit.cache
build
composer.lock
coverage
Expand Down
1 change: 0 additions & 1 deletion .phpunit.cache/test-results

This file was deleted.

7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

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

## v2.3.0 - 2024-05-24

- Added UUID support @KieranLProctor
- Added Configurable model support @KieranLProctor

## v2.2.0 - 2024-03-12

- Laravel 11 support
Expand All @@ -22,7 +27,7 @@ This new version introduce the block by country middleware

## v1.2.0 - 2023-03-02

- Adding Metas (cutom properties) to bans.
- Adding Metas (cutom properties) to bans. @mepsd
- You may have to run `php artisan migrate` if you are upgrading from v1.1.x

## v1.1.5 - 2023-02-21
Expand Down
72 changes: 61 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ Banned models can have an expiration date and will be automatically unbanned usi
- [Scheduler](#scheduler)
- [Events](#events)
- [Miscellaneous](#misc)
6. [Testing](#testing)
7. [Changelog](#changelog)
8. [Roadmap / Todo](#roadmap--todo)
9. [Contributing](#contributing)
10. [Security Vulnerabilities](#security-vulnerabilities)
11. [Credits](#credits)
12. [License](#license)
6. [UUIDs](#uuids)
7. [Testing](#testing)
8. [Changelog](#changelog)
9. [Roadmap / Todo](#roadmap--todo)
10. [Contributing](#contributing)
11. [Security Vulnerabilities](#security-vulnerabilities)
12. [Credits](#credits)
13. [License](#license)

## Version Compatibility

Expand All @@ -48,19 +49,22 @@ You can install the package via composer:
composer require mchev/banhammer
```

Then run the migrations with:
Then publish and run the migrations with:

> To use UUIDs see [UUIDs](#uuids)

```bash
php artisan vendor:publish --provider="Mchev\Banhammer\BanhammerServiceProvider" --tag="migrations"
php artisan migrate
```

You can publish the config file with:

```bash
php artisan vendor:publish --tag="banhammer-config"
php artisan vendor:publish --provider="Mchev\Banhammer\BanhammerServiceProvider" --tag="config"
```

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

## Upgrading To 2.0 from 1.x

Expand All @@ -86,7 +90,7 @@ composer update mchev/banhammer
- Backup your previous configuration file located at `config/ban.php`.
- Force republish the new configuration using the command:
```bash
php artisan vendor:publish --tag="banhammer-config" --force
php artisan vendor:publish --provider="Mchev\Banhammer\BanhammerServiceProvider" --tag="config" --force
```
- Review the new configuration file and make any necessary edits.

Expand Down Expand Up @@ -337,6 +341,52 @@ Or you can use the command:
php artisan banhammer:clear
```

## UUIDs

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

```bash
php artisan vendor:publish --provider="Mchev\Banhammer\BanhammerServiceProvider" --tag="migrations"
```

```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;
}
```
> 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,
```

## Testing

```bash
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
1 change: 0 additions & 1 deletion database/migrations/create_bans_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public function up(): void
$table->timestamp('expired_at')->nullable();
$table->softDeletes();
$table->timestamps();

$table->index('ip');
$table->index('expired_at');
});
Expand Down
35 changes: 14 additions & 21 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
<?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">
<directory>tests</directory>
</testsuite>
</testsuites>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
<php>
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="MAIL_DRIVER" value="array"/>
</php>
<source>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Unit">
<directory>tests/Unit</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./src</directory>
<directory>src</directory>
</include>
</source>
</source>
<php>
<env name="APP_ENV" value="testing" />
<env name="DB_CONNECTION" value="sqlite" />
<env name="DB_DATABASE" value=":memory:" />
</php>
</phpunit>
48 changes: 0 additions & 48 deletions phpunit.xml.dist.bak

This file was deleted.

5 changes: 2 additions & 3 deletions src/Banhammer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
namespace Mchev\Banhammer;

use Illuminate\Support\Facades\Cache;
use Mchev\Banhammer\Models\Ban;

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();
}
}
13 changes: 5 additions & 8 deletions src/BanhammerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Mchev\Banhammer\Middleware\BlockByCountry;
use Mchev\Banhammer\Middleware\IPBanned;
use Mchev\Banhammer\Middleware\LogoutBanned;
use Mchev\Banhammer\Models\Ban;
use Mchev\Banhammer\Observers\BanObserver;

class BanhammerServiceProvider extends ServiceProvider
Expand All @@ -21,9 +20,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 @@ -38,12 +35,12 @@ public function boot(): void
// Publishing the config.
$this->publishes([
__DIR__.'/../config/config.php' => config_path('ban.php'),
], 'banhammer-config');
], 'config');

// Publishing migrations
// $this->publishes([
// __DIR__.'/../database/migrations' => database_path('migrations'),
// ], 'banhammer-migrations');
$this->publishes([
__DIR__.'/../database/migrations' => database_path('migrations'),
], '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)
{
}
}
Loading