Restricting Customer Grid Access for Magento Referrers
In this guide, we’ll walk through how to restrict access to the customer grid for Magento referrer users based on a specific customer attribute called customer_referrer_id. We’ll use Magento 1.4.1.1 as our base version.
Overview of the Requirement
The existing setup includes:
- An additional customer attribute named
customer_referrer_id. - A custom role called ‘referrer’ with access to the customer and order grids only.
- The need for each referrer user to see only those customers in the grid who have their
customer_referrer_idset as the logged-in referrer’s ID.
Understanding the Solution Approach
To achieve this, we’ll be overriding two core Magento blocks:
Adminhtml/Block/Customer/Grid::_prepareCollection: For the customer grid.Adminhtml/Block/Sales/Order/Grid::_prepareCollection: For the order grid.
We’ll focus on the customer grid override in this post for brevity, but the approach is similar for orders.
Setting Up the Module
First, ensure your module declaration file (app/etc/modules/Myproject_Adminhtml) is correctly configured:
<?xml version="1.0"?>
<config>
<modules>
<Myproject_Adminhtml>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Sales />
</depends>
</Myproject_Adminhtml>
</modules>
</config>
Configuring the Module
In your local/Myproject/Adminhtml/etc/config.xml, configure the block overrides:
<config>
<modules>
<Myproject_Adminhtml>
<version>1.0.0</version>
</Myproject_Adminhtml>
</modules>
<global>
<blocks>
<adminhtml>
<rewrite>
<customer_grid>Myproject_Adminhtml_Block_Customer_Grid</customer_grid>
</rewrite>
</adminhtml>
</blocks>
</global>
</config>
Overriding the Customer Grid Block
Create a new file in local/Myproject/Adminhtml/Block/Customer/Grid.php:
class Myproject_Adminhtml_Block_Customer_Grid extends Mage_Adminhtml_Block_Customer_Grid
{
protected function _prepareCollection()
{
// Prepare the base customer collection
$collection = Mage::getResourceModel('customer/customer_collection')
->addNameToSelect()
->addAttributeToSelect('email')
->addAttributeToSelect('created_at')
->addAttributeToSelect('group_id')
->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');
// Set the collection and retrieve the logged-in referrer's ID
$this->setCollection($collection);
$referrer_id = Mage::getSingleton('admin/session')->getUser()->getId();
// Restrict the collection to customers with a matching `customer_referrer_id`
if ($referrer_id) {
$collection->addAttributeToFilter('customer_referrer_id', $referrer_id);
}
return parent::_prepareCollection();
}
}
How It Works
The overridden _prepareCollection method first prepares the base customer collection with relevant attributes and joins. It then sets this collection for the block.
Next, it retrieves the ID of the currently logged-in user using Magento’s session management. If a referrer is logged in (i.e., $referrer_id is not empty), it adds an attribute filter to the collection: customer_referrer_id = $referrer_id.
This effectively restricts the customer grid data to only those customers whose customer_referrer_id matches the ID of the currently logged-in referrer.
Conclusion
By following this guide, you should now have a solid understanding of how to restrict access to the Magento customer grid based on custom attributes and user roles. This approach allows for fine-grained control over user permissions within your store’s admin interface.
Remember that while I’ve focused on the customer grid in this post, you can apply the same logic to other grids by overriding the relevant collection preparation methods.
For more advanced customizations or if you encounter any issues, feel free to reach out to our experienced development team at Laravel Company. We’re always ready to help you optimize your Magento store’s functionality.