bouncer: Cross-request caching does not work as expected

Bouncer version: 1.0.0-rc3 Laravel version: 5.7.3

Hi @JosephSilber ,

I recently upgraded to Laravel 5.7 and I noticed that the cache does not work anymore. What I mean is that even with the cache enabled, Bouncer is still making queries to resolve the abilities at the Gate.

Here are some thoughts after my investigation:

  1. Bouncer’s service provider is registering and booting. The boot method calls registerAtGate, which resolves the Gate singleton and a Sliber\Bouncer\Clipboard instance, and then the Clipboard registers its before/after callbacks in the Gate, via the Clipboard::registerAt() method.

  2. In my code, I’m calling Bouncer::cache() in the AuthServiceProvider’s bootmethod. By calling the Bouncer facade, Laravel resolves the Bouncer singleton for the first time, which is created through the Silber\Bouncer\Factory. The factory uses a new Sliber\Bouncer\Clipboard instance, as well as the Gate singleton.

  3. When the create method of the factory is called, this Clipboard instance registers its before/after callbacks in the Gate, via the Clipboard::registerAt() method. Now the Gate has two before callbacks, and two after callbacks.

  4. Then, the cache method is called, which is instantiating a Silber\Bouncer\CachedClipboard instance with the before slot from the previous clipboard. The CachedClipboard instance is then bound into the container, and set to the Bouncer singleton.

So at this point the Bouncer instance has a CachedClipboard instance, but the Gate has two sets of callbacks which are bound on two Silber\Bouncer\Clipboard instances which are not being used by the Bouncer instance anymore. As a result, for each ability check the queries are performed twice, and the cache is never used.

However when I use the isA(), isAn() methods on the user, the CachedClipboard is being used.

I believe this issue is related to #345.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 3
  • Comments: 18 (9 by maintainers)

Most upvoted comments

I published RC4 this morning, with the same compatibility as RC3.

https://github.com/JosephSilber/bouncer/releases/tag/v1.0.0-rc.4

I have a fix in the works. Should be out in a few days.

Here’s a PR that fixes this issue #359.

If any of you want to test it, change your composer’s Bouncer dependency to dev-clipboard-fix and run composer update.

Please report back with your findings.