Laravel策略:代码更改被忽略.是否有任何要清除的策略缓存? [英] Laravel policies : code change is ignored. Is there any policy cache to clear?

查看:61
本文介绍了Laravel策略:代码更改被忽略.是否有任何要清除的策略缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在全职工作2年.我经常每隔几个月遇到一次该错误,但是从来没有以一种可靠的方式对其进行修复,因为它只是消失了,而且我也永远找不到为什么".

I'm working full time on an application for 2 years. I encounter this bug regularily, let's say every couple months, but never manage to fix it in a reliable way because it just disappears and I never get to find the "why".

那么,又来了,我不知道为什么以及如何解决它.这次,我正在写一些东西,以便下次有一个我喜欢的页面.

So, here it is again, and I have no clue why and how to fix it. This time, I'm writing something so I'll have a page to favorite for the next time.

这是错误:
如果我对策略方法进行了任何更改(例如在app/Policies/UserPolicy中),则在刀片中使用@can或在php文件中使用$ user-> can时,不会考虑此更改.我可以在策略的开始部分引入 die ,或者在开始时引入 Log :: debug('something')甚至是 return false 该函数,但不,仍然返回true.

Here is the bug:
If I make any change to a policy method, for instance in app/Policies/UserPolicy, the change is not taken into account when using @can in a blade or $user->can in a php file. I can introduce a die in the policy, a Log::debug('something') or even a return false at the very start of the function, but nope, still returning true.

这是一个代码示例:
文件:app/Policies/UserPolicy

Here is a code sample:
File : app/Policies/UserPolicy

public function deleteUser(User $user, User $target)
{
    return false;
    if ($user->id === $target->id) {
        return false;
    }
// [...]
}

这是代码测试,无论我在策略代码中做什么,它都会返回true:

Here is the code testing, it returns true, whatever I do in the policy code:

$me = Auth::user();
dd($me->can('deleteUser', $me));

最初,此示例应返回false,但返回true,但我不知道为什么.修改代码不会改变任何事情,就像有一个无法清除的缓存一样.我已经尝试了所有我知道的缓存清除命令:

Originally this example should return false, but it's returning true and I don't know why. Modifying the code does not change a thing, it's like there is a cache that nothing can clear. I've tried all the cache clearing commands I know:

  • php artisan cache:clear
  • php artisan config:clear
  • php artisan view:clear
  • php artisan明确编译
  • composer dump-autoload

即使重新启动了Apache,依此类推...我检查了php.ini,我没有启用OPCache(注释行,但是我也尝试过OPcache.enabled = 0,没有任何变化).

Even restarted apache, and so on... I checked php.ini, I don't have OPCache enabled (line commented, but I tried with OPcache.enabled=0 too, no changes).

也许原因在别处,但我不知道在哪里看.就像我说的那样,这个错误通常会自行消失,而不会给我时间去寻找原因.

Maybe the reason is elsewhere but I don't know where to look. As I said, this bug usually disappear by itself without leaving me the time to find the cause.

重现该错误的其他方式
在刀片中,如果我写:

Other way to reproduce the bug
In a blade, if I write:

@can('deleteUser', $user)
  CAN
@endcan

它始终显示CAN.例如,如果我将策略文件中的函数重命名为deleteUserr,则没有任何变化(仍然返回true).但是,如果我将刀片服务器代码更改为 @can('deleteUserr',$ user),那么我就不会显示"CAN",因为找不到此功能并且未找到规则的结果总是错误的.

It always display CAN. If I rename the function in the policy file to deleteUserr for instance, nothing changes (still returns true). However, if I change the blade code to @can('deleteUserr', $user) then I don't have the "CAN" displayed, as this function is not found and the result for unfound rule is alwways false.

环境
WSL(Ubuntu 18.04,Apache 2.4.29,php 7.2.19),Laravel 6.0.3

Environment
WSL (Ubuntu 18.04, apache 2.4.29, php 7.2.19), Laravel 6.0.3

感谢您的帮助!

编辑/已解决:找到了罪魁祸首!

与作曲者包 spatie/laravel-permission 的交互不良.
我有一个名为"deleteUser"的spatie权限,并且已被授予.该程序包可能已经重载了-> can"方法,现在在进行策略路由之前首先检查其权限机制.因此,我的UserPolicy @ deleteUser只是被忽略了.

It is a bad interaction with the composer package spatie/laravel-permission.
I have a spatie permission that is name "deleteUser" and is granted. The package has probably overloaded the "->can" method and now checks first in its permissions mechanism before going on the policy route. So my UserPolicy@deleteUser is simply ignored.

推荐答案

这是我发现的原因:

与作曲家包spatie/laravel-permission的交互不良.

It is a bad interaction with the composer package spatie/laravel-permission.

我有一个spatie权限,名称为"deleteUser"并被授予.该程序包可能已经重载了-> can"方法,现在在进行策略路由之前先检查其权限机制.
授予权限"deleteUser"后,将忽略UserPolicy @ deleteUser.

I have a spatie permission that is name "deleteUser" and is granted. The package has probably overloaded the "->can" method and now checks first in its permissions mechanism before going on the policy route.
As the permission "deleteUser" is granted, the UserPolicy@deleteUser is simply ignored.

这篇关于Laravel策略:代码更改被忽略.是否有任何要清除的策略缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆