4

I want to create sub admin role with own customer access permission. That means sub admin will login in admin panel and able to create customers. Sub admin can see only own customers, nothing else. There will be two sub-admin.

Please let me know how can I do this in Magento 2. I don't want to purchase any third party module.

Thanks in advance.

  • 1
    can you elaborate your question? you seem to want the user to be able to see only the customers he has created? This will involve customisation, because this requirement can lead to many customisations, you might want to come up with a detailed description of your needs. – Herve Tribouilloy Sep 16 '18 at 7:43
  • Hello herve, Thanks for your reply. I have created two admin user like user1 and user2 from system-> permissions-> All users. And also created a custom rule to assign access of customers only from USER ROLES. Now i want customer created by user1 only visible to this user in customer grid. When user2 logged in as admin he won't be able to view user1 customers. Could you please help me to some customization steps to perform the task. Thanks in advance. – user2082075 Sep 16 '18 at 8:42
  • will look into it asap. unfortunately, I have my girls asking for daddy's time. How urgent is it for you? – Herve Tribouilloy Sep 16 '18 at 8:47
  • hi herve, yes please go on with on your beautiful girls.... but there is an dead line for me by monday!!!!! curious to know the solution ASAP – user2082075 Sep 16 '18 at 9:22
0

step 1: create a new customer attribute that keeps track of the admin user who has created the customer. For customers created from frontend, I guess you may come up with a business rule whether they show or not..

step 2: extends the class: \Magento\Customer\Ui\Component\DataProvider or write a plugin for it.

  • I made a start that should be helping you a bit further: I did extend but you are welcome to use plugin of course:

and then I think it will be better to extends the function getSearchCriteria. See code below:

<?php    
namespace Mbs\CustomerTakeOver\Ui\Component;

use Magento\Customer\Ui\Component\Listing\AttributeRepository;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\Search\SearchCriteriaBuilder;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\View\Element\UiComponent\DataProvider\Reporting;

class CustomerGridDataProvider extends \Magento\Customer\Ui\Component\DataProvider
{
    /**
     * @var \Magento\Backend\Model\Auth\Session
     */
    private $authSession;

    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        Reporting $reporting,
        SearchCriteriaBuilder $searchCriteriaBuilder,
        RequestInterface $request,
        FilterBuilder $filterBuilder,
        AttributeRepository $attributeRepository,
        \Magento\Backend\Model\Auth\Session $authSession,
        array $meta = [],
        array $data = []
    ) {
        parent::__construct($name, $primaryFieldName, $requestFieldName, $reporting, $searchCriteriaBuilder, $request,
            $filterBuilder, $attributeRepository, $meta, $data);
        $this->authSession = $authSession;
    }

    /**
     * {@inheritdoc}
     */
    public function getSearchCriteria()
    {
        if (!$this->searchCriteria) {
            $filter = $this->filterBuilder->setField('user_creator')
                    ->setConditionType('eq')
                    ->setValue($this->getUserIdFromLoggedInBackend());
            $this->searchCriteria->setFilterGroups([$filter]);

            $this->searchCriteria = $this->searchCriteriaBuilder->create();
            $this->searchCriteria->setRequestName($this->name);
        }
        return $this->searchCriteria;
    }

    private function getUserIdFromLoggedInBackend()
    {
        return $this->authSession->getUser()->getId();
    }
}

I have not test the search criteria code and I do know the filter builder can be fiddly at times. But hopefully, you will get the gist of this solution now.

  • hi herve Thanks for your response.... i will try to implement this.....Thanks a ton!!!!! – user2082075 Sep 16 '18 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.