Giter Club home page Giter Club logo

laravel-ban's Introduction

cog-laravel-ban

Build Status StyleCI Releases License

Introduction

Laravel Ban simplify management of Eloquent model's ban. Make any model bannable in a minutes!

Use case is not limited to User model, any Eloquent model could be banned: Organizations, Teams, Groups and others.

Contents

Features

  • Designed to work with Laravel Eloquent models.
  • Using contracts to keep high customization capabilities.
  • Using traits to get functionality out of the box.
  • Model can has many bans.
  • Removed bans keeps in history as Soft deleted record.
  • Most part of the the logic is handled by the BanService.
  • Has middleware to prevent banned user route access.
  • Use case is not limited to User model, any Eloquent model could be banned.
  • Events firing on models ban and unban.
  • Covered with unit tests.

Installation

First, pull in the package through Composer:

$ composer require cybercog/laravel-ban

And then include the service provider within app/config/app.php:

'providers' => [
    Cog\Ban\Providers\BanServiceProvider::class,
],

At last you need to publish and run database migrations:

$ php artisan vendor:publish --provider="Cog\Ban\Providers\BanServiceProvider" --tag="migrations"
$ php artisan migrate

Usage

Prepare bannable model

use Cog\Ban\Contracts\HasBans as HasBansContract;
use Cog\Ban\Traits\HasBans;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements HasBansContract
{
    use HasBans;
}

Note: HasBans contract using CanBeOwner contract under the hood. If you are using cybercog/laravel-ownership package CanBeOwner contract could be omitted from bannable model.

Prepare bannable model database table

Bannable model must have nullable timestamp column named banned_at. This value used as flag and simplify checks if user was banned. If you are trying to make default Laravel User model to be bannable you can use example below.

Create a new migration file

$ php artisan make:migration add_banned_at_column_to_users_table

Then insert the following code into migration file:

<?php

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

class AddBannedAtColumnToUsersTable extends Migration
{
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->timestamp('banned_at')->nullable();
        });
    }
    
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('banned_at');
        });
    }
}

Available methods

Apply ban for the entity

$user->bans()->create([]);

$user->ban();

Apply ban for the entity with reason comment

$user->bans()->create([
    'comment' => 'Enjoy your ban!',
]);

$user->ban([
    'comment' => 'Enjoy your ban!',
]);

Apply ban for the entity which will be deleted over time

$user->bans()->create([
    'expired_at' => '+1 month',
]);

$user->ban([
    'expired_at' => '2086-03-28 00:00:00',
]);

expired_at attribute could be \Carbon\Carbon instance or any string which could be parsed by \Carbon\Carbon::parse($string) method.

Remove ban from entity

$user->unban();

On unban all related ban models are soft deletes.

Check if entity is banned

$user->isBanned();

Check if entity is not banned

$user->isNotBanned();

Delete expired bans manually

app(\Cog\Ban\Services\BanService::class)->deleteExpiredBans();

Events

If entity is banned \Cog\Ban\Events\ModelWasBanned event is fired.

Is entity is unbanned \Cog\Ban\Events\ModelWasUnbanned event is fired.

Middleware

This package has route middleware designed to prevent banned users to go to protected routes.

To use it define new middleware in $routeMiddleware array of app/Http/Kernel.php file:

protected $routeMiddleware = [
    'forbid-banned-user' => \Cog\Ban\Http\Middleware\ForbidBannedUser::class,
]

Then use it in any routes and route groups you need to protect:

Route::get('/', [
    'uses' => 'UsersController@profile',
    'middleware' => 'forbid-banned-user',
]);

Scheduling

After you have performed the basic installation you can start using the ban:delete-expired command. In most cases you'll want to schedule these command so you don't have to manually run it everytime you need to delete expired bans and unban models.

The command can be scheduled in Laravel's console kernel, just like any other command.

// app/Console/Kernel.php

protected function schedule(Schedule $schedule)
{
    $schedule->command('ban:delete-expired')->everyMinute();
}

Of course, the time used in the code above is just example. Adjust it to suit your own preferences.

Change log

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Testing

Run the tests with:

$ composer test

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

Alternatives

Feel free to add more alternatives as Pull Request.

License

  • Laravel Ban package is open-sourced software licensed under the MIT License.
  • Fat Boss In Jail image licensed under Creative Commons 3.0 by Gan Khoon Lay.

About CyberCog

CyberCog is a Social Unity of enthusiasts. Research best solutions in product & software development is our passion.

cybercog-logo

laravel-ban's People

Contributors

antonkomarev avatar lex111 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.