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.
BanService
.User
model, any Eloquent model could be banned.ban
and unban
.First, pull in the package through Composer:
$ composer require cybercog/laravel-ban
The package will automatically register itself. This step required for Laravel 5.4 or earlier releases only.
Include the service provider within app/config/app.php
:
'providers' => [
Cog\Laravel\Ban\Providers\BanServiceProvider::class,
],
At last you need to publish and run database migrations:
$ php artisan vendor:publish --provider="Cog\Laravel\Ban\Providers\BanServiceProvider" --tag="migrations"
$ php artisan migrate
use Cog\Contracts\Ban\Bannable as BannableContract;
use Cog\Laravel\Ban\Traits\Bannable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements BannableContract
{
use Bannable;
}
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.
$ 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');
});
}
}
$user->ban();
$user->ban([
'comment' => 'Enjoy your ban!',
]);
$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:
$user->ban([
'expired_at' => '+1 month',
]);
$user->unban();
On unban
all related ban models are soft deletes.
$user->isBanned();
$user->isNotBanned();
app(\Cog\Contracts\Ban\BanService::class)->deleteExpiredBans();
$ban = $user->ban();
$ban->isPermanent(); // true
Or pass null
value.
$ban = $user->ban([
'expired_at' => null,
]);
$ban->isPermanent(); // true
$ban = $user->ban([
'expired_at' => '2086-03-28 00:00:00',
]);
$ban->isTemporary(); // true
$users = User::withoutBanned()->get();
$users = User::withBanned()->get();
$users = User::onlyBanned()->get();
To apply query scopes all the time you can define shouldApplyBannedAtScope
method in bannable model. If method returns true
all banned models will be hidden by default.
use Cog\Contracts\Ban\Bannable as BannableContract;
use Cog\Laravel\Ban\Traits\Bannable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements BannableContract
{
use Bannable;
/**
* Determine if BannedAtScope should be applied by default.
*
* @return bool
*/
public function shouldApplyBannedAtScope()
{
return true;
}
}
If entity is banned \Cog\Laravel\Ban\Events\ModelWasBanned
event is fired.
Is entity is unbanned \Cog\Laravel\Ban\Events\ModelWasUnbanned
event is fired.
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\Laravel\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',
]);
If you want force logout banned user on protected routes access, use LogsOutBannedUser
middleware instead:
protected $routeMiddleware = [
'logs-out-banned-user' => \Cog\Laravel\Ban\Http\Middleware\LogsOutBannedUser::class,
]
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.
Please see CHANGELOG for more information on what has changed recently.
Please see UPGRADING for detailed upgrade instructions.
Please see CONTRIBUTING for details.
Run the tests with:
$ vendor/bin/phpunit
If you discover any security related issues, please email open@cybercog.su instead of using the issue tracker.
Anton Komarev |
badr aldeen shek salim |
Rick Mac Gillis |
AnsellC |
Joe Archer |
---|---|---|---|---|
Francisco Solis |
Jakub Adamec |
Laravel Ban
package is open-sourced software licensed under the MIT License by Anton Komarev.Fat Boss In Jail
image licensed under Creative Commons 3.0 by Gan Khoon Lay.CyberCog is a Social Unity of enthusiasts. Research best solutions in product & software development is our passion.
前言: 基于 Laravel 5.2 版本 环境要求:PHP 5.5.9+,MySQL 5.1+ Laravel 官网地址: http://www.golaravel.com/ 官方学习教程地址: http://www.golaravel.com/post/2016-ban-laravel-xi-lie-ru-men-jiao-cheng-yi/ 一、安装composer 如果还未安装 com
#PHP环境搭建与Laravel部署 先安装xampp,这里只需要安装其中的apache与mysql,安装后如果不能正常运行apache与mysql的端口号与httpd-ssl.conf的443(一共三个,改下面两个)端口号都要更改。 下载Composer的Windows版本,安装,其中选择php.exe在xampp/php下 现在在xampp/htdocs下,cmd如下指令 composer c
with(['testB.testD','testC']) 预查询嵌套预查询 使用. 拼接 with(['testB.testD']) 预查询 条件 使用whereHas, 但必须是with存在的才会生效 $grid = new Grid(new TestA()); $grid->model()->with(['testB.testD','testC'])->whereHas('testB',
Laravel 是一套简洁、优雅的PHP Web开发框架(PHP Web Framework)。它可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁、富于表达力。 功能特点 1、语法更富有表现力 你知道下面这行代码里 “true” 代表什么意思么? $uri = Uri::create(‘some/uri’, array(), array(), tr
我需要空间/Laravel权限的帮助。当我试图分配它给我错误哎呀,看起来像出了问题。 错误 Connection.php第761行中的QueryExcema:SQLSTATE[23000]:完整性约束冲突:1048列role_id不能为空(SQL:插入到(,)值(9,))
Laravel 作为现在最流行的 PHP 框架,其中的知识较多,所以单独拿出来写一篇。 简述 Laravel 的生命周期 Laravel 采用了单一入口模式,应用的所有请求入口都是 public/index.php 文件。 注册类文件自动加载器 : Laravel通过 composer 进行依赖管理,无需开发者手动导入各种类文件,而由自动加载器自行导入。 创建服务容器:从 bootstrap/ap
简介 Laravel Scout 为 Eloquent 模型 全文搜索提供了简单的,基于驱动的解决方案。通过使用模型观察者,Scout 会自动同步 Eloquent 记录的搜索索引。 目前,Scout 自带一个 Algolia 驱动;不过,编写自定义驱动很简单, 你可以轻松的通过自己的搜索实现来扩展 Scout。 安装 首先,通过 Composer 包管理器来安装 Scout: composer
简介 Laravel 致力于让整个 PHP 开发体验变得愉快, 包括你的本地开发环境。 Vagrant 提供了一种简单,优雅的方式来管理和配置虚拟机。 Laravel Homestead 是一个官方预封装的 Vagrant box,它为你提供了一个完美的开发环境,而无需在本地机器安装 PHP 、Web 服务器和其他服务器软件。不用担心会搞乱你的操作系统!Vagrant boxes 是一次性的。如果
WebStack-Laravel 一个开源的网址导航网站项目,具备完整的前后台,您可以拿来制作自己的网址导航。 部署 克隆代码: git clone https://github.com/hui-ho/WebStack-Laravel.git 安装依赖: composer installphp artisan key:generate 编辑配置: cp .env.example .env ...D