0

I want to show a button on frontend that is only visible to admins and NOT customers. Is there some way? I've tried getting adminId on frontend but seems like it's not possible as backend and frontend work separately in magento.

2 Answers 2

0

Magento has different sessions for frontend and backend. So you can not be an admin user of the frontend, I think you will have to take a different approach.

There is functionality in Magento 2.4 called Login as Customer that could be interesting for you:

https://docs.magento.com/user-guide/configuration/customers/login-as-customer.html

Maybe your admin users can login as customers and you can based on this show a different buttons for them that are logged as a customer compared to real customers.

0
0

I did like your question, likely not for the right reason as it is not exactly a best principle to hijack the admin user table. Regardless, my solution below gives you something that works on my machine. If your customer is logged in and his email matches an admin user, it will consider this customer to be an admin user and subsequently will show the button. I am sorry, there is no ACL in this solution. If you needed the ACL built-in, I'd love to help but I hope this is sufficient to help you out now.

<?php

/**
 * @var $block \Magento\Framework\View\Element\Template
 *
 * @var $viewModel \Mbs\ButtonRestricted\ViewModel\ButtonView
 */
$viewModel = $block->getData('viewModel');
?>

<?php if ($viewModel->isSuperUser()): ?>
    <button id="click_me"><?= __('Click Me'); ?></button>
<?php endif; ?>

The ViewModel:

<?php

namespace Mbs\ButtonRestricted\ViewModel;

use Magento\Framework\AuthorizationInterface;
use Magento\Framework\View\Element\Block\ArgumentInterface;

class ButtonView implements ArgumentInterface
{
    /**
     * @var \Magento\Framework\AuthorizationInterface
     */
    private $authorization;

    /**
     * @var \Mbs\ButtonRestricted\Model\CurrentUserReader
     */
    private $currentUserReader;

    public function __construct(
        AuthorizationInterface $authorization,
        \Mbs\ButtonRestricted\Model\CurrentUserReader $currentUserReader
    ) {
        $this->authorization = $authorization;
        $this->currentUserReader = $currentUserReader;
    }

    public function isSuperUser()
    {
        try {
            $adminUser = $this->currentUserReader->getUser();
            return true;
        } catch (\Mbs\ButtonRestricted\NoAdminUserException $exception) {
            return false;
        }
    }
}

And finally a Model to check if the logged in customer has the same email as an admin user

<?php

namespace Mbs\ButtonRestricted\Model;

use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\Session;
use Mbs\ButtonRestricted\NoAdminUserException;

class CurrentUserReader
{
    /**
     * @var \Magento\Customer\Helper\Session\CurrentCustomer
     */
    private $currentCustomer;
    /**
     * @var \Magento\User\Model\ResourceModel\User
     */
    private $userResource;

    public function __construct(
        \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
        \Magento\User\Model\ResourceModel\User $userResource
    ) {
        $this->currentCustomer = $currentCustomer;
        $this->userResource = $userResource;
    }

    /**
     * @return mixed
     * @throws NoAdminUserException
     */
    public function getUser()
    {
        if ($this->currentCustomer->getCustomerId()) {
            $customer = $this->currentCustomer->getCustomer();

            $select = $this->userResource->getConnection()->select()->from($this->userResource->getMainTable())
                ->where('email=?', $customer->getEmail());
            $user = $this->userResource->getConnection()->fetchOne($select);

            if (!$user) {
                throw new NoAdminUserException();
            }

            return $user;
        }

        throw new NoAdminUserException();
    }

}

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.