diff --git a/html/modules/custom/ocha_ai_summarize/src/EventSubscriber/SocialAuthSubscriber.php b/html/modules/custom/ocha_ai_summarize/src/EventSubscriber/SocialAuthSubscriber.php new file mode 100644 index 0000000..5fb0e17 --- /dev/null +++ b/html/modules/custom/ocha_ai_summarize/src/EventSubscriber/SocialAuthSubscriber.php @@ -0,0 +1,96 @@ +configFactory = $config_factory; + $this->mailManager = $mail_manager; + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + $events[SocialAuthEvents::USER_FIELDS] = [ + 'onUserPreCreate', + ]; + + $events[SocialAuthEvents::USER_CREATED] = [ + 'onUserCreated', + ]; + + return $events; + } + + /** + * Set name to mail. + * + * @param \Drupal\social_auth\Event\UserFieldsEvent $event + * The Social Auth user fields event object. + */ + public function onUserPreCreate(UserFieldsEvent $event) { + $fields = $event->getUserFields(); + $fields['name'] = $fields['mail']; + + $parts = explode('@', $fields['mail']); + if ($parts[1] == 'un.org') { + $fields['status'] = TRUE; + } + + $event->setUserFields($fields); + } + + /** + * Send notification. + * + * @param \Drupal\social_auth\Event\UserEvent $event + * The Social Auth user event object. + */ + public function onUserCreated(UserEvent $event) { + $user = $event->getUser(); + + $parts = explode('@', $user->getInitialEmail()); + if ($parts[1] != 'un.org') { + return; + } + + $user->activate(); + $user->addRole('document_analyzer'); + $user->save(); + } + +}