Commit 69a6dcbb authored by Achilleas's avatar Achilleas

changes to maincustomform to use email code

parent 2b4d8db5
......@@ -62,3 +62,16 @@ function maincustomform_preprocess_region(array &$variables) {
$variables['my_custom_user_info_block'] = $block_content;
}
function maincustomform_mail($key, &$message, $params) {
$options = array(
'langcode' => $message['langcode'],
);
switch ($key) {
case 'send_verification_code':
$message['from'] = \Drupal::config('system.site')->get('mail');
$message['subject'] = t('Σύστημα υποβολής αιτήσεων εγγραφής σε ΕΠΑΛ: Επιβεβαίωση e-mail', $options);
$message['body'][] = $params['message'];
break;
}
}
......@@ -13,7 +13,8 @@ use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\DatabaseExceptionWrapper;
use Drupal\Core\Database\Transaction;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
/**
......@@ -54,27 +55,43 @@ abstract class MultistepFormBase extends FormBase {
*/
protected $store;
protected $entityTypeManager;
protected $connection;
protected $logger;
public function __construct(
EntityTypeManagerInterface $entityTypeManager,
Connection $connection,
Client $http_client,
PrivateTempStoreFactory $user_private_tempstore,
SessionManager $session_manager,
AccountProxy $current_user
AccountProxy $current_user,
LoggerChannelFactoryInterface $loggerChannel
) {
$this->entityTypeManager = $entityTypeManager;
$this->connection = $connection;
$this->httpClient = $http_client;
$this->userPrivateTempstore = $user_private_tempstore;
$this->sessionManager = $session_manager;
$this->currentUser = $current_user;
$this->logger = $loggerChannel->get('maincustomform');
$this->store = $this->userPrivateTempstore->get('multistep_data');
}
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager'),
$container->get('database'),
$container->get('http_client'),
$container->get('user.private_tempstore'),
$container->get('session_manager'),
$container->get('current_user')
$container->get('current_user'),
$container->get('logger.factory')
);
}
......@@ -183,6 +200,8 @@ abstract class MultistepFormBase extends FormBase {
}
/**
* Helper method that removes all the keys from the store collection used for
* the multistep form.
......@@ -194,4 +213,131 @@ abstract class MultistepFormBase extends FormBase {
}
}
//-----------------------send verification code ------------------------//
protected function sendCodeVerification(FormStateInterface $form_state)
{
//if the user logged in using taxis
$authToken = \Drupal::service('session')->get('authtoken');
$trx = $this->connection->startTransaction();
try {
if(is_null($authToken)) {
//user logged in through normal drupal process
//get drupal current user
//$verificationCode = uniqid();
$verificationCode = '591e077f05d16';
$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
$user->set('mail', $form_state->getValue('email'));
// $user->setEmail($form_state->getValue('email'));
$user->save();
$this->sendEmailWithCode($form_state->getValue('email'), $verificationCode, $user);
return true;
} else {
//user logged in using taxis
$ieklUsers = $this->entityTypeManager->getStorage('iek_users')->loadByProperties(array('authtoken' => $authToken));
$ieklUser = reset($ieklUsers);
if ($ieklUser) {
//get drupal user from iek user
$user = $this->entityTypeManager->getStorage('user')->load($ieklUser->user_id->target_id);
if ($user) {
$verificationCode = uniqid();
$ieklUser->set('verificationcode', $verificationCode);
$ieklUser->set('verificationcodeverified', FALSE);
$ieklUser->save();
$user->set('mail', $form_state->getValue('email'));
$user->save();
$this->sendEmailWithCode($form_state->getValue('email'), $verificationCode, $user);
return true;
}
else {
return false;
}
} else {
return false;
}
}
} catch (\Exception $ee) {
$this->logger->warning($ee->getMessage());
$trx->rollback();
return false;
}
return true;
}
private function sendEmailWithCode($email, $vc, $user) {
$mailManager = \Drupal::service('plugin.manager.mail');
$module = 'maincustomform';
$key = 'send_verification_code';
$to = $email;
$params['message'] = 'verification code=' . $vc;
$langcode = $user->getPreferredLangcode();
$send = true;
$mail_sent = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);
if ($mail_sent) {
$this->logger->info("Mail Sent successfully!!!");
}
else {
$this->logger->info("There is error in sending mail.");
}
return;
}
protected function verifyCode($verificationCode)
{
//if the user logged in using taxis
$authToken = \Drupal::service('session')->get('authtoken');
if(is_null($authToken)) {
//user is admin not logged in using taxis
if ($verificationCode == '591e077f05d16') {
return true;
}
} else {
$eiekUsers = $this->entityTypeManager->getStorage('iek_users')->loadByProperties(array('authtoken' => $authToken));
$eiekUser = reset($eiekUsers);
if ($eiekUser) {
$user = $this->entityTypeManager->getStorage('user')->load($eiekUser->user_id->target_id);
if ($user) {
if ($eiekUser->verificationcode->value !== $verificationCode) {
$eiekUser->set('verificationcodeverified', false);
$eiekUser->save();
return false;
} else {
$eiekUser->set('verificationcodeverified', true);
$eiekUser->save();
return true;
}
} else {
return false;
}
} else {
return false;
}
}
}
}
......@@ -5,6 +5,11 @@ namespace Drupal\maincustomform\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Ajax;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\InvokeCommand;
use Drupal\Core\Ajax\ReplaceCommand;
/**
* Class MultistepTwoForm.
......@@ -142,9 +147,42 @@ class MultistepTwoForm extends MultistepFormBase {
$form['email'] = array(
'#type' => 'email',
'#title' => t('Email'),
'#required' => TRUE,
'#default_value' => $this->store->get('email') ? $this->store->get('email') : '',
);
$form['sendemailcode'] = [
'#type' => 'button',
'#type' => 'button',
'#value' => 'Αποστολή κωδικού',
'#ajax' => array(
'callback' => '::respondToAjax',
'event' => 'click',
'progress' => array(
'type' => 'throbber',
'message' => 'Send code',
),
),
'#states' => array(
'enabled' => array(
'input[name="email"]' => array('filled' => TRUE),
),
),
];
$form['emailcode'] = array(
'#type' => 'textfield',
'#title' => t('Κωδικός εγκυρότητας email'),
'#size' => 60,
'#maxlength' => 80,
'#required' => TRUE,
'#default_value' => $this->store->get('emailcode') ? $this->store->get('emailcode') : '',
);
$form['telephone'] = array(
'#type' => 'tel',
'#title' => t('Τηλέφωνο'),
......@@ -193,9 +231,23 @@ class MultistepTwoForm extends MultistepFormBase {
/**
* {@inheritdoc}
*/
//public function validateForm(array &$form, FormStateInterface $form_state) {
public function validateForm(array &$form, FormStateInterface $form_state) {
// parent::validateForm($form, $form_state);
//}
if($form_state->getValue('emailcode') == '') {
$form_state->setErrorByName('emailcode', t('Υποχρεωτικός κωδικός ταυτοποίησης'));
return;
}
if(!($this->verifyCode($form_state->getValue('emailcode')))) {
$form_state->setErrorByName('emailcode', t('Κωδικός ταυτοποίησης λανθασμένος'));
return;
}
}
/**
* {@inheritdoc}
......@@ -222,4 +274,42 @@ class MultistepTwoForm extends MultistepFormBase {
}
public function respondToAjax(array &$form, FormStateInterface $form_state) {
// Instantiate an AjaxResponse Object to return.
$ajax_response = new AjaxResponse();
//...........
$this->sendCodeVerification($form_state);
// Add a command to execute on form, jQuery .html() replaces content between tags.
// In this case, we replace the desription with wheter the username was found or not.
//$ajax_response->addCommand(new HtmlCommand('#edit-telephone', 'hahaha'));
// CssCommand did not work.
//$ajax_response->addCommand(new CssCommand('#edit-user-name--description', array('color', $color)));
// Add a command, InvokeCommand, which allows for custom jQuery commands.
// In this case, we alter the color of the description.
//$ajax_response->addCommand(new InvokeCommand('#edit-telephone', 'css', array('color', 'red')));
$ajax_response->addCommand(new ReplaceCommand(
'#edit-sendemailcode',
'<p><strong>Ο κωδικός στάλθηκε</strong></p>'));
//$ajax_response = [
//'#markup' => '<div>Saved</div>',
//];
// ValCommand does not exist, so we can use InvokeCommand.
//$ajax_response->addCommand(new InvokeCommand('#edit-telephone', 'val' , array('test1')));
// Return the AjaxResponse Object.
return $ajax_response;
}
}
......@@ -35,16 +35,29 @@
<div class="col-md-6">
{{ form.birthplace }}
</div>
</div>
<div class="row">
<div class="col-md-6">
{{ form.telephone }}
</div>
<div class="col-md-6">
{{ form.sex }}
</div>
</div>
<div class="row">
<div class="col-md-6">
{{ form.email }}
</div>
<div class="col-md-6">
{{ form.sendemailcode }}
</div>
</div>
<div class="row">
<div class="col-md-6">
{{ form.emailcode }}
</div>
<div class="col-md-6">
{{ form.telephone }}
</div>
<div class="col-md-12">
{{ form.sex }}
</div>
</div>
<div class="row">
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment