Laravel禁止/不活动用户中间件问题

时间:2017-05-22 14:23:06

标签: php laravel

我遇到了创建中间件以将禁止(或在本例中为“非活动”)用户重定向到特殊视图的问题。它似乎根本不起作用,因此必然会缺少一些东西。

中间件

namespace App\Http\Middleware;

use Closure;
use Auth;

class NotInactive extends Auth
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
      if (Auth::user()->isInactive() )
    {

       return redirect('/inactive');

    }
    else{
        return $next($request);
    }
    }
 }

Kernel.php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware    \ConvertEmptyStringsToNull::class,
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
            \App\Http\Middleware\NotInactive::class,

        ],

        'api' => [
            'throttle:60,1',
            'bindings',
        ],

    'admin' => [
      \App\Http\Middleware\EncryptCookies::class,
      \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
      \Illuminate\Session\Middleware\StartSession::class,
      \Illuminate\View\Middleware\ShareErrorsFromSession::class,
      \App\Http\Middleware\VerifyCsrfToken::class,
      \App\Http\Middleware\Admin::class,
    ]

    ];

    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'notinactive' => \App\Http\Middleware\NotInactive::class,
    ];
}

user.php的

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'id', 'first_name', 'last_name', 'middle', 'email', 'status', 'role'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token'
    ];

    // Gets all of the skills the user knows
    public function skills()
    {
        return $this->belongsToMany('App\Skill')->withPivot('skill_level');
    }

    // Gets all of the roles this user holds
    public function roles()
    {
     return $this->belongsToMany('App\Role', 'role_user');   
    }

    public function isAdmin()
    {
      return $this->roles()->where('name', 'Admin')->exists();   
    }

    public function isInactive()
    {
      return $this->roles()->where('name', 'Inactive')->exists();
    }
}

有什么想法我错过了什么或我哪里出错了?但它并没有吐出任何错误。

1 个答案:

答案 0 :(得分:0)

您不应在web组中添加新的中间件。这将导致无限重定向,因为Web的每个路由都将使用此中间件组。而是将新路由添加到您的身份验证或管理组,如果不将其手动添加到您希望使用新中间件保护的路由。另外,请确保排除从中间件重定向非活动用户的路由。