Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fiko committed Oct 31, 2020
0 parents commit a0c9a76
Show file tree
Hide file tree
Showing 14 changed files with 538 additions and 0 deletions.
55 changes: 55 additions & 0 deletions Api/Data/NotificationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Fiko\AdminUrl\Api\Data;

interface NotificationInterface
{
const KEY = 'key';
const DESTINATION = 'destination';
const CREATED_AT = 'created_at';

/**
* get key
*
* @return string
*/
public function getKey();

/**
* set key
*
* @param string $key
* @return $this
*/
public function setKey($key);

/**
* get destination
*
* @return string
*/
public function getDestination();

/**
* set destination
*
* @param string $destination
* @return $this
*/
public function setDestination($destination);

/**
* get created at
*
* @return string
*/
public function getCreatedAt();

/**
* set created at
*
* @param string $createdAt
* @return $this
*/
public function setCreatedAt($createdAt);
}
78 changes: 78 additions & 0 deletions Controller/Adminhtml/Go/To.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Fiko\AdminUrl\Controller\Adminhtml\Go;

use Magento\Backend\App\Action\Context;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\Request\Http;
use Fiko\AdminUrl\Model\NotificationFactory;
use Magento\Backend\Model\Url;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\ResponseInterface;
use Fiko\AdminUrl\Model\ResourceModel\Notification\CollectionFactory;
use Magento\Framework\Controller\Result\RedirectFactory;

class To extends \Magento\Backend\App\Action
{
public function __construct(
Context $context,
StoreManagerInterface $storeManager,
Http $http,
NotificationFactory $notificationFactory,
Url $url,
ResultFactory $resultFactory,
ResponseInterface $response,
CollectionFactory $notificationCollectionFactory,
RedirectFactory $resultRedirectFactory
) {
$this->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;
}
}
89 changes: 89 additions & 0 deletions Model/Notification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace Fiko\AdminUrl\Model;

use \Magento\Framework\Model\Context;
use \Magento\Framework\Registry;
use \Magento\Framework\Model\ResourceModel\AbstractResource;
use \Magento\Framework\Data\Collection\AbstractDb;
use \Magento\Framework\Model\AbstractModel;
use \Fiko\AdminUrl\Api\Data\NotificationInterface;
use \Fiko\AdminUrl\Model\ResourceModel\Notification as ResourceModelNotification;

class Notification extends AbstractModel implements NotificationInterface
{
/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
*/
public function __construct(
Context $context,
Registry $registry,
AbstractResource $resource = null,
AbstractDb $resourceCollection = null,
array $data = []
) {
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
}

/**
* Initialize resource model
*
* @return void
*/
protected function _construct()
{
$this->_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);
}
}
55 changes: 55 additions & 0 deletions Model/ResourceModel/Notification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Fiko\AdminUrl\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\Context;
use Magento\Framework\App\ResourceConnection;
use Psr\Log\LoggerInterface;

class Notification extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
const MAIN_TABLE_NAME = 'fiko_adminurl_notification';

/**
* @param \Magento\Framework\App\ResourceConnection
*/
private $resourceConnection;

/**
* @param \Psr\Log\LoggerInterface
*/
private $logger;

public function __construct(
Context $context,
ResourceConnection $resourceConnection,
LoggerInterface $logger,
$connectionName = null
) {
$this->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]);
}
}
}
22 changes: 22 additions & 0 deletions Model/ResourceModel/Notification/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Fiko\AdminUrl\Model\ResourceModel\Notification;

use \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use \Fiko\AdminUrl\Model\Notification;
use \Fiko\AdminUrl\Model\ResourceModel\Notification as ResourceModelNotification;

class Collection extends AbstractCollection
{
protected $_idFieldName = 'key';

/**
* Define the resource model & the model.
*
* @return void
*/
protected function _construct()
{
$this->_init(Notification::class, ResourceModelNotification::class);
}
}
39 changes: 39 additions & 0 deletions Model/Url.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Fiko\AdminUrl\Model;

use Fiko\AdminUrl\Model\NotificationFactory;
use Fiko\AdminUrl\Model\ResourceModel\Notification;
use Magento\Backend\Model\Url as MagentoUrl;

class Url
{
public function __construct(
NotificationFactory $notificationFactory,
Notification $notificationResourceModel,
MagentoUrl $url
) {
$this->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;
}
}
44 changes: 44 additions & 0 deletions Plugin/Authentication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Fiko\AdminUrl\Plugin;

use Magento\Backend\Model\Auth;
use Fiko\AdminUrl\Model\NotificationFactory;
use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\Model\Url;
use Magento\Framework\App\ResponseInterface;

class Authentication
{
public function __construct(
Auth $auth,
NotificationFactory $notificationFactory,
ResultFactory $resultFactory,
Url $url,
ResponseInterface $response
) {
$this->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;
}
}
Loading

0 comments on commit a0c9a76

Please sign in to comment.