【问题标题】:Laravel 5.3 policies how to redirect unauthorized usersLaravel 5.3 策略如何重定向未授权用户
【发布时间】:2016-09-17 14:11:25
【问题描述】:

我在文档中找不到它。如何重定向未授权用户?

RolePolicy.php

class RolePolicy
{
    use HandlesAuthorization;

    public function manageRoles(User $user)
    {
        return $user->isAdmin();
    }
}

RolesController.php

function __construct()
{
    $this->authorize('manageRoles', Role::class);
}

提前致谢

【问题讨论】:

  • 为什么它会被否决? :(怎么了?

标签: php acl laravel-5.3


【解决方案1】:

您可以修改文件app\Exceptions\Handler.php

关于渲染功能:

public function render($request, Exception $e)
{

    /**modified part**/  
    if ($request->wantsJson()) {
        return response([
            'success' => false,
            'message' => $e->getMessage()
        ], 404);
    }

    if ($e instanceof AuthorizationException) {
        return redirect('path');

        //or simply
        return view('errors.forbidden');
        //but this will return an OK, 200 response.
    }
    /**end of modified part**/

    return parent::render($request, $e);
}

如果要设置 403,请使用辅助函数 response()。 您可以在此处查看回复文档https://laravel.com/docs/master/responses

基本上,您可以使用该解决方案来玩更多选项。但最简单的方法是创建一个视图文件:errors/403.blade.php,当您遇到未经授权的异常时,该视图将自动加载。同样适用于 404 not found,只需创建 404.blade.php

【讨论】:

    【解决方案2】:

    据我所知,这在 Laravel 5.3 中与在任何版本的 Laravel 5 中没有什么不同。

    有一个名为auth 的路由中间件引用App\Http\Middleware\Authenticate(在app/http/Kernel.php 中定义)

    在这个课程中:

    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->guest()) {
            if ($request->ajax()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect()->guest('login');
            }
        }
    
        return $next($request);
    }
    

    这个auth 中间件可以应用于需要身份验证的路由。

    在此处阅读有关中间件的更多信息:https://laravel.com/docs/5.3/middleware

    在此处阅读有关身份验证的更多信息:https://laravel.com/docs/5.3/authentication

    【解决方案3】:

    在您的控制器方法中使用 Laravel Gates。例如:

    public function update(Role $role){
    
    if(\Gates::allows('manageRoles',$role)) {
    
    return redirect()->back()->with('status','Success');
    
    }
    

    但是,我个人认为为每个控制器操作设置重定向页面太麻烦了。如果操作被拒绝,因为用户操纵了 url 而不是因为某些先决条件尚未完成,那么带有主页按钮的直接 404 页面就足够了。

    就像上面的答案所说,使用 Laravel 响应,调用您想要的错误页面并传入自定义消息会更好、更容易。

    喜欢另一个帖子的这个答案:

    return response("User can't perform this action.", 401);
    

    https://stackoverflow.com/a/38611737/6131033

    【讨论】:

      【解决方案4】:

      我实现了一些逻辑,希望对某人有所帮助。
      一、调用方法例如$this->anonymouslyAuthorize($model)
      在您的例如 PostController 中的方法中

      public function show(Post $post)
      {
          $this->anonymouslyAuthorize($post);
      
          // The user is authorized.
      }
      

      在控制器目录中的基本控制器中添加此方法。

      public function anonymouslyAuthorize($model)
      {
          Auth::user()->can('see', $model);
      }
      

      在启动方法中的AuthServiceProvider中定义这个Gate。

      public function boot()
      {
          $this->registerPolicies();
      
          Gate::define('see', function ($user, $model) {
              if ($user->id !== $model->user_id) {
                  abort(404);
              }
          });
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-02-05
        • 1970-01-01
        • 2019-05-04
        • 1970-01-01
        • 1970-01-01
        • 2019-10-21
        • 2020-06-26
        相关资源
        最近更新 更多