Skip to content

Commit

Permalink
Add admin request login
Browse files Browse the repository at this point in the history
  • Loading branch information
ronangr1 committed Apr 1, 2024
1 parent 326e3bb commit e92d87b
Show file tree
Hide file tree
Showing 35 changed files with 842 additions and 127 deletions.
2 changes: 1 addition & 1 deletion Block/Adminhtml/System/Config/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected function getButtonHtml()
*/
protected function getJs(AbstractElement $element): string
{
$url = $this->getUrl(Config::XML_PATH_PASSWORDLESSLOGIN_SECRET_KEY_URL->value);
$url = $this->getUrl(Config::XML_PATH_PASSWORDLESSLOGIN_SECRET_KEY->value);
return '
<script>
require(["jquery", "domReady!"], function($) {
Expand Down
84 changes: 84 additions & 0 deletions Controller/Adminhtml/Pwl/ProcessLogin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Opengento\PasswordLessLogin\Controller\Adminhtml\Pwl;

use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\Result\RedirectFactory;
use Magento\Framework\Message\Manager as MessageManager;
use Opengento\PasswordLessLogin\Api\RequestLoginRepositoryInterface;
use Opengento\PasswordLessLogin\Enum\Config;
use Opengento\PasswordLessLogin\Exception\RequestException;
use Opengento\PasswordLessLogin\Service\Admin\Login as LoginService;
use Opengento\PasswordLessLogin\Service\Request\Encryption as EncryptionService;

class ProcessLogin implements HttpGetActionInterface
{
/**
* @param \Opengento\PasswordLessLogin\Api\RequestLoginRepositoryInterface $loginRequestRepository
* @param \Opengento\PasswordLessLogin\Service\Admin\Login $adminLoginService
* @param \Opengento\PasswordLessLogin\Service\Request\Encryption $encryptionService
* @param \Magento\Framework\App\RequestInterface $request
* @param \Magento\Framework\Controller\Result\RedirectFactory $redirectFactory
* @param \Magento\Framework\Message\Manager $messageManager
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
*/
public function __construct(
protected readonly RequestLoginRepositoryInterface $loginRequestRepository,
protected readonly LoginService $adminLoginService,
protected readonly EncryptionService $encryptionService,
protected readonly RequestInterface $request,
protected readonly RedirectFactory $redirectFactory,
protected readonly MessageManager $messageManager,
protected readonly ScopeConfigInterface $scopeConfig,
) {
}

/**
* @return \Magento\Framework\Controller\Result\Redirect
*/
public function execute()
{
$redirect = $this->redirectFactory->create();
$params = $this->request->getParams();
if ($params) {
try {
if (isset($params['request'])) {
$secretKey = $this->scopeConfig->getValue(Config::XML_PATH_PASSWORDLESSLOGIN_SECRET_KEY->value);
$decryptedData = $this->encryptionService->decrypt($params['request'], $secretKey);
$params = explode("/", $decryptedData);
$params = array_chunk($params, 2);
$params = array_combine(array_column($params, 0), array_column($params, 1));
if (isset($params['email']) && isset($params['token'])) {
$loginRequest = $this->loginRequestRepository->get($params['email']);
if ($loginRequest->getToken() === $params['token']) {
if ($loginRequest->hasBeenUsed() || $loginRequest->hasExpired()) {
$this->messageManager->addErrorMessage(__('Unable to execute request. Please try again.'));
return $redirect->setPath('*');
}
$this->loginRequestRepository->lock($loginRequest);
$this->request->setParams(['email' => $params['email']]);
$this->adminLoginService->perform($this->request);
$this->loginRequestRepository->delete($loginRequest);
} else {
throw new RequestException(_('Invalid request. Please try again.'));
}
} else {
throw new RequestException(_('Invalid request. Please try again.'));
}
} else {
throw new RequestException(_('Invalid request. Please try again.'));
}
} catch (\Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
}
return $redirect->setPath('*');
}
}
65 changes: 65 additions & 0 deletions Controller/Adminhtml/Pwl/RequestLogin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Opengento\PasswordLessLogin\Controller\Adminhtml\Pwl;

use Magento\Backend\Block\Admin\Formkey;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\Result\RedirectFactory;
use Magento\Framework\Message\Manager as MessageManager;
use Opengento\PasswordLessLogin\Service\Queue;

class RequestLogin implements HttpPostActionInterface
{
/**
* @param \Magento\Framework\App\RequestInterface $request
* @param \Magento\Framework\Controller\Result\RedirectFactory $redirectFactory
* @param \Magento\Framework\Message\Manager $messageManager
* @param \Magento\Backend\Block\Admin\Formkey $formKey
* @param \Opengento\PasswordLessLogin\Service\Queue $queueService
*/
public function __construct(
protected readonly RequestInterface $request,
protected readonly RedirectFactory $redirectFactory,
protected readonly MessageManager $messageManager,
protected readonly FormKey $formKey,
protected readonly Queue $queueService
) {
}

/**
* @return \Magento\Framework\Controller\Result\Redirect
*/
public function execute()
{
$redirect = $this->redirectFactory->create();

$params = $this->request->getParams();
if ($params) {
$isFormKey = $this->formKey->isEmpty();
if ($isFormKey) {
$this->messageManager->addErrorMessage(__('Invalid Form Key. Please refresh the page.'));
return $redirect->setPath('*/*');
}
if (!isset($params['login']['username'])) {
$this->messageManager->addErrorMessage(__('You must enter a valid email address.'));
return $redirect->setPath('*/*');
} else {
try {
$this->queueService->add($params, 'admin');
$this->messageManager->addSuccessMessage(__('If an account exists, you will receive an email to proceed with your request.'));
} catch (\Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
return $redirect->setPath('*/*');
}
}
}

return $redirect->setPath('*/*');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
declare(strict_types=1);

namespace Opengento\PasswordLessLogin\Controller\Account;
namespace Opengento\PasswordLessLogin\Controller\Pwl;

use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
Expand All @@ -15,14 +15,14 @@
use Opengento\PasswordLessLogin\Api\RequestLoginRepositoryInterface;
use Opengento\PasswordLessLogin\Enum\Config;
use Opengento\PasswordLessLogin\Exception\RequestException;
use Opengento\PasswordLessLogin\Service\Account\Login as LoginService;
use Opengento\PasswordLessLogin\Service\Customer\Login as LoginService;
use Opengento\PasswordLessLogin\Service\Request\Encryption as EncryptionService;

class ProcessLogin implements HttpGetActionInterface
{
/**
* @param \Opengento\PasswordLessLogin\Api\RequestLoginRepositoryInterface $loginRequestRepository
* @param \Opengento\PasswordLessLogin\Service\Account\Login $loginService
* @param \Opengento\PasswordLessLogin\Service\Customer\Login $loginService
* @param \Opengento\PasswordLessLogin\Service\Request\Encryption $encryptionService
* @param \Magento\Framework\App\RequestInterface $request
* @param \Magento\Framework\Controller\Result\RedirectFactory $redirectFactory
Expand Down Expand Up @@ -60,10 +60,10 @@ public function execute()
if ($request->getToken() === $params['token']) {
if ($request->hasBeenUsed() || $request->hasExpired()) {
$this->messageManager->addErrorMessage(__('Unable to execute request. Please try again.'));
return $redirect->setPath('customer/account/login');
return $redirect->setPath('*/account/login');
}
$this->loginRequestRepository->lock($request);
$this->loginService->process($params);
$this->loginService->perform($params);
$this->loginRequestRepository->delete($request);
} else {
throw new RequestException(_('Invalid request. Please try again.'));
Expand All @@ -78,6 +78,6 @@ public function execute()
$this->messageManager->addErrorMessage($e->getMessage());
}
}
return $redirect->setPath('customer/account');
return $redirect->setPath('*/account');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
*/
declare(strict_types=1);

namespace Opengento\PasswordLessLogin\Controller\Account;
namespace Opengento\PasswordLessLogin\Controller\Pwl;

use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\Result\RedirectFactory;
use Magento\Framework\Data\Form\FormKey;
use Magento\Framework\Message\Manager as MessageManager;
use Opengento\PasswordLessLogin\Service\Account;
use Opengento\PasswordLessLogin\Service\Queue;
use Opengento\PasswordLessLogin\Service\Request;

class RequestLogin implements HttpPostActionInterface
{
Expand All @@ -21,14 +22,16 @@ class RequestLogin implements HttpPostActionInterface
* @param \Magento\Framework\Controller\Result\RedirectFactory $redirectFactory
* @param \Magento\Framework\Message\Manager $messageManager
* @param \Magento\Framework\Data\Form\FormKey $formKey
* @param \Opengento\PasswordLessLogin\Service\Account $accountService
* @param \Opengento\PasswordLessLogin\Service\Request $requestService
* @param \Opengento\PasswordLessLogin\Service\Queue $queueService
*/
public function __construct(
protected readonly RequestInterface $request,
protected readonly RedirectFactory $redirectFactory,
protected readonly MessageManager $messageManager,
protected readonly FormKey $formKey,
protected readonly Account $accountService
protected readonly Request $requestService,
protected readonly Queue $queueService
) {
}

Expand All @@ -51,10 +54,7 @@ public function execute()
return $redirect->setPath('*/*/login');
} else {
try {
$addToQueue = $this->accountService->createLoginRequest($params['login']['username']);
if ($addToQueue) {
$this->accountService->sendLoginEmail($params['login']['username']);
}
$this->queueService->add($params, 'customer');
$this->messageManager->addSuccessMessage(__('If a customer account exists, you will receive an email to proceed with your request.'));
} catch (\Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
Expand Down
4 changes: 3 additions & 1 deletion Enum/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

enum Config: string
{
case XML_PATH_PASSWORDLESSLOGIN_ENABLE_ADMIN = 'passwordlesslogin/general/enable_admin';
case XML_PATH_PASSWORDLESSLOGIN_ENABLE_FRONTEND = 'passwordlesslogin/general/enable_frontend';
case XML_PATH_PASSWORDLESSLOGIN_TEMPLATE_ID = 'passwordlesslogin/email/template_id';
case XML_PATH_PASSWORDLESSLOGIN_SENDER_EMAIL = 'passwordlesslogin/email/sender_email';
case XML_PATH_PASSWORDLESSLOGIN_SENDER_NAME = 'passwordlesslogin/email/sender_name';
case XML_PATH_PASSWORDLESSLOGIN_SECRET_KEY = 'passwordlesslogin/security/secret_key';
case XML_PATH_PASSWORDLESSLOGIN_SECRET_KEY_URL = 'passwordlesslogin/system_config/secretkey';
case XML_PATH_PASSWORDLESSLOGIN_MAX_TIME_EXPIRATION = 'passwordlesslogin/security/max_time_expiration';
}
89 changes: 89 additions & 0 deletions Model/Admin/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Opengento\PasswordLessLogin\Model\Admin;

use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\User\Model\Spi\NotificatorInterface;
use Magento\User\Model\UserValidationRules;

class User extends \Magento\User\Model\User
{
protected $serializer;

/**
* @param \Opengento\PasswordLessLogin\Model\ResourceModel\Admin\User $resourceModel
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\User\Helper\Data $userData
* @param \Magento\Backend\App\ConfigInterface $config
* @param \Magento\Framework\Validator\DataObjectFactory $validatorObjectFactory
* @param \Magento\Authorization\Model\RoleFactory $roleFactory
* @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
* @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\User\Model\UserValidationRules $validationRules
* @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
* @param array $data
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
* @param \Magento\Framework\App\DeploymentConfig|null $deploymentConfig
* @param \Magento\User\Model\Spi\NotificatorInterface|null $notificator
*/
public function __construct(
protected readonly \Opengento\PasswordLessLogin\Model\ResourceModel\Admin\User $resourceModel,
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\User\Helper\Data $userData,
\Magento\Backend\App\ConfigInterface $config,
\Magento\Framework\Validator\DataObjectFactory $validatorObjectFactory,
\Magento\Authorization\Model\RoleFactory $roleFactory,
\Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
\Magento\Framework\Encryption\EncryptorInterface $encryptor,
\Magento\Store\Model\StoreManagerInterface $storeManager,
UserValidationRules $validationRules,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = [],
Json $serializer = null,
DeploymentConfig $deploymentConfig = null,
?NotificatorInterface $notificator = null
) {
parent::__construct(
$context,
$registry,
$userData,
$config,
$validatorObjectFactory,
$roleFactory,
$transportBuilder,
$encryptor,
$storeManager,
$validationRules,
$resource,
$resourceCollection,
$data,
$serializer,
$deploymentConfig,
$notificator
);
}

/**
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function loadByEmail(string $email): static
{
$data = $this->resourceModel->loadByEmail($email);
if ($data !== false) {
$this->setData($data);
$this->setOrigData();
}
return $this;
}
}
Loading

0 comments on commit e92d87b

Please sign in to comment.