-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from AmpersandHQ/add-admin-ui-button-to-get-key
Add admin panel interface to see the verbose log key
- Loading branch information
Showing
22 changed files
with
495 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,6 +80,26 @@ Update your `.gitignore` to ignore | |
app/etc/di.xml_ampersand_magento2_verbose_log_request/di.xml | ||
``` | ||
|
||
If you want to give certain admin users permissions to get the key via the `Admin Panel -> Account Settings -> Get Verbose Log Key` create `app/etc/di.xml_ampersand_magento2_verbose_log_request/allowed_emails_di.xml` and define the allowed emails/domains. | ||
|
||
```xml | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<type name="Ampersand\VerboseLogRequest\Service\Adminhtml\AllowedEmails"> | ||
<arguments> | ||
<!-- It is recommended to allow specific emails --> | ||
<argument name="allowedEmails" xsi:type="array"> | ||
<item name="1" xsi:type="string">[email protected]</item> | ||
</argument> | ||
<!-- There is support for whitelisting a whole domain, but this is less strict than the above --> | ||
<argument name="allowedDomains" xsi:type="array"> | ||
<item name="1" xsi:type="string">secondexample.com</item> | ||
</argument> | ||
</arguments> | ||
</type> | ||
</config> | ||
``` | ||
|
||
## Security considerations | ||
|
||
As all we are doing is writing to the log files the biggest "risk" is to your disk space. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
declare(strict_types=1); | ||
namespace Ampersand\VerboseLogRequest\Test\Integration\Adminhtml; | ||
|
||
use Magento\TestFramework\Helper\Bootstrap; | ||
|
||
class AccountControllerTest extends \Magento\TestFramework\TestCase\AbstractBackendController | ||
{ | ||
public function testNotAllowed() | ||
{ | ||
$allowedEmails = Bootstrap::getObjectManager() | ||
->get(\Ampersand\VerboseLogRequest\Service\Adminhtml\AllowedEmails::class); | ||
$allowedEmails->__construct([], []); // no allowed emails or domains | ||
$this->dispatch('backend/admin/system_account/index'); | ||
$this->assertStringContainsString('My Account', $this->getResponse()->getBody()); | ||
$this->assertStringNotContainsString('Get Verbose Log Key', $this->getResponse()->getBody()); | ||
} | ||
|
||
public function testAllowedEmail() | ||
{ | ||
$emails = [ | ||
'[email protected]', | ||
// https://github.com/AmpersandHQ/magento-docker-test-instance/blob/ec8b3cf09d286e19f01b40e3a09d5e17d65a7edc/Dockerfile-assets/magento-install.sh#L109 | ||
'[email protected]', | ||
'[email protected]', | ||
]; | ||
$allowedEmails = Bootstrap::getObjectManager() | ||
->get(\Ampersand\VerboseLogRequest\Service\Adminhtml\AllowedEmails::class); | ||
$allowedEmails->__construct($emails, []); | ||
$this->dispatch('backend/admin/system_account/index'); | ||
$this->assertStringContainsString('My Account', $this->getResponse()->getBody()); | ||
$this->assertStringContainsString('Get Verbose Log Key', $this->getResponse()->getBody()); | ||
} | ||
|
||
public function testAllowedDomain() | ||
{ | ||
$domains = [ | ||
'email.com', | ||
// https://github.com/AmpersandHQ/magento-docker-test-instance/blob/ec8b3cf09d286e19f01b40e3a09d5e17d65a7edc/Dockerfile-assets/magento-install.sh#L109 | ||
'@example.com', | ||
'example.com', | ||
'foobar.com' | ||
]; | ||
$allowedEmails = Bootstrap::getObjectManager() | ||
->get(\Ampersand\VerboseLogRequest\Service\Adminhtml\AllowedEmails::class); | ||
$allowedEmails->__construct([], $domains); | ||
$this->dispatch('backend/admin/system_account/index'); | ||
$this->assertStringContainsString('My Account', $this->getResponse()->getBody()); | ||
$this->assertStringContainsString('Get Verbose Log Key', $this->getResponse()->getBody()); | ||
} | ||
|
||
public function testAllowedEmailAndDomain() | ||
{ | ||
$emails = [ | ||
'[email protected]', | ||
// https://github.com/AmpersandHQ/magento-docker-test-instance/blob/ec8b3cf09d286e19f01b40e3a09d5e17d65a7edc/Dockerfile-assets/magento-install.sh#L109 | ||
'[email protected]', | ||
'[email protected]', | ||
]; | ||
$domains = [ | ||
'email.com', | ||
// https://github.com/AmpersandHQ/magento-docker-test-instance/blob/ec8b3cf09d286e19f01b40e3a09d5e17d65a7edc/Dockerfile-assets/magento-install.sh#L109 | ||
'@example.com', | ||
'example.com', | ||
'foobar.com' | ||
]; | ||
|
||
$allowedEmails = Bootstrap::getObjectManager() | ||
->get(\Ampersand\VerboseLogRequest\Service\Adminhtml\AllowedEmails::class); | ||
$allowedEmails->__construct($emails, $domains); | ||
$this->dispatch('backend/admin/system_account/index'); | ||
$this->assertStringContainsString('My Account', $this->getResponse()->getBody()); | ||
$this->assertStringContainsString('Get Verbose Log Key', $this->getResponse()->getBody()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
declare(strict_types=1); | ||
namespace Ampersand\VerboseLogRequest\Test\Integration\Adminhtml; | ||
|
||
use Magento\TestFramework\Helper\Bootstrap; | ||
|
||
class GetControllerTest extends \Magento\TestFramework\TestCase\AbstractBackendController | ||
{ | ||
public function testNotAllowed() | ||
{ | ||
$allowedEmails = Bootstrap::getObjectManager() | ||
->get(\Ampersand\VerboseLogRequest\Service\Adminhtml\AllowedEmails::class); | ||
$allowedEmails->__construct([], []); // no allowed emails or domains | ||
$this->dispatch('backend/ampersandverboselogrequest/key/get'); | ||
$this->assertRedirect($this->stringContains('admin/dashboard/index')); | ||
} | ||
|
||
public function testAllowedEmail() | ||
{ | ||
$emails = [ | ||
'[email protected]', | ||
// https://github.com/AmpersandHQ/magento-docker-test-instance/blob/ec8b3cf09d286e19f01b40e3a09d5e17d65a7edc/Dockerfile-assets/magento-install.sh#L109 | ||
'[email protected]', | ||
'[email protected]', | ||
]; | ||
$allowedEmails = Bootstrap::getObjectManager() | ||
->get(\Ampersand\VerboseLogRequest\Service\Adminhtml\AllowedEmails::class); | ||
$allowedEmails->__construct($emails, []); | ||
$this->dispatch('backend/ampersandverboselogrequest/key/get'); | ||
$this->assertStringContainsString('The current key is', $this->getResponse()->getBody()); | ||
$this->assertStringContainsString('The current key will expire at', $this->getResponse()->getBody()); | ||
} | ||
|
||
public function testAllowedDomain() | ||
{ | ||
$domains = [ | ||
'email.com', | ||
// https://github.com/AmpersandHQ/magento-docker-test-instance/blob/ec8b3cf09d286e19f01b40e3a09d5e17d65a7edc/Dockerfile-assets/magento-install.sh#L109 | ||
'@example.com', | ||
'example.com', | ||
'foobar.com' | ||
]; | ||
$allowedEmails = Bootstrap::getObjectManager() | ||
->get(\Ampersand\VerboseLogRequest\Service\Adminhtml\AllowedEmails::class); | ||
$allowedEmails->__construct([], $domains); | ||
$this->dispatch('backend/ampersandverboselogrequest/key/get'); | ||
$this->assertStringContainsString('The current key is', $this->getResponse()->getBody()); | ||
$this->assertStringContainsString('The current key will expire at', $this->getResponse()->getBody()); | ||
} | ||
|
||
public function testAllowedEmailAndDomain() | ||
{ | ||
$emails = [ | ||
'[email protected]', | ||
// https://github.com/AmpersandHQ/magento-docker-test-instance/blob/ec8b3cf09d286e19f01b40e3a09d5e17d65a7edc/Dockerfile-assets/magento-install.sh#L109 | ||
'[email protected]', | ||
'[email protected]', | ||
]; | ||
$domains = [ | ||
'email.com', | ||
// https://github.com/AmpersandHQ/magento-docker-test-instance/blob/ec8b3cf09d286e19f01b40e3a09d5e17d65a7edc/Dockerfile-assets/magento-install.sh#L109 | ||
'@example.com', | ||
'example.com', | ||
'foobar.com' | ||
]; | ||
|
||
$allowedEmails = Bootstrap::getObjectManager() | ||
->get(\Ampersand\VerboseLogRequest\Service\Adminhtml\AllowedEmails::class); | ||
$allowedEmails->__construct($emails, $domains); | ||
$this->dispatch('backend/ampersandverboselogrequest/key/get'); | ||
$this->assertStringContainsString('The current key is', $this->getResponse()->getBody()); | ||
$this->assertStringContainsString('The current key will expire at', $this->getResponse()->getBody()); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
declare(strict_types=1); | ||
namespace Ampersand\VerboseLogRequest\Block\Adminhtml; | ||
|
||
use Ampersand\VerboseLogRequest\Service\Adminhtml\UserChecker; | ||
use Ampersand\VerboseLogRequest\Service\GetKey as GetKeyService; | ||
use Magento\Framework\View\Element\Template; | ||
|
||
class GetKey extends Template | ||
{ | ||
/** | ||
* @var UserChecker | ||
*/ | ||
private UserChecker $userChecker; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private string $timestamp; | ||
|
||
/** | ||
* @var string|false | ||
*/ | ||
private $key; | ||
|
||
/** | ||
* @param Template\Context $context | ||
* @param GetKeyService $getKey | ||
* @param UserChecker $userChecker | ||
* @param mixed[] $data | ||
*/ | ||
public function __construct( | ||
Template\Context $context, | ||
GetKeyService $getKey, | ||
UserChecker $userChecker, | ||
array $data = [] | ||
) { | ||
parent::__construct($context, $data); | ||
$this->userChecker = $userChecker; | ||
|
||
$this->key = $getKey->execute(); | ||
$this->timestamp = date('Y-m-d H', strtotime('now +1 hour')) . ':00:00'; | ||
} | ||
|
||
/** | ||
* Is the current admin user allowed to acces the key | ||
* | ||
* @return bool | ||
*/ | ||
public function isAllowed() | ||
{ | ||
return $this->userChecker->isAdminUserAllowed(); | ||
} | ||
|
||
/** | ||
* Get the current verbose log key | ||
* | ||
* @return false|string | ||
*/ | ||
public function getKey() | ||
{ | ||
return $this->key; | ||
} | ||
|
||
/** | ||
* Get the timestamp that the current key expires | ||
* | ||
* @return string | ||
*/ | ||
public function getTimestamp() | ||
{ | ||
return $this->timestamp; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
declare(strict_types=1); | ||
namespace Ampersand\VerboseLogRequest\Controller\Adminhtml\Key; | ||
|
||
use Ampersand\VerboseLogRequest\Service\Adminhtml\UserChecker; | ||
use Magento\Backend\App\Action; | ||
use Magento\Backend\App\Action\Context; | ||
use Magento\Backend\Model\View\Result\Redirect; | ||
use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface; | ||
use Magento\Framework\View\Result\PageFactory; | ||
|
||
class Get extends Action implements HttpGetActionInterface | ||
{ | ||
/** | ||
* @var PageFactory | ||
*/ | ||
private PageFactory $pageFactory; | ||
|
||
/** | ||
* @var Redirect | ||
*/ | ||
protected Redirect $resultRedirect; | ||
|
||
/** | ||
* @var UserChecker | ||
*/ | ||
private UserChecker $userChecker; | ||
|
||
/** | ||
* @param Context $context | ||
* @param UserChecker $userChecker | ||
* @param PageFactory $pageFactory | ||
* @param Redirect $redirect | ||
*/ | ||
public function __construct( | ||
Context $context, | ||
UserChecker $userChecker, | ||
PageFactory $pageFactory, | ||
Redirect $redirect | ||
) { | ||
parent::__construct($context); | ||
$this->userChecker = $userChecker; | ||
$this->pageFactory = $pageFactory; | ||
$this->resultRedirect = $redirect; | ||
} | ||
|
||
/** | ||
* Display the Verbose Log Request Key to the allowed admin user | ||
* | ||
* @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface | ||
*/ | ||
public function execute() | ||
{ | ||
if (!$this->userChecker->isAdminUserAllowed()) { | ||
return $this->resultRedirect->setPath('admin/dashboard/index'); | ||
} | ||
|
||
$resultPage = $this->pageFactory->create(); | ||
// @phpstan-ignore-next-line | ||
$resultPage->getConfig()->getTitle()->prepend(__('Ampersand Verbose Log Request Key')); | ||
return $resultPage; | ||
} | ||
} |
Oops, something went wrong.