From a0c9a76298d12f3bedaf0a57c229a93bf7e4cf57 Mon Sep 17 00:00:00 2001 From: Fiko Borizqy Date: Sat, 31 Oct 2020 11:22:59 +0000 Subject: [PATCH] Initial Commit --- Api/Data/NotificationInterface.php | 55 ++++++++++++ Controller/Adminhtml/Go/To.php | 78 ++++++++++++++++ Model/Notification.php | 89 +++++++++++++++++++ Model/ResourceModel/Notification.php | 55 ++++++++++++ .../ResourceModel/Notification/Collection.php | 22 +++++ Model/Url.php | 39 ++++++++ Plugin/Authentication.php | 44 +++++++++ Plugin/Controller/Adminhtml/Auth/Login.php | 51 +++++++++++ Setup/InstallSchema.php | 55 ++++++++++++ composer.json | 17 ++++ etc/adminhtml/routes.xml | 8 ++ etc/di.xml | 10 +++ etc/module.xml | 8 ++ registration.php | 7 ++ 14 files changed, 538 insertions(+) create mode 100644 Api/Data/NotificationInterface.php create mode 100644 Controller/Adminhtml/Go/To.php create mode 100644 Model/Notification.php create mode 100644 Model/ResourceModel/Notification.php create mode 100644 Model/ResourceModel/Notification/Collection.php create mode 100644 Model/Url.php create mode 100644 Plugin/Authentication.php create mode 100644 Plugin/Controller/Adminhtml/Auth/Login.php create mode 100644 Setup/InstallSchema.php create mode 100644 composer.json create mode 100644 etc/adminhtml/routes.xml create mode 100644 etc/di.xml create mode 100644 etc/module.xml create mode 100644 registration.php diff --git a/Api/Data/NotificationInterface.php b/Api/Data/NotificationInterface.php new file mode 100644 index 0000000..4daf70d --- /dev/null +++ b/Api/Data/NotificationInterface.php @@ -0,0 +1,55 @@ +storeManager = $storeManager; + $this->http = $http; + $this->notificationFactory = $notificationFactory; + $this->url = $url; + $this->resultFactory = $resultFactory; + $this->response = $response; + $this->notificationCollectionFactory = $notificationCollectionFactory; + $this->resultRedirectFactory = $resultRedirectFactory; + + parent::__construct($context); + } + + /** + * Check url keys. If non valid - redirect + * + * @return bool + * @see \Magento\Backend\App\Request\BackendValidator for default + * request validation. + */ + public function _processUrlKeys() + { + $key = $this->http->getParam('key'); + $path = $this->http->getPathInfo(); + $destination = substr($path, strpos($path, $key) + strlen($key) + 1); + $notification = $this->notificationCollectionFactory->create()->addFieldToFilter('key', ['eq' => $key]) + ->addFieldToFilter('destination', ['eq' => $destination])->getFirstItem(); + + if ($notification->isEmpty()) return parent::_processUrlKeys(); + + if (!$this->_auth->isLoggedIn()) { + $url = $this->url->getUrl('admin/index/index', ['fiko_adminurl' => $key]); + $this->response->setRedirect($url)->sendResponse(); + return false; + } + + $this->response->setRedirect($this->url->getUrl($notification->getDestination()))->sendResponse(); + return false; + } + + /** + * Always redirect to homepage if failed to validate URL + * + * @return \Magento\Framework\View\Result\Page + */ + public function execute() + { + $resultRedirect = $this->resultRedirectFactory->create(); + $resultRedirect->setPath(''); + return $resultRedirect; + } +} diff --git a/Model/Notification.php b/Model/Notification.php new file mode 100644 index 0000000..9d7d939 --- /dev/null +++ b/Model/Notification.php @@ -0,0 +1,89 @@ +_init(ResourceModelNotification::class); + } + + /** + * {@inheritdoc} + */ + public function getKey() + { + return $this->getData(self::KEY); + } + + /** + * {@inheritdoc} + */ + public function setKey($key) + { + $this->setData(self::KEY, $key); + } + + /** + * {@inheritdoc} + */ + public function getDestination() + { + return $this->getData(self::DESTINATION); + } + + /** + * {@inheritdoc} + */ + public function setDestination($destination) + { + $this->setData(self::DESTINATION, $destination); + } + + /** + * {@inheritdoc} + */ + public function getCreatedAt() + { + return $this->getData(self::CREATED_AT); + } + + /** + * {@inheritdoc} + */ + public function setCreatedAt($createdAt) + { + $this->setData(self::CREATED_AT, $createdAt); + } +} diff --git a/Model/ResourceModel/Notification.php b/Model/ResourceModel/Notification.php new file mode 100644 index 0000000..6807404 --- /dev/null +++ b/Model/ResourceModel/Notification.php @@ -0,0 +1,55 @@ +resourceConnection = $resourceConnection; + $this->logger = $logger; + + parent::__construct($context, $connectionName); + } + + protected function _construct() + { + $this->_init(self::MAIN_TABLE_NAME, 'key'); + } + + public function save($notification) + { + try { + $this->resourceConnection->getConnection()->query( + "INSERT INTO fiko_adminurl_notification VALUES (:key, :destination, NOW()) + ON DUPLICATE KEY UPDATE destination = :destination", + [ + ':key' => $notification->getKey(), + ':destination' => $notification->getDestination(), + ] + ); + } catch (\Exception $e) { + $this->logger->warning($e->getMessage(), ['exception' => $e]); + } + } +} diff --git a/Model/ResourceModel/Notification/Collection.php b/Model/ResourceModel/Notification/Collection.php new file mode 100644 index 0000000..6b6b4e3 --- /dev/null +++ b/Model/ResourceModel/Notification/Collection.php @@ -0,0 +1,22 @@ +_init(Notification::class, ResourceModelNotification::class); + } +} diff --git a/Model/Url.php b/Model/Url.php new file mode 100644 index 0000000..b2e211c --- /dev/null +++ b/Model/Url.php @@ -0,0 +1,39 @@ +notificationFactory = $notificationFactory; + $this->notificationResourceModel = $notificationResourceModel; + $this->url = $url; + } + + public function generateKey() + { + return hash('tiger192,3', uniqid() . time() . uniqid()); + } + + public function getUrl($path, $params = []) + { + $path .= substr($path, -1, 1) != '/' ? '/' : ''; + foreach ($params as $key => $value) { + $path .= "{$key}/{$value}/"; + } + $key = $this->generateKey(); + $notification = $this->notificationFactory->create(); + $notification->setKey($key); + $notification->setDestination($path); + $this->notificationResourceModel->save($notification); + return $this->url->getUrl("fiko_adminurl/go/to", ['key' => $key]) . $path; + } +} diff --git a/Plugin/Authentication.php b/Plugin/Authentication.php new file mode 100644 index 0000000..b60bc0b --- /dev/null +++ b/Plugin/Authentication.php @@ -0,0 +1,44 @@ +auth = $auth; + $this->notificationFactory = $notificationFactory; + $this->resultFactory = $resultFactory; + $this->url = $url; + $this->response = $response; + } + + public function aroundDispatch( + \Magento\Backend\App\AbstractAction $subject, + callable $proceed, + \Magento\Framework\App\RequestInterface $request + ) { + if (is_null($request->getParam('fiko_adminurl'))) return $proceed($request); + if ($request->getMethod() !== "POST") return $proceed($request); + if ($request->getActionName() !== "index") return $proceed($request); + if ($request->getControllerName() !== "index") return $proceed($request); + if (!$this->auth->isLoggedIn()) return $proceed($request); + + $notification = $this->notificationFactory->create()->load($request->getParam('fiko_adminurl')); + if ($notification->isEmpty()) return $proceed($request); + + $this->response->setRedirect($this->url->getUrl($notification->getDestination()))->sendResponse(); + return false; + } +} diff --git a/Plugin/Controller/Adminhtml/Auth/Login.php b/Plugin/Controller/Adminhtml/Auth/Login.php new file mode 100644 index 0000000..dd1f031 --- /dev/null +++ b/Plugin/Controller/Adminhtml/Auth/Login.php @@ -0,0 +1,51 @@ +_auth = $_auth; + $this->_backendUrl = $_backendUrl; + $this->response = $response; + $this->resultPageFactory = $resultPageFactory; + $this->notificationCollectionFactory = $notificationCollectionFactory; + $this->http = $http; + } + + public function aroundExecute( + \Magento\Backend\Controller\Adminhtml\Auth\Login $subject, + callable $proceed + ) { + if ($this->_auth->isLoggedIn()) { + if ($this->_auth->getAuthStorage()->isFirstPageAfterLogin()) { + $this->_auth->getAuthStorage()->setIsFirstPageAfterLogin(true); + } + return $this->response->setRedirect($this->_backendUrl->getStartupPageUrl())->sendResponse(); + } + + if (strpos($this->http->getPathInfo(), 'fiko_adminurl') === false) return $proceed(); + + $key = $this->http->getParam('fiko_adminurl'); + $notification = $this->notificationCollectionFactory->create()->addFieldToFilter('key', ['eq' => $key]) + ->getFirstItem(); + + if ($notification->isEmpty()) return $proceed(); + + return $this->resultPageFactory->create(); + } +} diff --git a/Setup/InstallSchema.php b/Setup/InstallSchema.php new file mode 100644 index 0000000..536a333 --- /dev/null +++ b/Setup/InstallSchema.php @@ -0,0 +1,55 @@ +startSetup(); + + $table = $installer->getConnection() + ->newTable($installer->getTable(self::TABLE_NAME)) + ->addColumn( + 'key', + Table::TYPE_TEXT, + 50, + [ + 'nullable' => false, + 'unsigned' => true, + 'primary' => true + ], + 'Notification key' + ) + ->addColumn( + 'destination', + Table::TYPE_TEXT, + 255, + ['nullable' => false], + 'URL Destination' + ) + ->addColumn( + 'created_at', + Table::TYPE_TIMESTAMP, + null, + [ + 'nullable' => false, + 'default' => Table::TIMESTAMP_INIT + ], + 'Created At' + ) + ->setComment('Adminhtml direct link'); + $installer->getConnection()->createTable($table); + + $installer->endSetup(); + } +} diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..bc078a5 --- /dev/null +++ b/composer.json @@ -0,0 +1,17 @@ +{ + "name": "fiko/magento2-adminurl", + "description": "Create direct URL to magento2 adminhtml.", + "type": "magento2-module", + "version": "1.0.0", + "license": [ + "MIT" + ], + "autoload": { + "psr-4": { + "Fiko\\AdminUrl\\": "" + }, + "files": [ + "registration.php" + ] + } +} diff --git a/etc/adminhtml/routes.xml b/etc/adminhtml/routes.xml new file mode 100644 index 0000000..1b247ee --- /dev/null +++ b/etc/adminhtml/routes.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/etc/di.xml b/etc/di.xml new file mode 100644 index 0000000..bed1531 --- /dev/null +++ b/etc/di.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/etc/module.xml b/etc/module.xml new file mode 100644 index 0000000..66c9bd9 --- /dev/null +++ b/etc/module.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/registration.php b/registration.php new file mode 100644 index 0000000..03d516f --- /dev/null +++ b/registration.php @@ -0,0 +1,7 @@ +