Skip to content
This repository has been archived by the owner on Jul 8, 2022. It is now read-only.

Commit

Permalink
[PRODDEV-463] Introduce automated welcome email for new users feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladyslav Solodovnyk committed Nov 10, 2021
1 parent fa57180 commit 9b26a75
Show file tree
Hide file tree
Showing 17 changed files with 478 additions and 49 deletions.
18 changes: 18 additions & 0 deletions config/install/openy_gated_content.welcome_email_settings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
enabled: 1
email_subject: 'Welcome to Virtual Y!'
email_body: |
<p>Explore your favorite YMCA programs from home or on the go!</p>
<p>Dear [user:display-name],</p>
<p>Thanks for joining our Virtual Y community. We are excited to have you here. With Virtual Y, you can:</p>
<ul>
<li>check out our latest fitness content, updated weekly</li>
<li>join live events to connect with other members, virtually</li>
<li>save your favorite classes, instructors, and content for easy reference next time.</li>
</ul>
<p>See you online!</p>
<p>Your YMCA team</p>
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,6 @@ class DaxkoSSO extends GCIdentityProviderPluginBase {

use MessengerTrait;

/**
* The configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;

/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* Daxko Client service instance.
*
Expand All @@ -56,13 +42,6 @@ class DaxkoSSO extends GCIdentityProviderPluginBase {
*/
protected $request;

/**
* The form builder service.
*
* @var \Drupal\Core\Form\FormBuilderInterface
*/
protected $formBuilder;

/**
* Daxko logger channel.
*
Expand Down Expand Up @@ -209,7 +188,7 @@ public function getLoginForm() {
*/
public function getMemberNotificationEmail(int $uid): string {
/** @var \Drupal\user\UserInterface $user */
$user = $this->entityTypeManager->getStorage('user')->load($uid);
$user = $this->userStorage->load($uid);
$daxko_email = $user->getEmail();

if (strpos($daxko_email, "daxko-") === FALSE) {
Expand Down
4 changes: 0 additions & 4 deletions modules/openy_gc_auth/openy_gc_auth.links.task.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
openy_gated_content.settings:
title: 'Virtual YMCA Settings'
route_name: openy_gated_content.settings
base_route: openy_gated_content.settings
openy_gc_auth.settings:
title: 'AUTH settings'
route_name: openy_gc_auth.settings
Expand Down
12 changes: 12 additions & 0 deletions modules/openy_gc_auth/src/GCIdentityProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\Session\AccountInterface;

/**
* Defines the common interface for all GCIdentityProvider plugins.
Expand Down Expand Up @@ -52,4 +53,15 @@ public function getLoginForm();
*/
public function getMemberNotificationEmail(int $uid): string;

/**
* Sends welcome email if enabled.
*
* Some providers can store fake email in system, in this case method
* should implement other logic.
*
* @param \Drupal\Core\Session\AccountInterface $user
* The account of user that should receive an email.
*/
public function sendWelcomeEmail(AccountInterface $user);

}
29 changes: 23 additions & 6 deletions modules/openy_gc_auth/src/GCIdentityProviderPluginBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\openy_gated_content\GCUserService;
use Symfony\Component\DependencyInjection\ContainerInterface;
Expand All @@ -33,11 +34,11 @@ abstract class GCIdentityProviderPluginBase extends PluginBase implements GCIden
protected $configFactory;

/**
* The entity type manager.
* The user storage.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
* @var \Drupal\user\UserStorageInterface
*/
protected $entityTypeManager;
protected $userStorage;

/**
* The form builder service.
Expand All @@ -62,7 +63,7 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
// We use pre-saved configuration here.
$configuration = $this->configFactory->get($this->getConfigName())->get();
$this->setConfiguration($configuration);
$this->entityTypeManager = $entity_type_manager;
$this->userStorage = $entity_type_manager->getStorage('user');
$this->formBuilder = $form_builder;
$this->gcUserService = $gc_user_service;
}
Expand Down Expand Up @@ -174,8 +175,24 @@ public function getMemberNotificationEmail(int $uid): string {
// Basic implementation of this method returns just drupal user email.
// Override this method in case your provider store fake emails on
// the Drupal side.
$user = $this->entityTypeManager->getStorage('user')->load($uid);
return $user->getEmail();
/** @var \Drupal\Core\Session\AccountInterface $user */
$user = $this->userStorage->load($uid);
return $user ? $user->getEmail() : '';
}

/**
* {@inheritdoc}
*/
public function sendWelcomeEmail(AccountInterface $user) {
$to = $this->getMemberNotificationEmail($user->id());
if (empty($to)) {
return;
}
$this->gcUserService->sendEmail(
'welcome_email',
$to,
['user' => $user]
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ public function notifyCustomer(PersonalTrainingInterface $personal_training) {
public function getUserEmail(int $uid) {
$virtual_y_config = $this->configFactory->get('openy_gc_auth.settings');
$active_provider = $virtual_y_config->get('active_provider');
$this->authManager->getDefinition($virtual_y_config->get('active_provider'), TRUE);
/** @var \Drupal\openy_gc_auth\GCIdentityProviderPluginBase $plugin_instance */
$this->authManager->getDefinition($active_provider, TRUE);
/** @var \Drupal\openy_gc_auth\GCIdentityProviderInterface $plugin_instance */
$plugin_instance = $this->authManager->createInstance($active_provider);
return $plugin_instance->getMemberNotificationEmail($uid);
}
Expand Down
13 changes: 13 additions & 0 deletions openy_gated_content.install
Original file line number Diff line number Diff line change
Expand Up @@ -459,3 +459,16 @@ function openy_gated_content_update_8021() {
$persistent_map->setMultiple($map);

}

/**
* Install initial settings for the welcome email.
*/
function openy_gated_content_update_8022() {
$config_dir = drupal_get_path('module', 'openy_gated_content');
$config_dir .= '/config/install/';
$config_importer = \Drupal::service('openy_upgrade_tool.importer');
$config_importer->setDirectory($config_dir);
$config_importer->importConfigs([
'openy_gated_content.welcome_email_settings',
]);
}
5 changes: 5 additions & 0 deletions openy_gated_content.links.action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
openy_gated_content.test_welcome_email:
route_name: openy_gated_content.test_welcome_email
title: 'Test welcome email'
appears_on:
- openy_gated_content.welcome_email_settings
7 changes: 7 additions & 0 deletions openy_gated_content.links.menu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ openy_gated_content.settings:
parent: openy_gated_content.openy
weight: 20

openy_gated_content.welcome_email_settings:
title: 'Welcome email settings'
route_name: openy_gated_content.welcome_email_settings
description: 'Virtual YMCA welcome email settings form.'
parent: openy_gated_content.settings
weight: 30

openy_gated_content.taxonomy:
title: 'Taxonomy'
route_name: openy_gated_content.taxonomy_config
Expand Down
9 changes: 9 additions & 0 deletions openy_gated_content.links.task.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
openy_gated_content.settings:
title: 'Virtual YMCA Settings'
route_name: openy_gated_content.settings
base_route: openy_gated_content.settings
openy_gated_content.welcome_email_settings:
title: 'Welcome email settings'
route_name: openy_gated_content.welcome_email_settings
base_route: openy_gated_content.settings
weight: 100
15 changes: 15 additions & 0 deletions openy_gated_content.module
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,21 @@ function openy_gated_content_user_pass_validation(array &$form, FormStateInterfa
}
}

/**
* Implements hook_mail().
*/
function openy_gated_content_mail($key, &$message, $params) {
switch ($key) {
case 'welcome_email':
$token = \Drupal::token();
$config = \Drupal::config('openy_gated_content.welcome_email_settings');
$message['from'] = \Drupal::config('system.site')->get('mail');
$message['subject'] = $token->replace($config->get('email_subject'), $params);
$message['body'][] = $token->replace($config->get('email_body'), $params);
break;
}
}

/**
* Make sure the terms do not have 2nd level of hierarchy.
*/
Expand Down
16 changes: 16 additions & 0 deletions openy_gated_content.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ openy_gated_content.settings:
requirements:
_permission: 'administer gated content configuration'

openy_gated_content.welcome_email_settings:
path: '/admin/openy/virtual-ymca/welcome-email-settings'
defaults:
_form: '\Drupal\openy_gated_content\Form\WelcomeEmailSettingsForm'
_title: 'Virtual Y welcome email settings'
requirements:
_permission: 'administer gated content configuration'

openy_gated_content.test_welcome_email:
path: '/admin/openy/virtual-ymca/welcome-email-settings/test'
defaults:
_form: '\Drupal\openy_gated_content\Form\TestWelcomeEmail'
_title: 'Test Virtual Y welcome email'
requirements:
_permission: 'administer gated content configuration'

openy_gated_content.categories.list:
path: '/api/categories-list'
defaults:
Expand Down
7 changes: 6 additions & 1 deletion openy_gated_content.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ services:
- { name: 'event_subscriber' }
openy_gated_content.user_service:
class: '\Drupal\openy_gated_content\GCUserService'
arguments: ['@entity_type.manager']
arguments: ['@entity_type.manager', '@plugin.manager.mail', '@language_manager']
openy_gated_content.route_subscriber:
class: '\Drupal\openy_gated_content\EventSubscriber\RouteSubscriber'
tags:
- { name: event_subscriber }
openy_gated_content.user_login_subscriber:
class: '\Drupal\openy_gated_content\EventSubscriber\GCUserLoginSubscriber'
arguments: ['@openy_gated_content.user_service', '@config.factory', '@plugin.manager.gc_identity_provider']
tags:
- { name: event_subscriber }
# Custom cache bin for favorites controller.
cache.vy_favorites:
class: Drupal\Core\Cache\CacheBackendInterface
Expand Down
97 changes: 97 additions & 0 deletions src/EventSubscriber/GCUserLoginSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Drupal\openy_gated_content\EventSubscriber;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\openy_gated_content\GCUserService;
use Drupal\openy_gc_auth\Event\GCUserLoginEvent;
use Drupal\openy_gc_auth\GCIdentityProviderManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Class GCLogUserLogin Subscriber.
*
* @package Drupal\openy_gated_content\EventSubscriber
*/
class GCUserLoginSubscriber implements EventSubscriberInterface {

/**
* The Gated Content User Service.
*
* @var \Drupal\openy_gated_content\GCUserService
*/
protected $gcUserService;

/**
* Config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;

/**
* The gated content authentication manager.
*
* @var \Drupal\openy_gc_auth\GCIdentityProviderManager
*/
protected $authManager;

/**
* Constructs a new GCUserLoginSubscriber.
*
* @param \Drupal\openy_gated_content\GCUserService $gcUserService
* The Gated Content User Service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* Config factory.
* @param \Drupal\openy_gc_auth\GCIdentityProviderManager $auth_manager
* The gated content authentication manager.
*/
public function __construct(
GCUserService $gcUserService,
ConfigFactoryInterface $configFactory,
GCIdentityProviderManager $auth_manager
) {
$this->gcUserService = $gcUserService;
$this->configFactory = $configFactory;
$this->authManager = $auth_manager;
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
GCUserLoginEvent::EVENT_NAME => 'onUserLogin',
];
}

/**
* Subscribe to the GC user login event dispatched.
*
* @param \Drupal\openy_gc_auth\Event\GCUserLoginEvent $event
* Event object.
*/
public function onUserLogin(GCUserLoginEvent $event) {
if (!($event->account instanceof AccountInterface)) {
return;
}
if ($event->account->getLastAccessedTime() > 0) {
return;
}
$welcome_email_config = $this->configFactory->get('openy_gated_content.welcome_email_settings');
if (!$welcome_email_config->get('enabled')) {
return;
}
$gc_auth_config = $this->configFactory->get('openy_gc_auth.settings');
$active_provider = $gc_auth_config->get('active_provider');
$plugin_definition = $this->authManager->getDefinition($active_provider, TRUE);
if (!$plugin_definition) {
return;
}
/** @var \Drupal\openy_gc_auth\GCIdentityProviderInterface $plugin_instance */
$plugin_instance = $this->authManager->createInstance($active_provider);
$plugin_instance->sendWelcomeEmail($event->account);
}

}
Loading

0 comments on commit 9b26a75

Please sign in to comment.