diff --git a/modules/openy_gc_auth/openy_gc_auth.services.yml b/modules/openy_gc_auth/openy_gc_auth.services.yml index 5dc7f0c73..26d6cef33 100644 --- a/modules/openy_gc_auth/openy_gc_auth.services.yml +++ b/modules/openy_gc_auth/openy_gc_auth.services.yml @@ -14,4 +14,4 @@ services: openy_gc_auth.user_authorizer: class: Drupal\openy_gc_auth\GCUserAuthorizer - arguments: ['@entity_type.manager', '@event_dispatcher', '@password_generator'] + arguments: ['@entity_type.manager', '@event_dispatcher', '@messenger', '@password_generator'] diff --git a/modules/openy_gc_auth/src/GCUserAuthorizer.php b/modules/openy_gc_auth/src/GCUserAuthorizer.php index 0af52b41d..fe14f8d99 100644 --- a/modules/openy_gc_auth/src/GCUserAuthorizer.php +++ b/modules/openy_gc_auth/src/GCUserAuthorizer.php @@ -3,6 +3,9 @@ namespace Drupal\openy_gc_auth; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Link; +use Drupal\Core\Messenger\MessengerInterface; +use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\Password\PasswordGeneratorInterface; use Drupal\openy_gc_auth\Event\GCUserLoginEvent; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -12,6 +15,8 @@ */ class GCUserAuthorizer { + use StringTranslationTrait; + const VIRTUAL_Y_DEFAULT_ROLE = 'virtual_y'; /** @@ -28,6 +33,13 @@ class GCUserAuthorizer { */ protected $eventDispatcher; + /** + * The Messenger service. + * + * @var \Drupal\Core\Messenger\MessengerInterface + */ + protected $messenger; + /** * The service for generating passwords. * @@ -35,6 +47,7 @@ class GCUserAuthorizer { */ protected $passwordGenerator; + /** * GCUserAuthorizer constructor. * @@ -42,12 +55,15 @@ class GCUserAuthorizer { * Entity Type Manager. * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher * Event dispatcher. + * @param \Drupal\Core\Messenger\MessengerInterface $messenger + * The messenger service. * @param \Drupal\Core\Password\PasswordGeneratorInterface $password_generator * Service for generating passwords. */ - public function __construct(EntityTypeManagerInterface $entityTypeManager, EventDispatcherInterface $event_dispatcher, PasswordGeneratorInterface $password_generator) { + public function __construct(EntityTypeManagerInterface $entityTypeManager, EventDispatcherInterface $event_dispatcher, MessengerInterface $messenger, PasswordGeneratorInterface $password_generator) { $this->userStorage = $entityTypeManager->getStorage('user'); $this->eventDispatcher = $event_dispatcher; + $this->messenger = $messenger; $this->passwordGenerator = $password_generator; } @@ -88,6 +104,23 @@ public function authorizeUser($name, $email, array $extra_data = []) { $account->save(); } } + // List of roles to redirect user login page. + $userRolesArray = [ + 'administrator', + 'virtual_ymca_editor', + ]; + // Redirecting user login page. + foreach ($userRolesArray as $role) { + if ($account->hasRole($role)) { + $link = Link::createFromRoute($this->t('Login'), + 'user.login', + )->toString(); + $this->messenger->addStatus($this->t('Please retry your login from this link. @link', [ + '@link' => $link, + ])); + return; + } + } // Instantiate GC login user event. $event = new GCUserLoginEvent($account, $extra_data); // Dispatch the event.