Skip to content

Instantly share code, notes, and snippets.

@mooror
Created September 2, 2018 20:42
Show Gist options
  • Save mooror/2e4e7a37894cbfd9f4ef30d3b6c98749 to your computer and use it in GitHub Desktop.
Save mooror/2e4e7a37894cbfd9f4ef30d3b6c98749 to your computer and use it in GitHub Desktop.
<?php
namespace Sitelease\PaypalAutoSeller\Extensions;
use SilverStripe\ORM\DataExtension;
use SilverStripe\ORM\ArrayList;
use SilverStripe\View\ArrayData;
use Sitelease\PaypalAutoSeller\Model\PrivateProduct;
use Sitelease\PaypalAutoSeller\Model\Organization;
use Sitelease\PaypalAutoSeller\Model\Tracker;
use Sitelease\PaypalAutoSeller\Model\Order;
use Sitelease\PaypalAutoSeller\Model\Offer;
class PaypalAutoSellerMemberExtension extends DataExtension
{
//...
public static function hasPivateProductAccess($product, $userID = false)
{
// Default product access
$productAccess = false;
$productID = $product->ID;
// Get the users record
if ($userID) {
$userRecord = Member::get()->byID($userID);
}else{
$userRecord = Member::currentUser();
}
// Get a list of all the products this user has direct access to
if ($userRecord->relField("ProductsForClient")) {
$userProductRelationship = $userRecord->ProductsForClient()->byID($productID);
if ($userProductRelationship) {
$productAccess = true;
}
}
// If product access was not explicitly granted to this user
// check to see if access was granted via an organization
if (!$productAccess) {
// If organizations are enabled on the product
if ($product->hasField("EnableOrganizations") &&
$product->EnableOrganizations){
// Get a list of all the organizations that member is in
if ($userRecord->relField("InOrganizations")) {
$organizations = $userRecord->InOrganizations();
// Loop over each organization and see
// if it grants access to the current product
foreach ($organizations as $organization) {
$productRelationship = $organization->ProductsForOrganization()->byID($productID);
if ($productRelationship) {
return true;
}
}
}
}
}
return $productAccess;
}
//...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment