Skip to content
Snippets Groups Projects
Commit 6f439431 authored by Shashank Kumar's avatar Shashank Kumar Committed by Rajan Kumar
Browse files

#3360148 fix dependency injection issue

parent 4955b75b
No related branches found
No related tags found
1 merge request!4#3360148 fix dependency injection issue
services:
reset_password_email_otp.email_otp:
class: Drupal\reset_password_email_otp\EmailPassOTPContents
arguments: ['@database']
arguments: ['@database','@messenger','@logger.factory','@current_user','@config.factory','@plugin.manager.mail']
......@@ -3,12 +3,18 @@
namespace Drupal\reset_password_email_otp;
use Drupal\Core\Database\Connection;
use Drupal\Core\Messenger\Messenger;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Service that handles wordcloud data.
*/
class EmailPassOTPContents {
use StringTranslationTrait;
/**
* Database Connection Service.
*
......@@ -16,16 +22,67 @@ class EmailPassOTPContents {
*/
protected $dbConn;
/**
* Messenger.
*
* @var \Drupal\Core\Messenger\Messenger
*/
protected $messenger;
/**
* Logger.
*
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
*/
protected $loggerFactory;
/**
* User Account.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $account;
/**
* Config Factory.
*
* @var \Drupal\Core\Config\ConfigFactory
*/
protected $configFactory;
/**
* The mail manager.
*
* @var \Drupal\Core\Mail\MailManagerInterface
*/
protected $mailManager;
/**
* Constructor.
*
* @param \Drupal\Core\Database\Connection $connection
* The database connection.
* @param \Drupal\Core\Messenger\Messenger $messenger_status
* Messenger.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* Logger.
* @param \Drupal\Core\Session\AccountInterface $account
* Account Interface.
* @param \Drupal\Core\Config\ConfigFactory $configFactory
* Config Factory.
* @param \Drupal\Core\Mail\MailManagerInterface $mail_manager
* The mail manager.
*/
public function __construct(Connection $connection) {
public function __construct(Connection $connection, Messenger $messenger_status, LoggerChannelFactoryInterface $logger_factory, AccountInterface $account, ConfigFactory $configFactory, MailManagerInterface $mail_manager) {
$this->dbConn = $connection;
$this->messenger = $messenger_status;
$this->loggerFactory = $logger_factory->get('reset_password_email_otp');
$this->account = $account;
$this->configFactory = $configFactory;
$this->mailManager = $mail_manager;
}
/**
* Get reset OTP.
*
......@@ -76,24 +133,23 @@ class EmailPassOTPContents {
* Send mail callback.
*/
public function resetPasswordEmailOtpSend($mail, $otp, $key_mail) {
$mailManager = \Drupal::service('plugin.manager.mail');
$module = 'reset_password_email_otp';
switch ($key_mail) {
case 'reset-password-email-otp':
// Get Admin email id.
$params['message'] = str_replace('[OTP]', $otp, \Drupal::config('reset_password_email_otp.settings')
$params['message'] = str_replace('[OTP]', $otp, $this->configFactory->getEditable('reset_password_email_otp.settings')
->get('reset_password_email_otp_mail_body'));
$langcode = \Drupal::currentUser()->getPreferredLangcode();
$langcode = $this->account->getPreferredLangcode();
$send = TRUE;
// Send mail to all user.
$to = $mail;
if (!empty($to)) {
$result = $mailManager->mail($module, $key_mail, $to, $langcode, $params, NULL, $send);
$result = $this->mailManager->mail($module, $key_mail, $to, $langcode, $params, NULL, $send);
}
if ($result['result'] != TRUE) {
$message = t('There was a problem sending your email notification to @email.', ['@email' => 'users']);
\Drupal::messenger()->addError($message);
\Drupal::logger('mail-log')->error($message);
$message = $this->t('There was a problem sending your email notification to @email.', ['@email' => 'users']);
$this->messenger->addError($message);
$this->loggerFactory->error($message);
return;
}
......@@ -101,8 +157,8 @@ class EmailPassOTPContents {
default;
}
$message = t('An email notification with OTP has been sent to @email', ['@email' => 'users']);
\Drupal::logger('mail-log')->notice($message);
$message = $this->t('An email notification with OTP has been sent to @email', ['@email' => 'users']);
$this->loggerFactory->notice($message);
}
}
......@@ -4,12 +4,78 @@ namespace Drupal\reset_password_email_otp\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Config\ConfigFactory;
use Drupal\reset_password_email_otp\EmailPassOTPContents;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Form class for reset password email otp.
*/
class EmailOTPCheck extends FormBase {
/**
* Entity manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* Config Factory.
*
* @var \Drupal\Core\Config\ConfigFactory
*/
protected $configFactory;
/**
* Reset Password Service.
*
* @var \Drupal\reset_password_email_otp\EmailPassOTPContents
*/
protected $emailPassOTPContents;
/**
* Class Constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* Entity Type Manager.
* @param \Drupal\Core\Database\Connection $connection
* Connection.
* @param \Drupal\Core\Config\ConfigFactory $configFactory
* Config Factory.
* @param \Drupal\reset_password_email_otp\EmailPassOTPContents $emailPassOTPContents
* Config Factory.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, Connection $connection, ConfigFactory $configFactory, EmailPassOTPContents $emailPassOTPContents) {
$this->entityTypeManager = $entity_type_manager;
$this->connection = $connection;
$this->configFactory = $configFactory;
$this->emailPassOTPContents = $emailPassOTPContents;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager'),
$container->get('database'),
$container->get('config.factory'),
$container->get('reset_password_email_otp.email_otp'),
);
}
/**
* {@inheritdoc}
*/
......@@ -70,7 +136,7 @@ class EmailOTPCheck extends FormBase {
if (empty($account)) {
$account = user_load_by_name($email_or_username);
}
$user_storage = \Drupal::entityTypeManager()->getStorage('user');
$user_storage = $this->entityTypeManager->getStorage('user');
$user = $user_storage->load($account->id());
$user->setPassword($password);
$user->save();
......@@ -93,13 +159,12 @@ class EmailOTPCheck extends FormBase {
if (!empty($account)) {
if ($account->get('status')->value == '1') {
// Get OTP length config.
$emailOtp = \Drupal::service('reset_password_email_otp.email_otp');
$otp = $emailOtp->getOtpForResetPassword(\Drupal::config('reset_password_email_otp.settings')
$otp = $this->emailPassOTPContents->getOtpForResetPassword($this->configFactory->getEditable('reset_password_email_otp.settings')
->get('reset_password_email_otp_length'));
// Generate OTP and save in DB for every reset override old one.
$emailOtp->sendUserResetPasswordOtpWithEmail($account->id(), $otp);
$this->emailPassOTPContents->sendUserResetPasswordOtpWithEmail($account->id(), $otp);
// Send mail with OTP.
$emailOtp->resetPasswordEmailOtpSend($account->get('mail')->value, $otp, 'reset-password-email-otp');
$this->emailPassOTPContents->resetPasswordEmailOtpSend($account->get('mail')->value, $otp, 'reset-password-email-otp');
}
}
$form_state
......@@ -177,19 +242,19 @@ class EmailOTPCheck extends FormBase {
}
if (!empty($account)) {
// Get database connection to get has detail.
$query = \Drupal::database()->select('reset_password_email_otp_list', 'email_otp')
$query = $this->connection->select('reset_password_email_otp_list', 'email_otp')
->fields('email_otp', ['resetpass_id', 'uid', 'time', 'OTP', 'count'])
->condition('uid', $account->id(), '=')
->range(0, 1)
->orderBy('time', 'DESC');
$user_record = $query->execute()->fetchAssoc();
// Get config limit of OTP wrong attempt.
$limit_wrong_otp = \Drupal::config('reset_password_email_otp.settings')
$limit_wrong_otp = $this->configFactory->getEditable('reset_password_email_otp.settings')
->get('reset_password_email_otp_wrong_attempt');
if ($otp != $user_record['OTP'] && $user_record != FALSE) {
if ($user_record['count'] < $limit_wrong_otp) {
$form_state->setErrorByName('otp', $this->t('The OTP is not valid. Please try again.'));
\Drupal::database()->update('reset_password_email_otp_list')
$this->connection->update('reset_password_email_otp_list')
->fields([
'count' => $user_record['count'] + 1,
])
......
......@@ -3,6 +3,9 @@
namespace Drupal\reset_password_email_otp\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides Block for show Email OTP form.
......@@ -13,15 +16,51 @@ use Drupal\Core\Block\BlockBase;
* category = @Translation("Blocks")
* )
*/
class EmailOTPFormBlock extends BlockBase {
class EmailOTPFormBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* The form builder.
*
* @var \Drupal\Core\Form\FormBuilderInterface
*/
protected $formBuilder;
/**
* Constructs class.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Form\FormBuilderInterface $form_builder
* The form builder.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, FormBuilderInterface $form_builder) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->formBuilder = $form_builder;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('form_builder')
);
}
/**
* {@inheritdoc}
*/
public function build() {
$build = [];
$build['form'] = \Drupal::formBuilder()
->getForm('Drupal\reset_password_email_otp\Form\EmailOTPCheck');
$build['form'] = $this->formBuilder->getForm('Drupal\reset_password_email_otp\Form\EmailOTPCheck');
return $build;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment