Laravel 5.1 阻止使用 Bican 角色的用户

Laravel 5.1 Block users using Bican roles

我在想,如何通过 this 包来禁止他人...

所以我目前正在使用 Laravel 5.1 并且我正在尝试 'ban' 来自我网站的用户。 我有一个名为 'banned' 的 table,它具有以下结构:

    +---------------+--------------+------------+-------------+------------------+----------------+--+
    | TABLE_NAME | COLUMN_NAME | COLUMN_DEFAULT | IS_NULLABLE | DATA_TYPE | CHARACTER_MAXIMUM_LENGTH | 
    +---------------+--------------+------------+-------------+------------------+----------------+--+
    | banned     | id          | NULL           | NO          | int       | NULL                     |
    | banned     | user_id     | NULL           | NO          | int       | NULL                     |
    | banned     | banned_by   | NULL           | NO          | int       | NULL                     |
    | banned     | reason      | NULL           | NO          | varchar   |                      255 |
    | banned     | expires     | NULL           | NO          | datetime  | NULL                     |
    | banned     | lifted      | NULL           | YES         | datetime  | NULL                     |
    | banned     | lifted_by   | NULL           | YES         | int       | NULL                     |
    +---------------+--------------+------------+-------------+------------------+----------------+--+

角色(bican roles)的标准结构我也有。

现在,我希望能够使用 'banned' table.

中的数据向我的被禁用户显示自定义被禁视图

执行此操作的最佳方法是什么?

如果您添加一个新的中间件并在您的控制器中调用用户可以访问该页面的地方,它将检查他们是否被禁止

BannedMiddleware.php

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class BannedMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (!Auth::guest() && Auth::user()->is('banned')) {
            return view('BannedPageView');
        }

        return $next($request);
    }
}

并在 protected $routeMiddleware 下编辑您的 kernal.php 添加此

'isBanned' => \App\Http\Middleware\BannedMiddleware::class,

然后在你的控制器中添加这个

public function __construct()
     {
         $this->middleware('isBanned');
     }

这将检查他们是否在点击该控制器中的任何路线时被禁止。

编辑


为每个请求全局检查所有内容:

制作相同的中间件并放置此代码:

<?php

namespace App\Http\Middleware;

use Closure;
use Auth;
use App\User;
use DB;
use Carbon\Carbon;

class CheckBanMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (Auth::check()) {
            if (User::CheckBan()) {
                $bandata = DB::table('banned')->where('user_id', '=', Auth::id())->where('lifted', '=', Null)->where('expires', '>=', Carbon::now())->first();
                return response()->view('banned', ['bandata' => $bandata]);
            }
        }
        return $next($request);
    }
}

在您的 User.php 中创建一个新函数:

public static function CheckBan()
    {
        return DB::table('banned')->where('user_id', '=', Auth::id())->where('lifted', '=', Null)->where('expires', '>=', Carbon::now())->exists();
    }

这个功能是因为我有另一种方法来存储禁令等...

将以下行添加到 protected $middleware 数组中的 app/http/kernel.php

\App\Http\Middleware\CheckBanMiddleware::class,

这样可以在每次请求之前检查数据。

大功告成!