Laravel 5.2某些用户的锁定控制器

Laravel 5.2某些用户的锁定控制器,laravel,laravel-5.2,laravel-routing,Laravel,Laravel 5.2,Laravel Routing,我想为所有用户锁定AccountsController,除了管理员用户的用户 例如: Auth::user()->滚动!='管理员“然后关闭帐户控制器 AccountsController构造代码: 这是一个完全有效的解决方案,但我已经切换到这种情况。门的使用更方便一点。我使用角色和权限以及hasRole方法来管理访问级别,但是如果您的系统很简单,您可以轻松地在用户模型上使用isAdmin方法来检查数据库中的标志 中间件 创建自定义中间件 AuthenticateAdmin.php <?p

我想为所有用户锁定AccountsController,除了管理员用户的用户

例如:

Auth::user()->滚动!='管理员“
然后关闭帐户控制器

AccountsController构造代码:

这是一个完全有效的解决方案,但我已经切换到这种情况。门的使用更方便一点。我使用角色和权限以及
hasRole
方法来管理访问级别,但是如果您的系统很简单,您可以轻松地在
用户
模型上使用
isAdmin
方法来检查数据库中的标志

中间件 创建自定义中间件

AuthenticateAdmin.php

<?php namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;

class AuthenticateAdmin {

  protected $auth;

  public function __construct(Guard $auth)
  {
    $this->auth = $auth;
  }

  public function handle($request, Closure $next)
  {
    if ($this->auth->user()->hasRole('admin'))
    {
     return $next($request);
    }
  }
}
protected $routeMiddleware = [
    'auth' => Middleware\Authenticate::class,
    'auth.admin' => Middleware\AuthenticateAdmin::class,
];
public function boot(GateContract $gate)
{
    parent::registerPolicies($gate);

    $gate->define('user-admin', function($user){
        return $user->hasRole('admin');
    });
}
public function show($slug)
{
  if (Gate::allows('user-admin')){
    return $yes;
  }
  return $no;
}
然后您可以在控制器中使用中间件

public function __construct()
{
  $this->middleware('auth.admin');
}
大门 使用添加的Gate,您可以在AuthServiceProvider中定义策略

AuthServiceProvider.php

<?php namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;

class AuthenticateAdmin {

  protected $auth;

  public function __construct(Guard $auth)
  {
    $this->auth = $auth;
  }

  public function handle($request, Closure $next)
  {
    if ($this->auth->user()->hasRole('admin'))
    {
     return $next($request);
    }
  }
}
protected $routeMiddleware = [
    'auth' => Middleware\Authenticate::class,
    'auth.admin' => Middleware\AuthenticateAdmin::class,
];
public function boot(GateContract $gate)
{
    parent::registerPolicies($gate);

    $gate->define('user-admin', function($user){
        return $user->hasRole('admin');
    });
}
public function show($slug)
{
  if (Gate::allows('user-admin')){
    return $yes;
  }
  return $no;
}
然后将其添加到控制器或任何需要的位置

Controller.php

<?php namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;

class AuthenticateAdmin {

  protected $auth;

  public function __construct(Guard $auth)
  {
    $this->auth = $auth;
  }

  public function handle($request, Closure $next)
  {
    if ($this->auth->user()->hasRole('admin'))
    {
     return $next($request);
    }
  }
}
protected $routeMiddleware = [
    'auth' => Middleware\Authenticate::class,
    'auth.admin' => Middleware\AuthenticateAdmin::class,
];
public function boot(GateContract $gate)
{
    parent::registerPolicies($gate);

    $gate->define('user-admin', function($user){
        return $user->hasRole('admin');
    });
}
public function show($slug)
{
  if (Gate::allows('user-admin')){
    return $yes;
  }
  return $no;
}

您应该创建自己的中间件来检查登录用户的角色。如果遇到问题,请尝试并发回您尝试过的内容。在控制器中使用
if(Auth::user()->roll==“Admin”)
如何?不建议您检查控制器对业务逻辑的权限(关注点的说明),但您可以这样做。。。你甚至可以限制每种方法。好吧,我相信类似的方法会奏效,试试看。