Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Checking the roles of admin user? 
 
maburoe
Jr. Member
 
Total Posts:  8
Joined:  2008-05-14
Kyoto, Japan
 

Hi,

How can I write the code to check if the specified role is same as the role which the admin user belongs to?

Here is what I’m trying to do:

1. Get the roles of admin user.
2. Check if the specified role(Let’s say “manufacturer") is in the roles.
3. If YES, get the catalog/product data which manufacture is equal to the admin user name.
4. If NO, get all the catalog/product data.

(I’m trying to create kind of B2B functionality where each manufacturer can create, edit, delete, seach only their own products.)

I have done on 3 and 4, but I don’t have a clue on 1 and 2.

Please Help.

 
Magento Community Magento Community
Magento Community
Magento Community
 
maburoe
Jr. Member
 
Total Posts:  8
Joined:  2008-05-14
Kyoto, Japan
 

I found the way, but a bit different from the procedure I posted.
(I’m overloading Mage_Adminhtml_Block_Catalog_Product_Grid::_prepareCollection.)

<?php
class Isupet_Manufacturer_Block_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid
{

    
protected function _prepareCollection()
    
{
        $store 
$this->_getStore();
        
$collection Mage::getModel('catalog/product')->getCollection()
            ->
addAttributeToSelect('sku')
            ->
addAttributeToSelect('name')
            ->
addAttributeToSelect('attribute_set_id')
            ->
addAttributeToSelect('type_id')
            ->
joinField('qty',
                
'cataloginventory/stock_item',
                
'qty',
                
'product_id=entity_id',
                
'{{table}}.stock_id=1',
                
'left');

        if (
$store->getId()) {
            
//$collection->setStoreId($store->getId());
            
$collection->addStoreFilter($store);
            
$collection->joinAttribute('custom_name''catalog_product/name''entity_id'null'inner'$store->getId());
            
$collection->joinAttribute('status''catalog_product/status''entity_id'null'inner'$store->getId());
            
$collection->joinAttribute('visibility''catalog_product/visibility''entity_id'null'inner'$store->getId());
            
$collection->joinAttribute('price''catalog_product/price''entity_id'null'left'$store->getId());
        
}
        
else {
            $collection
->addAttributeToSelect('price');
            
$collection->addAttributeToSelect('status');
            
$collection->addAttributeToSelect('visibility');
        
}
        
        
/*
         * Restrict data retrieval for manufacturer
         * (manufacturer can deal with only their own products.)
         */
        
        //Get logged in user
        
$user Mage::getSingleton('admin/session')->getUser();
        
        
//Get the role id of the user
        
$roleId implode(''$user->getRoles());
        
        
//Get the role name
        
$roleName Mage::getModel('admin/roles')->load($roleId)->getRoleName();
        
        
//Check if the user is 'manufacturer'
        
if ($roleName == 'manufacturer'{
            $collection
->addAttributeToSelect('manufacturer');
            
            
//Get the user's manufacture id
            //(suppose that the manufacturer name was set on the user's fisrt name attribute.)
            
$source $collection->getResource()->getAttribute('manufacturer')->getSource();
            
$optionId Mage::getSingleton('catalog/config')->getSourceOptionId($source$user->getFirstname());
            
            
//Retrieve the data which manufacturer name is equal to the user's manufacturer 
            
$collection->addAttributeToFilter('manufacturer'$optionId);
        
}
        
        $this
->setCollection($collection);
        
        
Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
        
$this->getCollection()->addWebsiteNamesToResult();
        return 
$this;
    
}

}

I still need to apply this customization on ‘create’, ‘edit’, ‘delete’… of catalog/product.
I’m not sure that I’m on the right track.

 
Magento Community Magento Community
Magento Community
Magento Community
 
schmelzgeist
Sr. Member
 
Total Posts:  92
Joined:  2008-05-27
Marzipane World Capital (Lübeck, Germany)
 

did you changed something else?  i’m getting only NULL whenever i use ‘$user = Mage::getSingleton(’admin/session’)->getUser();’

outside the backend, and i want to test for a logged in admin user in the frontend smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
maburoe
Jr. Member
 
Total Posts:  8
Joined:  2008-05-14
Kyoto, Japan
 

Sorry for this late reply.

I guess the code below only works when you are logged in the backend.

Mage::getSingleton('admin/session')->getUser();

If you wanna get user role data in the frontend, you can do such

$roleId Mage::getSingleton('customer/session')->getCustomerGroupId();
$role Mage::getSingleton('customer/group')->load($roleId)->getData('customer_group_code');

Hope this helps.

 
Magento Community Magento Community
Magento Community
Magento Community
 
BLIZZARD
Jr. Member
 
Total Posts:  21
Joined:  2008-12-15
Kharkov, Ukraine
 

How I can get user in frontend cotroller?

 
Magento Community Magento Community
Magento Community
Magento Community
 
joey santiago
Sr. Member
 
Total Posts:  76
Joined:  2009-02-25
 

this sounds veeeery interesting to me… but can’t actually make it work:

$optionId Mage::getSingleton('catalog/config')->getSourceOptionId($source$user->getFirstName());

returns me nothing and when i tried inserting:

print_r ($option);

in Mage/Catalog/Config.php, public function getSourceOptionId($source, $value)

it actually just prints

Array ( [label] => [value] => )

, like there wasn’t anything…

can you please give me a hint?

thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
 
arvillegas
Jr. Member
 
Total Posts:  1
Joined:  2010-11-16
 

hey maburoe how are you!! i need your help, when you say that you overloading
age_Adminhtml_Block_Catalog_Product_Grid::_prepareCollection. what you mean?

you overwrite the file Grid.php with you code?

or you make other file?? and from where you call this code??

o where ir the file that i have modificate to create this.?

regards!! and tnks

maburoe - 27 June 2008 12:40 AM

I found the way, but a bit different from the procedure I posted.
(I’m overloading Mage_Adminhtml_Block_Catalog_Product_Grid::_prepareCollection.)

<?php
class Isupet_Manufacturer_Block_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid
{

    
protected function _prepareCollection()
    
{
        $store 
$this->_getStore();
        
$collection Mage::getModel('catalog/product')->getCollection()
            ->
addAttributeToSelect('sku')
            ->
addAttributeToSelect('name')
            ->
addAttributeToSelect('attribute_set_id')
            ->
addAttributeToSelect('type_id')
            ->
joinField('qty',
                
'cataloginventory/stock_item',
                
'qty',
                
'product_id=entity_id',
                
'{{table}}.stock_id=1',
                
'left');

        if (
$store->getId()) {
            
//$collection->setStoreId($store->getId());
            
$collection->addStoreFilter($store);
            
$collection->joinAttribute('custom_name''catalog_product/name''entity_id'null'inner'$store->getId());
            
$collection->joinAttribute('status''catalog_product/status''entity_id'null'inner'$store->getId());
            
$collection->joinAttribute('visibility''catalog_product/visibility''entity_id'null'inner'$store->getId());
            
$collection->joinAttribute('price''catalog_product/price''entity_id'null'left'$store->getId());
        
}
        
else {
            $collection
->addAttributeToSelect('price');
            
$collection->addAttributeToSelect('status');
            
$collection->addAttributeToSelect('visibility');
        
}
        
        
/*
         * Restrict data retrieval for manufacturer
         * (manufacturer can deal with only their own products.)
         */
        
        //Get logged in user
        
$user Mage::getSingleton('admin/session')->getUser();
        
        
//Get the role id of the user
        
$roleId implode(''$user->getRoles());
        
        
//Get the role name
        
$roleName Mage::getModel('admin/roles')->load($roleId)->getRoleName();
        
        
//Check if the user is 'manufacturer'
        
if ($roleName == 'manufacturer'{
            $collection
->addAttributeToSelect('manufacturer');
            
            
//Get the user's manufacture id
            //(suppose that the manufacturer name was set on the user's fisrt name attribute.)
            
$source $collection->getResource()->getAttribute('manufacturer')->getSource();
            
$optionId Mage::getSingleton('catalog/config')->getSourceOptionId($source$user->getFirstname());
            
            
//Retrieve the data which manufacturer name is equal to the user's manufacturer 
            
$collection->addAttributeToFilter('manufacturer'$optionId);
        
}
        
        $this
->setCollection($collection);
        
        
Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
        
$this->getCollection()->addWebsiteNamesToResult();
        return 
$this;
    
}

}

I still need to apply this customization on ‘create’, ‘edit’, ‘delete’… of catalog/product.
I’m not sure that I’m on the right track.
 
Magento Community Magento Community
Magento Community
Magento Community
 
Webtechnology
Jr. Member
 
Total Posts:  2
Joined:  2013-03-20
 

By using this code you will get the detail of the user and also it’s role data
$user = Mage::getSingleton(’admin/session’);
$username = $user->getUser()->getUsername();
$role_data = Mage::getModel(’admin/user’)->getCollection()->addFieldToFilter(’username’,$username)->getFirstItem()->getRole()->getData();
$role_name = $role_data[’role_name’];
For brief and full code you can check it out here..
http://www.webtechnologycodes.com/get-admin-user-details-and-its-role-data-in-magento/

 
Magento Community Magento Community
Magento Community
Magento Community
 
Pepijn GS
Jr. Member
 
Total Posts:  1
Joined:  2013-10-22
 

Somehow these solutions all seem a bit wrong.
Personally I came up with this:

$user Mage::getSingleton('admin/session')->getUser();
$role $user->getRole();
$roleId $role->getRoleId();

$role will have various data in it:

[role_id] => 1
[parent_id] 
=> 0
[tree_level] 
=> 1
[sort_order] 
=> 1
[role_type] 
=> G
[user_id] 
=> 0
[role_name] 
=> Administrators

So you can do
$role->getUserId();
$role->getRoleType();
$role->getRoleName();
$role->getSortOrder();

etc..

You’re welcome smile

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top