Closed jordanmkoncz closed 8 years ago
Take a look at TransactionManager::runAsAdmin($closure)
, or TransactionManager::runAs($closure, $user)
$owner = $this->owner;
singleton('TransactionManager')->runAsAdmin(function () use ($owner) {
$owner->decrementStock(a, b, c);
});
Is that useful?
Thanks for the suggestion, I'll give that a try. I'm guessing that this method would essentially require me to fork the SilverStripe module that makes the call to the decrementStock()
function just so that I can wrap the call to this function with silverstripe-restrictedobjects
TransactionManager
?
Hmm, yes most likely - unless there's an interim method you can hook into that will allow you to temporarily disable the restrictable checks via Restrictable::set_enabled(false);
I ended up forking the eComerce inventory module (https://github.com/milkyway-multimedia/ss-shop-inventory) and adding an additional extension point to decrementStock()
via $this->owner->extend()
before the write operations happen, to go along with the existing one (onDecrementStock
) that is called after the write operations happen. I then created a project-specific DataExtension
that applies to the same classes (Product
and ProductVariation
) and calls Restrictable::set_enabled(false)
in the first extension point and then Restrictable::set_enabled(true)
in the second extension point. That way I was able to avoid putting any code specific to silverstripe-restricedobjects
in my fork of the eCommerce inventory module.
Thanks for your suggestions!
I have a website that's using
silverstripe-restrictedobjects
, and I've run into an issue while trying to add eCommerce functionality to this website.The issue happens when a Member has just paid for a product with an external payment gateway and is redirected back to the website, at which point the website recognises the payment was successful and attempts to decrement the stock level of the purchased product.
When it does this,
silverstripe-restrictedobjects
catches the attempt to write to thisProduct
DataObject
inRestrictable->onBeforeWrite()
, and proceeds to check whether the currently logged inMember
has theWrite
permissions for thisProduct
DataObject
. Of course, theMember
does not have theWrite
permissions sosilverstripe-restrictedobjects
throws aPermissionDeniedException
, the user sees a Server Error page, and the attempt to decrement the purchased product's stock fails.What is the best way to resolve this issue? It's essentially a case where it's really the website itself that is trying to update the product, which it needs to be able to do, but
silverstripe-restrictedobjects
does not recognise this distinction and just checks whether the currently logged inMember
has permissions to perform thewrite
action that has been triggered.For testing purposes I confirmed that I could prevent the
PermissionDeniedException
by explicitly granting theWrite
permission for theProduct
to theMember
who is making the purchase, but I can't imagine the best solution here is to just give all usersWrite
permissions to allProduct
s.In case it helps, here's the full stack trace for the
PermissionDeniedException
:And here's the
decrementStock()
function that is causing thewrite
operation that is caught bysilverstripe-restrictedobjects
: