Skip to content

Commit

Permalink
FEATURE: implement laravel ban functionality for user model
Browse files Browse the repository at this point in the history
  • Loading branch information
vinothkannans committed Nov 19, 2024
1 parent a35ad15 commit 67ee0cb
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 2 deletions.
6 changes: 5 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Models;

use Cog\Contracts\Ban\Bannable as BannableInterface;
use Cog\Laravel\Ban\Traits\Bannable;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
Expand All @@ -11,7 +13,7 @@
use Laravel\Jetstream\HasTeams;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
class User extends Authenticatable implements BannableInterface
{
use HasApiTokens;

Expand All @@ -22,6 +24,8 @@ class User extends Authenticatable
use Notifiable;
use TwoFactorAuthenticatable;

use Bannable;

/**
* The attributes that are mass assignable.
*
Expand Down
1 change: 1 addition & 0 deletions app/Providers/FortifyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function boot(): void
->orWhere('username', $request->email)->first();

if ($user &&
$user->isNotBanned() &&
Hash::check($request->password, $user->password)) {
return $user;
}
Expand Down
1 change: 1 addition & 0 deletions bootstrap/providers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
App\Providers\AppServiceProvider::class,
App\Providers\FortifyServiceProvider::class,
App\Providers\JetstreamServiceProvider::class,
Cog\Laravel\Ban\Providers\BanServiceProvider::class,
Vinkas\Discourse\ServiceProvider::class,
];
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"license": "MIT",
"require": {
"php": "^8.2",
"cybercog/laravel-ban": "^4.9",
"laravel/framework": "^11.9",
"laravel/jetstream": "^5.3",
"laravel/sanctum": "^4.0",
Expand Down
85 changes: 84 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions database/migrations/2017_03_04_000000_create_bans_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of Laravel Ban.
*
* (c) Anton Komarev <anton@komarev.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateBansTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::create('bans', function (Blueprint $table) {
$table->increments('id');
$table->morphs('bannable');
$table->nullableMorphs('created_by');
$table->text('comment')->nullable();
$table->timestamp('expired_at')->nullable();
$table->softDeletes();
$table->timestamps();

$table->index('expired_at');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::dropIfExists('bans');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->timestamp('banned_at')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('banned_at');
});
}
};

0 comments on commit 67ee0cb

Please sign in to comment.