Skip to content

laravel package to add prohibition system to your application

License

Notifications You must be signed in to change notification settings

walidbtz7/LaravelProhibition

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Prohibition

laravel package to add prohibition system to your application

Installation

composer require joodek/laravel-prohibition 

Configurations

Add this middleware to your web middlewares group in app/Http/Kernel.php

Joodek\Prohibition\Http\Middleware\BlockBannedUsersMiddleware::class;

make sure to add it exactly to app/Http/Kernel::$middlewareGroup property in web key , otherwise it won't work,

publish the configurations using

php artisan vendor:publish --provider="Joodek\Prohibition\ProhibitionServiceProvider"

migrate the table

php artisan migrate

Usage

this package provides two prohibition phases, you can either ban the User model, or using the ip address, both using the same syntax and the same features

User model

to enable the model prohibition , you should use the Bannable trait on your User model like this :

use Joodek\Prohibition\Bannable;

class User extends Authenticatable
{
    use Bannable;
}

this will give you all you need to ban the user, here is an example

use App\Models\User;

$user = new User(['name' => 'foo', 'email' => 'baz']);

$user->ban()

this way the user will be banned forever , and will recieve 403 Forbidden error if he tries to access your application, you can customize that if you want , see the Restriction section section to see how,

its rare scynario to ban your registered users forever, often you want to only ban a user for a period of time, you can do that using one of the following available methods,

$user->banForMinutes($minutes = 1);

$user->banForHours($hours = 1);

$user->banForDays($days = 1);

$user->banForWeeks($weeks = 1);

$user->banForMonths($months = 1);

$user->banForYears($years = 1);

you can also check if the user is banned or not

$user->banned() // bool

you can also unban the users at any time using

$user->unban()

IP address

you can ban non registered users using the IP address, all you need available on the \Illuminate\Http\Request class, here is how :

using the dependency injection

use Illuminate\Http\Request;

/* ... */
    public function example_method(Request $request): View
    {
        $request->ban();
    }
/* ... */

using the helper method

request()->ban();

this will ban the current ip address forever, but you can customize the period like the following :

$request->banForMinutes($minutes = 1);

$request->banForHours($hours = 1);

$request->banForDays($days = 1);

$request->banForWeeks($weeks = 1);

$request->banForMonths($months = 1);

$request->banForYears($years = 1);

you can also check if the ip address is banned or not

$request->banned() // bool

you can also unban the ip at any time using

$request->unban()

Restriction

Banned users wether using the model or ip will by default recieve 403 Forbidden error when trying to access your application, but you can customize it using the error key in the config/prohibition.php config file, like this

/* ... */

"error" => [
        "code" => 403,
        "message" => "Forbidden !"
    ]
    
/* ... */

or you can disable the aborting at all, by setting the restriction to false in the config/prohibition.php config file,

/* ... */

 "restrict" => true,
    
/* ... */

and this way you should check if they're banned using the previous methods we've mentioned

Licence

this package is open source project under MIT licence,

Security

you can at anytime report any vulnerabilities or any security bugs to [email protected]

Contribution

all your feedback are welcome, if you noticed any improvement issues or need some new features that you think will raise our package quality, you can submit a pull request , or raise a new issue here on github or on my email [email protected] , I'll make sure to review all of them,

About

laravel package to add prohibition system to your application

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%