mocean_sms_login-8.x-1.x-dev/mocean_sms_login.module
mocean_sms_login.module
<?php
use Drupal\user\Entity;
use Drupal\user\Entity\User;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Database\Database;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\SessionManagerInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\mocean_sms_login\Utility;
/**
* Implements hook_help().
*/
function mocean_sms_login_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.mocean_sms_login':
$output .= '<h3>'.t('About'). '</h3>';
$output .= '<p>'.t('MoceanSMS Login module is used for providing 2FA login by using <a href=":mocean_link">Mocean</a>SMS service.', [':mocean_link' => 'https://moceanapi.com/']).'</p>';
$output .= '<p>'.t('This module can only send verfification code by SMS to users who have opted-in by using <a href=":der_link">Dynamic Entity Reference</a> to set up a field for telephone number in account settings. Any name for the label is valid as long as the machine name is field_phone.', [':der_link' => 'https://www.drupal.org/project/dynamic_entity_reference']).'</p>';
$output .= '<p>'.t('We also recommend <a href=":tv_link">Telephone Validation</a> to ensure phone numbers are in correct format.', [':tv_link' => 'https://www.drupal.org/project/telephone_validation']).'</p>';
$output .= '</br>';
$output .= '<dt>'.t('Enable and Disable 2FA') . '</dt>';
$output .= '<dd>'.t('A verification code will be sent to phone number provided by user to opt-in.').'</dd>';
return $output;
}
}
/**
* Implements hook_form_alter().
*/
function mocean_sms_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if($form_id === 'user_login_form') {
$form['error_field'] = [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => t(''),
];
$form['#validate'][] = 'mocean_sms_login_query';
}
}
/**
* Implements hook_user_login
*/
function mocean_sms_login_user_login($account) {
$uid = $account->id();
$database = \Drupal::database();
$query = $database->query('SELECT phone, verified FROM mocean_sms_login WHERE id = :id LIMIT 1', [':id' => $uid,]);
$results = $query->fetchAssoc();
if (!empty($results)) {
$phone = $results['phone'];
$verified = $results['verified'];
if ($verified == 0) {
$jsonResponse = (new Utility)->smsLoginSendCode($phone);
if ($jsonResponse['status'] == 0) {
//Verification code sent, redirect to page for entering verification code
$path = \Drupal\Core\Url::fromRoute('mocean_sms_login.sms_login_verify_form',
['uid' => $uid, 'arg' => $jsonResponse['reqid']])->toString();
$response = new RedirectResponse($path);
$response->send();
}
}
else if ($verified == 1) {}
else {
user_logout($account);
$response = new RedirectResponse('login');
$response->send();
}
}
}
/**
* Implements hook_user_logout
*/
function mocean_sms_login_user_logout($account) {
$uid = $account->id();
$database = \Drupal::database();
$database->update('mocean_sms_login')
->fields(['verified' => 0,])
->condition('id', $uid)
->execute();
}
/**
* Query of MoceanAPI for errors in user_login_form
*/
function mocean_sms_login_query(&$form, FormStateInterface $form_state) {
//if username of password incorrect, stop query
if (!$uid = $form_state->get('uid')) {
return;
}
else {
$uid = $form_state->get('uid');
$database = \Drupal::database();
$query = $database->query('SELECT * FROM mocean_sms_login WHERE id = :id LIMIT 1', [':id' => $uid,]);
$results = $query->fetchAssoc();
if (!empty($results)) {
$jsonResponse1 = (new Utility)->smsLoginGetCredit();
if ($jsonResponse1['status'] == 0) {
$jsonResponse2 = (new Utility)->smsLoginGetPricing();
if ($jsonResponse2['status'] == 0) {
$price = $jsonResponse2['destinations'][0]['price'];
$value = $jsonResponse1['value'];
if ($value < $price) {
$form_state->setErrorByName('error_field', t("Low on credit for 2FA Login, report to the site's admin."));
return;
}
}
else {
$form_state->setErrorByName('error_field', t('An error has occured, please try again later.'));
return;
}
}
else if ($jsonResponse1['status'] == 1) {
$form_state->setErrorByName('error_field', t("Authorization failed for 2FA Login, report to the site's admin."));
return;
}
else {
$form_state->setErrorByName('error_field', t('An error has occured, please try again later.'));
return;
}
}
}
}
