Skip to content

Commit

Permalink
Merge pull request #402 from magento-performance/cabpi-199-admin-crea…
Browse files Browse the repository at this point in the history
…te-email

CABPI-199: Send email to newly created admin user
  • Loading branch information
andimov authored Mar 29, 2022
2 parents 2925775 + 5f7fa64 commit b3eec32
Show file tree
Hide file tree
Showing 25 changed files with 764 additions and 16 deletions.
113 changes: 113 additions & 0 deletions app/code/Magento/AdminAdobeIms/Model/ImsEmailNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\AdminAdobeIms\Model;

use Magento\Backend\App\Area\FrontNameResolver;
use Magento\Backend\App\ConfigInterface;
use Magento\Email\Model\BackendTemplate;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\MailException;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\View\Asset\Repository as AssetRepo;
use Magento\Store\Model\Store;

class ImsEmailNotification
{
/**
* @var TransportBuilder
*/
private TransportBuilder $transportBuilder;

/**
* @var ConfigInterface
*/
private ConfigInterface $config;

/**
* @var AssetRepo
*/
private AssetRepo $assetRepo;

/**
* @param TransportBuilder $transportBuilder
* @param ConfigInterface $config
* @param AssetRepo $assetRepo
*/
public function __construct(
TransportBuilder $transportBuilder,
ConfigInterface $config,
AssetRepo $assetRepo
) {
$this->transportBuilder = $transportBuilder;
$this->config = $config;
$this->assetRepo = $assetRepo;
}

/**
* Send email notification
*
* @param string $emailTemplate
* @param array $templateVars
* @param string $toEmail
* @param string $toName
* @return void
* @throws LocalizedException
*
* @throws MailException
*/
public function sendNotificationEmail(
string $emailTemplate,
array $templateVars,
string $toEmail,
string $toName
): void {

$templateVars = $this->addTemplateVars($templateVars);

$transport = $this->transportBuilder
->setTemplateIdentifier($emailTemplate)
->setTemplateModel(BackendTemplate::class)
->setTemplateOptions([
'area' => FrontNameResolver::AREA_CODE,
'store' => Store::DEFAULT_STORE_ID
])
->setTemplateVars($templateVars)
->setFromByScope(
$this->config->getValue('adobe_ims/email/new_user_email_identity'),
Store::DEFAULT_STORE_ID
)
->addTo($toEmail, $toName)
->getTransport();
$transport->sendMessage();
}

/**
* Add additional (default) template variables like current_year and logo if not already set
*
* @param array $templateVars
* @return array
*/
private function addTemplateVars(array $templateVars): array
{
if (!isset($templateVars['current_year'])) {
$templateVars['current_year'] = date('Y');
}

if (!isset($templateVars['logo_url'])) {
$logo = $this->assetRepo->getUrlWithParams(
'Magento_AdminAdobeIms::images/adobe-commerce-light.png',
[]
);

$templateVars['logo_url'] = $logo;
}

return $templateVars;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\AdminAdobeIms\Observer;

use Magento\AdminAdobeIms\Service\AdminNotificationService;
use Magento\AdminAdobeIms\Service\ImsConfig;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\User\Model\User;

class AdminAccountCreatedObserver implements ObserverInterface
{
/**
* @var ImsConfig
*/
private ImsConfig $imsConfig;

/**
* @var AdminNotificationService
*/
private AdminNotificationService $adminNotificationService;

/**
* @param ImsConfig $imsConfig
* @param AdminNotificationService $adminNotificationService
*/
public function __construct(
ImsConfig $imsConfig,
AdminNotificationService $adminNotificationService
) {
$this->imsConfig = $imsConfig;
$this->adminNotificationService = $adminNotificationService;
}

/**
* @inheritDoc
*/
public function execute(Observer $observer)
{
if (!$this->imsConfig->enabled()) {
return;
}

/** @var User $user */
$user = $observer->getObject();

if ($user->isObjectNew()) {
$this->adminNotificationService->sendWelcomeMailToAdminUser($user);
}
}
}
11 changes: 11 additions & 0 deletions app/code/Magento/AdminAdobeIms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ It's triggered by the event `controller_action_predispatch_adminhtml_auth_logout

Token is invalidated with a call, if it's successful, the access and refresh token are deleted in the `adobe_user_profile` table.

# Admin Created Email

We created an Observer for the `admin_user_save_after` event. \
There we check if the customer object is newly created or not. \
When a new admin user got created in Magento, he will then receive an email with further information on how to login.

We use the `admin_emails_new_user_created_template` Template for the content, and also created a new header and footer template for the Admin Adobe IMS module templates.
They are called `admin_adobe_ims_email_header_template` and `admin_adobe_ims_email_footer_template`.

The notification mail will be sent inside our `AdminNotificationService` where we can add and modify the template variables.

# Error Handling
For the AdminAdobeIms Module we have two specific error messages and one general error message which are shown on the Admin Login page when an error occured.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\AdminAdobeIms\Service;

use Magento\AdminAdobeIms\Model\ImsEmailNotification;
use Magento\Backend\Model\UrlInterface as BackendUrlInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\MailException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use Magento\User\Api\Data\UserInterface;

class AdminNotificationService
{
/**
* @var ImsConfig
*/
private ImsConfig $imsConfig;

/**
* @var BackendUrlInterface
*/
private BackendUrlInterface $backendUrl;

/**
* @var StoreManagerInterface
*/
private StoreManagerInterface $storeManager;

/**
* @var ImsEmailNotification
*/
private ImsEmailNotification $emailNotification;

/**
* @param ImsConfig $imsConfig
* @param BackendUrlInterface $backendUrl
* @param StoreManagerInterface $storeManager
* @param ImsEmailNotification $emailNotification
*/
public function __construct(
ImsConfig $imsConfig,
BackendUrlInterface $backendUrl,
StoreManagerInterface $storeManager,
ImsEmailNotification $emailNotification
) {
$this->imsConfig = $imsConfig;
$this->backendUrl = $backendUrl;
$this->storeManager = $storeManager;
$this->emailNotification = $emailNotification;
}

/**
* Send a welcome mail to created admin user
*
* @param UserInterface $user
* @return void
* @throws LocalizedException
* @throws MailException
* @throws NoSuchEntityException
*/
public function sendWelcomeMailToAdminUser(UserInterface $user): void
{
if (!$this->imsConfig->enabled()) {
return;
}

$backendUrl = $this->backendUrl->getRouteUrl('adminhtml');

$emailTemplate = $this->imsConfig->getEmailTemplateForNewAdminUsers();

$this->emailNotification->sendNotificationEmail(
$emailTemplate,
[
'user' => $user,
'store' => $this->storeManager->getStore(
Store::DEFAULT_STORE_ID
),
'cta_link' => $backendUrl
],
$user->getEmail(),
$user->getFirstName() . ' ' . $user->getLastName()
);
}
}
14 changes: 14 additions & 0 deletions app/code/Magento/AdminAdobeIms/Service/ImsConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class ImsConfig extends Config
public const XML_PATH_PRIVATE_KEY = 'adobe_ims/integration/private_key';
public const XML_PATH_AUTH_URL_PATTERN = 'adobe_ims/integration/auth_url_pattern';
public const XML_PATH_PROFILE_URL = 'adobe_ims/integration/profile_url';
public const XML_PATH_NEW_ADMIN_EMAIL_TEMPLATE = 'adobe_ims/email/content_template';

private const OAUTH_CALLBACK_URL = 'adobe_ims_auth/oauth/';
public const XML_PATH_LOGOUT_URL = 'adobe_ims/integration/logout_url';

Expand Down Expand Up @@ -225,6 +227,18 @@ public function getAdminAdobeImsAuthUrl(?string $clientId): string
);
}

/**
* Get email template for new created admin users
*
* @return string
*/
public function getEmailTemplateForNewAdminUsers(): string
{
return (string) $this->scopeConfig->getValue(
self::XML_PATH_NEW_ADMIN_EMAIL_TEMPLATE
);
}

/**
* Get callback url for AdminAdobeIms Module
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
<section name="AdminSignInErrorMessageSection">
<element name="generalHeadline"
type="text"
selector=".admin-adobe-ims-message-wrapper .admin-adobe-ims-message-header"/>
selector=".adobe-ims-error-message-wrapper .admin-adobe-ims-message-header"/>
<element name="generalMessage"
type="text"
selector=".admin-adobe-ims-message-wrapper .admin-adobe-ims-message-message"/>
selector=".adobe-ims-error-message-wrapper .admin-adobe-ims-message-message"/>
</section>
</sections>
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
<amOnPage url="{{AdminAdobeImsCallbackPage.url}}" stepKey="openCallbackUrl"/>
</before>

<waitForPageLoad stepKey="waitForAdminLoginPageLoad"/>

<!-- Check for Sign in with error message -->
<actionGroup ref="AssertAdminSignInEmptyCodeErrorMessageTestActionGroup"
stepKey="assertAdminLoginShowsErrorMessage"/>
Expand Down
4 changes: 3 additions & 1 deletion app/code/Magento/AdminAdobeIms/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
"magento/module-captcha": "*",
"magento/module-config": "*",
"magento/module-user": "*",
"magento/module-backend": "*"
"magento/module-backend": "*",
"magento/module-store": "*",
"magento/module-email": "*"
},
"suggest": {
"magento/module-theme": "*"
Expand Down
10 changes: 8 additions & 2 deletions app/code/Magento/AdminAdobeIms/etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Backend\App\Action\Plugin\Authentication"
type="Magento\AdminAdobeIms\App\Action\Plugin\Authentication"/>
<preference for="Magento\Backend\Model\Auth\Credential\StorageInterface"
<preference for="Magento\Backend\Model\Auth\Credential\StorageInterface"
type="Magento\AdminAdobeIms\Model\User" />

<type name="Magento\Framework\View\Result\Layout">
<plugin name="add_adobe_ims_layout_handle"
type="Magento\AdminAdobeIms\Plugin\AddAdobeImsLayoutHandlePlugin" />
</type>

<type name="Magento\Framework\View\Element\Message\MessageConfigurationsPool">
<arguments>
<argument name="configurationsMap" xsi:type="array">
Expand Down
13 changes: 13 additions & 0 deletions app/code/Magento/AdminAdobeIms/etc/adminhtml/events.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="admin_user_save_after">
<observer name="new_admin_user_created"
instance="Magento\AdminAdobeIms\Observer\AdminAccountCreatedObserver"/>
</event>
</config>
2 changes: 1 addition & 1 deletion app/code/Magento/AdminAdobeIms/etc/adminhtml/routes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* * See COPYING.txt for license details.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
Expand Down
Loading

0 comments on commit b3eec32

Please sign in to comment.