Laravel政策总是错误的


雪浴冰灵
2025-03-25 11:26:21 (14天前)


() - > ID);
}
公共功能秀($ id)
{
$ user = User :: find($ id);
return view(‘user.show’,array(‘user’=> $ user,‘data’=> $ this-> data));
}

回报总是“假的”。呼叫相同

政策
</跨度>
形成

调节器
</跨度>
。我哪里出错了?
我试图让用户在Laravel 5.4中查看自己的个人资料。

UserPolicy.php

公共功能视图(User $ authUser,$ user)
{
返回true;
}

注册

政策
</跨度>
在AuthServiceProvider.php中

5 条回复
  1. 0# 张三岁 | 2019-08-31 10-32



    这是一种不同的方法,用户可以查看自己的个人资料。



    首先,我将为此创建一条路线




    1. Route::group([‘middleware => auth’], function() {
      Route::get(‘profile’, UserController@profile’);
      });

    2. </code>


    然后在

    profile

    我做的功能




    1. public function profile()
      {
      $user = Auth::user();
      return view(‘profile’, compact(‘user’));
      }

    2. </code>


    这样,用户只能自动查看自己的个人资料。



    现在,如果您想允许某些用户查看其他人的个人资料,那么您可以使用策略。为什么?因为我认为用户应该始终能够查看自己的个人资料。但并非所有用户都应查看其他用户个人资料。


  2. 1# afs-loliaholic | 2019-08-31 10-32



    回答我自己的问题感觉很奇怪,但是当我遇到没有后续问题的问题时,我讨厌它。



    所以经过双重检查后发现,如果我删除

    authorizeResource

    来自构造函数




    1. public function __construct()
      {
      $this->authorizeResource(User::class);
      }

    2. </code>


    并检查控制器功能的授权




    1. $this->authorize(‘view’,$user);

    2. </code>


    一切正常。
    当我补充说,我一定错过了这部分

    $user

    作为策略函数中的参数。因此,要查看的用户永远不会被传递

    authorizeResource

    方法。



    感谢大家花时间帮助我。


  3. 2# v-star*위위 | 2019-08-31 10-32



    当你添加




    1. public function __construct()
      {
      $this->authorizeResource(User::class);
      }

    2. </code>


    在您的控制器中,您必须编辑所有功能签名以使其与班级相匹配,例如你的节目签名必须改变

    public function show($id)



    public function show(User $user)



    之后它应该工作


  4. 3# 镜乃Kagamino | 2019-08-31 10-32




    解:
    </强>



    更改第二个参数

    @can( ‘view’, $user )



    @can( ‘view’, $subject )

    它会找到工作。




    为什么:
    </强>



    因为你做错了。




    1. public function view(User $user, $subject){
      return true;
      }

    2. </code>


    只需仔细查看策略视图方法,第一个参数是

    authenticated user

    要么

    current user

    第二个参数是

    $subject

    ,由于策略围绕模型组织授权逻辑。




    策略是围绕a组织授权逻辑的类
    特定的模型或资源。例如,如果您的应用程序是
    博客,您可能有Post模型和相应的PostPolicy
    授权用户操作,例如创建或更新帖子。




    如果你想进一步深入其中。




    https://github.com/laravel/framework/blob/5.3/src/Illuminate/Auth/Access/Gate.php#L353




    1. /**

      • Resolve the callback for a policy check.
        *
      • @param \Illuminate\Contracts\Auth\Authenticatable $user
      • @param string $ability
      • @param array $arguments
      • @return callable
        */
        protected function resolvePolicyCallback($user, $ability, array $arguments)
        {
        return function () use ($user, $ability, $arguments) {

      •  $instance = $this->getPolicyFor($arguments[0]);
      •  // If we receive a non-null result from the before method, we will return it
      •  // as the final result. This will allow developers to override the checks
      •  // in the policy to return a result for all rules defined in the class.
      •  if (method_exists($instance, 'before')) {
      •      if (! is_null($result = $instance->before($user, $ability, ...$arguments))) {
      •          return $result;
      •      }
      •  }
      •  if (strpos($ability, '-') !== false) {
      •      $ability = Str::camel($ability);
      •  }
      •  // If the first argument is a string, that means they are passing a class name
      •  // to the policy. We will remove the first argument from this argument list
      •  // because the policy already knows what type of models it can authorize.
      •  if (isset($arguments[0]) && is_string($arguments[0])) {
      •      array_shift($arguments);
      •  }
      •  if (! is_callable([$instance, $ability])) {
      •      return false;
      •  }
      •  return $instance->{$ability}($user, ...$arguments);
      • };
        }




    查看使用$ user调用方法的最后一行,并传递$ argument(在我们的示例中为Model)。




    Laravel Docs for Authorization / Policies


登录 后才能参与评论