-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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 #402 from magento-performance/cabpi-199-admin-crea…
…te-email CABPI-199: Send email to newly created admin user
- Loading branch information
Showing
25 changed files
with
764 additions
and
16 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
app/code/Magento/AdminAdobeIms/Model/ImsEmailNotification.php
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,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; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
app/code/Magento/AdminAdobeIms/Observer/AdminAccountCreatedObserver.php
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,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); | ||
} | ||
} | ||
} |
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
92 changes: 92 additions & 0 deletions
92
app/code/Magento/AdminAdobeIms/Service/AdminNotificationService.php
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,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() | ||
); | ||
} | ||
} |
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
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
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,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> |
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
Oops, something went wrong.