1022 lines
47 KiB
PHP
Executable File
1022 lines
47 KiB
PHP
Executable File
<?php
|
|
|
|
// declare(strict_types=1);
|
|
|
|
|
|
namespace App\Controllers;
|
|
use App\Helpers\MailHelper;
|
|
use App\Controllers\LoginController;
|
|
use App\Helpers\DepositHelper;
|
|
use App\Helpers\JWTToken;
|
|
use App\Helpers\HttpRequestHelper;
|
|
use App\Helpers\RestAuthHelper;
|
|
|
|
use CodeIgniter\HTTP\IncomingRequest;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
use CodeIgniter\API\ResponseTrait;
|
|
|
|
|
|
|
|
use App\Models\EmployeeModel;
|
|
use App\Models\AuthHistoryModel;
|
|
use App\Models\LevelContactModel;
|
|
|
|
use Firebase\JWT\JWT;
|
|
// require_once('../vendor/autoload.php');
|
|
|
|
|
|
|
|
class RestAuthenticationController extends AdminController
|
|
{
|
|
use ResponseTrait;
|
|
|
|
protected $myLogger;
|
|
protected $employeeModel;
|
|
protected $authHistoryModel;
|
|
protected $hrModel;
|
|
|
|
|
|
public function __construct()
|
|
{
|
|
// set_session_context('Client');
|
|
$this->myLogger = \Config\Services::mylogger();
|
|
|
|
$this->employeeModel = new EmployeeModel();
|
|
$this->authHistoryModel = new AuthHistoryModel();
|
|
$this->hrModel = new LevelContactModel();
|
|
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$this->myLogger->logme('error','Rest Authentication function called');
|
|
$data =["id"=>"2", "role"=>"staff"];
|
|
|
|
print_r(JWTToken::encode($data));
|
|
}
|
|
|
|
public function logined()
|
|
{
|
|
|
|
$this->myLogger->logme('error','Rest Authentication function called');
|
|
|
|
echo "Rest Authentication Logined Success........";
|
|
}
|
|
|
|
private function callThirdPartyAPI($postData , $endPoint)
|
|
{
|
|
$client = \Config\Services::curlrequest();
|
|
$url = env('POST_ENROLLMENT_BASEURL').$endPoint;
|
|
$response = $client->post( $url, ['json' => $postData, 'http_errors' => false ] );
|
|
// return json_decode($response->getBody(), true);
|
|
return $response->getBody();
|
|
}
|
|
|
|
// public function callThirdPartyGETAPI($queryParams, $endPoint)
|
|
// {
|
|
// $client = \Config\Services::curlrequest();
|
|
// $url = env('POST_ENROLLMENT_BASEURL') . $endPoint;
|
|
|
|
// $response = $client->get($url, [
|
|
// 'query' => $queryParams, // pass as query parameters
|
|
// 'http_errors' => false // prevent exception on non-200 status
|
|
// ]);
|
|
|
|
// return $response->getBody();
|
|
// }
|
|
|
|
public function callThirdPartyGETAPI($queryParams, $endPoint, $params = [])
|
|
{
|
|
$client = \Config\Services::curlrequest();
|
|
$url = env('POST_ENROLLMENT_BASEURL') . $endPoint;
|
|
|
|
$options = [
|
|
'query' => $queryParams,
|
|
'http_errors' => false,
|
|
];
|
|
|
|
if (isset($params['token']) && !empty($params['token'])) {
|
|
$options['headers'] = [
|
|
'Authorization' => 'Bearer ' . $params['token'],
|
|
'Accept' => 'application/json'
|
|
];
|
|
}
|
|
|
|
$response = $client->get($url, $options);
|
|
|
|
return $response->getBody();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// employee auth api's start
|
|
|
|
public function verifyEmployeeWithMobileNumber()
|
|
{
|
|
try {
|
|
$mobile_number = $this->request->getJSON()->mobile_number;
|
|
|
|
$empdata = RestAuthHelper::getPreAndPostDataByEmailOrMobile(['mobile_number' => $mobile_number]);
|
|
|
|
if(empty($empdata)){
|
|
$result = ['user_verification' => false , 'message' => "User not found"];
|
|
return $this->respond(['status' => 'failed','code' => 404,'data' => $result],200);
|
|
}
|
|
|
|
$employeeData = $this->employeeModel->select('employees.relationship,EP.employee_id')
|
|
->join('employee_polices EP', 'EP.employee_id = employees.id', 'inner')
|
|
->where('employees.is_active', 1)
|
|
->where('employees.relationship', 'self')
|
|
->where('employees.emp_status !=', 'truncated')
|
|
->where('employees.mobile', $mobile_number)
|
|
->where('EP.is_active', 1)
|
|
->whereIn('EP.status', ['draft', 'enrolled', 'expired'])
|
|
->first();
|
|
|
|
$employeeData = [];
|
|
if (isset($empdata['pre']) && !empty($empdata['pre'])) {
|
|
$employeeData = $empdata['pre'];
|
|
} elseif (isset($empdata['post']) && !empty($empdata['post'])) {
|
|
$employeeData = $empdata['post'];
|
|
}
|
|
|
|
|
|
if (isset($employeeData['employee_id']))
|
|
{
|
|
$result = ['user_verification' => true ,'message' => "Verified Successfully" ];
|
|
return $this->respond(['status' => 'success','code' => 200,'data' => $result],200);
|
|
} else {
|
|
// Call the third-party API function
|
|
return $this->callThirdPartyAPI($this->request->getJSON(),'verifyEmployeeNumber');
|
|
}
|
|
} catch (\Throwable $th) {
|
|
return $this->respond(['status' => 'failed','code' => 500,'data' => $th],500);
|
|
}
|
|
}
|
|
|
|
public function verifyEmployeeWithEmailId()
|
|
{
|
|
try {
|
|
|
|
$data = $this->request->getJSON();
|
|
$email = $data->email;
|
|
|
|
$empdata = RestAuthHelper::getPreAndPostDataByEmailOrMobile(['email_id' => $email]);
|
|
// print_r($empdata); die;
|
|
|
|
if(empty($empdata)){
|
|
$result = ['user_verification' => false , 'message' => "User not found"];
|
|
return $this->respond(['status' => 'failed','code' => 404,'data' => $result],200);
|
|
}
|
|
|
|
$employeeData = $this->employeeModel->select('
|
|
employees.relationship,
|
|
EP.employee_id,
|
|
employees.client_id,
|
|
employees.client_branch_id,
|
|
employees.email_corporate
|
|
|
|
')
|
|
->join('employee_polices EP', 'EP.employee_id = employees.id', 'inner')
|
|
->where('employees.is_active', 1)
|
|
->where('employees.relationship', 'Self')
|
|
->where('employees.emp_status !=', 'truncated')
|
|
->where('employees.email_corporate', $email)
|
|
->where('EP.is_active', 1)
|
|
->whereIn('EP.status', ['draft', 'enrolled', 'expired'])
|
|
->first();
|
|
|
|
$employeeData = [];
|
|
if (isset($empdata['pre']) && !empty($empdata['pre'])) {
|
|
$employeeData = $empdata['pre'];
|
|
}
|
|
|
|
|
|
if (isset($employeeData['employee_id'])) {
|
|
|
|
$otp = random_int(100000, 999999);
|
|
|
|
// $update = $this->employeeModel->where('email_corporate', $email)->where('relationship', 'self')
|
|
// ->where('is_active', 1)->set(array('otp' => $otp ))
|
|
// ->update();
|
|
|
|
$sql = "
|
|
UPDATE employees
|
|
INNER JOIN employee_polices ON employee_polices.employee_id = employees.id
|
|
SET employees.otp = ?
|
|
WHERE employees.id = ?
|
|
AND employees.relationship = 'self'
|
|
AND employees.is_active = 1
|
|
AND employee_polices.is_active = 1
|
|
AND employee_polices.status IN ('draft', 'enrolled')
|
|
";
|
|
|
|
$db = db_connect();
|
|
// $update = $db->query($sql, [$otp, $email]);
|
|
$update = $db->query($sql, [$otp, $employeeData['employee_id']]);
|
|
|
|
|
|
if($update)
|
|
{
|
|
$data->otp = $otp;
|
|
if (isset($empdata['post']['client_id']) && !empty($empdata['post']['client_id'])) {
|
|
$data->client_id = $empdata['post']['client_id'];
|
|
$data->employee_id = $empdata['post']['employee_id'];
|
|
}
|
|
$this->callThirdPartyAPI($data, 'updateEmpOTP');
|
|
|
|
|
|
$common = [
|
|
'client_id' => $employeeData['client_id'],
|
|
'client_branch_id' => $employeeData['client_branch_id'],
|
|
'client_policy_id' => null,
|
|
'employee_policy_id' => null,
|
|
'employee_id' => $employeeData['employee_id'],
|
|
'mail_type' => 'otp_mail',
|
|
];
|
|
$subject = 'Nhance user verification - OTP';
|
|
$mail_content = $otp . ' is your verification code for Nhance.';
|
|
$res = MailHelper::send_email(['mail' => $employeeData['email_corporate'], 'subject' => $subject, 'common' => $common, 'message' => $mail_content]);
|
|
$this->myLogger->logme("info", $res);
|
|
|
|
if (json_decode($res)->status == 'success') {
|
|
$result = ['user_verification' => true, 'message' => "Verified Successfully"];
|
|
return $this->respond(['status' => 'success', 'code' => 200, 'data' => $result], 200);
|
|
} else {
|
|
$result = ['user_verification' => false, 'message' => "Mail sending failed , try again"];
|
|
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => $result], 200);
|
|
}
|
|
} else {
|
|
|
|
$result = ['user_verification' => false, 'message' => "Verification failed , try again"];
|
|
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => $result], 200);
|
|
}
|
|
} else {
|
|
|
|
$apiParams = $this->request->getJSON();
|
|
if (!empty($empdata['post']['client_id'])) {
|
|
$apiParams->client_id = $empdata['post']['client_id'];
|
|
}
|
|
// print_r($apiParams); die;
|
|
// Call the third-party API function
|
|
return $this->callThirdPartyAPI($apiParams, 'verifyEmployeeEmailId');
|
|
}
|
|
} catch (\Throwable $th) {
|
|
$this->myLogger->logme("error", ($th->getMessage().' --- '.$th->getLine()));
|
|
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $th], 500);
|
|
}
|
|
}
|
|
|
|
public function getVerifiedUserData()
|
|
{
|
|
try {
|
|
|
|
$otp_verification = isset($this->request->getJSON()->otp_verification) ? $this->request->getJSON()->otp_verification : null;
|
|
$mobile_number = isset($this->request->getJSON()->mobile_number) ? $this->request->getJSON()->mobile_number : null;
|
|
|
|
$otp = isset($this->request->getJSON()->otp) ? $this->request->getJSON()->otp : null;
|
|
$email_id = isset($this->request->getJSON()->email_id) ? $this->request->getJSON()->email_id : null;
|
|
|
|
if (isset($this->request->getJSON()->login_by_hr))
|
|
{
|
|
$employeeData = $this->employeeModel->where('id', $this->request->getJSON()->employee_id)->where('relationship', 'self')->where('emp_status !=', 'truncated')->where('is_active', 1)->first();
|
|
}else{
|
|
|
|
$empdata = RestAuthHelper::getPreAndPostDataByEmailOrMobile(['email_id' => $email_id, 'otp' => $otp ]);
|
|
// print_r($empdata); die;
|
|
|
|
if(empty($empdata)){
|
|
return $this->respond(['status' => 'failed','code' => 404,'data' => "Invalid OTP"],200);
|
|
}
|
|
|
|
if (isset($mobile_number))
|
|
{
|
|
// $employeeData = $this->employeeModel->where('mobile', $mobile_number)->where('relationship', 'self')->where('emp_status !=', 'truncated')->where('is_active', 1)->first();
|
|
|
|
$employeeData = $this->employeeModel
|
|
->select('employees.*')
|
|
->join('employee_polices EP', 'EP.employee_id = employees.id', 'inner')
|
|
->where('employees.is_active', 1)
|
|
->where('employees.relationship', 'self')
|
|
->where('employees.emp_status !=', 'truncated')
|
|
->where('employees.mobile', $mobile_number)
|
|
->where('EP.is_active', 1)
|
|
->whereIn('EP.status', ['draft', 'enrolled'])
|
|
->first();
|
|
} else {
|
|
// $employeeData = $this->employeeModel->where('email_corporate', $email_id)->where('otp', $otp)->where('relationship', 'self')->where('emp_status !=', 'truncated')->where('is_active', 1)->first();
|
|
$employeeData = $this->employeeModel
|
|
->select('employees.*')
|
|
->join('employee_polices', 'employees.id = employee_polices.employee_id')
|
|
->where('employees.email_corporate', $email_id)
|
|
->where('employees.relationship', 'Self')
|
|
->where('employee_polices.is_active', 1)
|
|
->whereIn('employee_polices.status', ['draft', 'enrolled'])
|
|
->where('employees.is_active', 1)
|
|
->whereIn('employees.emp_status', ['draft', 'enrolled'])
|
|
->where('employees.otp', $otp)
|
|
->orderBy('id', 'desc')
|
|
->first();
|
|
}
|
|
|
|
$employeeData = [];
|
|
if (isset($empdata['pre']) && !empty($empdata['pre'])) {
|
|
$employeeData = $empdata['pre'];
|
|
}
|
|
|
|
}
|
|
|
|
$requestData = $this->request->getJSON();
|
|
if (isset($empdata['post']['client_id']) && !empty($empdata['post']['client_id'])) {
|
|
$requestData->client_id = $empdata['post']['client_id'];
|
|
$requestData->employee_id = $empdata['post']['employee_id'];
|
|
}
|
|
|
|
// print_r($requestData); die;
|
|
|
|
if ($employeeData && $otp_verification == true || $employeeData && isset($this->request->getJSON()->login_by_hr) || $employeeData && isset($this->request->getJSON()->otp) )
|
|
{
|
|
$auth = HttpRequestHelper::getRequestInfo();
|
|
if ($auth) {
|
|
$data = [
|
|
'user_id' => $employeeData['id'],
|
|
'user_type' => 'employee',
|
|
'ip' => $auth['ip'],
|
|
'platform' => $auth['platform'],
|
|
'broswer' => $auth['browser'],
|
|
];
|
|
$this->authHistoryModel->insert($data);
|
|
}
|
|
|
|
// print_r($employeeData); die;
|
|
unset($employeeData['employee_id']);
|
|
$result = JWTToken::encode($employeeData);
|
|
|
|
if(isset($this->request->getJSON()->otp)){
|
|
$this->employeeModel->where('id', $employeeData['id'])->where('otp', $otp)->where('relationship', 'self')->set(['otp'=>null])->update();
|
|
}
|
|
|
|
// Call the third-party API function
|
|
$apiResponse = $this->callThirdPartyAPI($requestData,'getVerifiedUserData');
|
|
return $this->respond(['status' => 'success','code' => 200,'data' => $result , 'post_enrollment'=> json_decode($apiResponse, true)],200);
|
|
|
|
} else {
|
|
|
|
// Call the third-party API function
|
|
$apiResponse = $this->callThirdPartyAPI($requestData,'getVerifiedUserData');
|
|
return $this->respond(['status' => 'failed','code' => 404,'data' => "Invalid OTP" , 'post_enrollment'=> json_decode($apiResponse, true)],200);
|
|
|
|
}
|
|
} catch (\Exception $e) {
|
|
$this->myLogger->logme("error", ($e->getMessage().' --- '.$e->getLine()));
|
|
return $this->respond(['status' => 'failed','code' => 500,'data' => $e->getMessage()],500);
|
|
}
|
|
}
|
|
|
|
// employee auth api's end
|
|
|
|
|
|
// HR auth api's start
|
|
|
|
public function verifyHrWithMobileNumber()
|
|
{
|
|
try {
|
|
$mobile_number = $this->request->getJSON()->mobile_number;
|
|
|
|
$HrData = $this->hrModel->where('mobile', $mobile_number)
|
|
->where('contact_type', 'client')
|
|
->where('is_active', 1)
|
|
->first();
|
|
|
|
if ($HrData) {
|
|
|
|
$result = ['user_verification' => true ,'message' => "Verified Successfully"];
|
|
return $this->respond(['status' => 'success','code' => 200,'data' => $result],200);
|
|
} else {
|
|
// Call the third-party API function
|
|
return $this->callThirdPartyAPI($this->request->getJSON(),'verifyHrWithMobileNumber');
|
|
|
|
}
|
|
} catch (\Throwable $th) {
|
|
return $this->respond(['status' => 'failed','code' => 500,'data' => $th],500);
|
|
}
|
|
}
|
|
|
|
public function verifyHrWithEmail()
|
|
{
|
|
try {
|
|
|
|
$data = $this->request->getJSON();
|
|
|
|
$email = $data->email;
|
|
|
|
$HrData = $this->hrModel->where('email', $email)
|
|
->where('contact_type', 'client')
|
|
->where('is_active', 1)
|
|
->first();
|
|
|
|
if ($HrData) {
|
|
|
|
$otp = random_int(100000, 999999);
|
|
|
|
$sql = "
|
|
UPDATE level_contacts
|
|
SET otp = ?
|
|
WHERE email = ?
|
|
AND contact_type = 'client'
|
|
AND is_active = 1
|
|
";
|
|
|
|
$db = db_connect();
|
|
$update = $db->query($sql, [$otp, $email]);
|
|
|
|
if($update)
|
|
{
|
|
$data->otp = $otp;
|
|
$this->callThirdPartyAPI($data, 'updateHROTP');
|
|
|
|
$common = [
|
|
'login_type' => 'HR login',
|
|
'login_email' => $email,
|
|
'mail_type' => 'otp_mail'
|
|
];
|
|
$subject = 'Nhance user verification - OTP';
|
|
$mail_content = $otp . ' is your verification code for Nhance.';
|
|
$res = MailHelper::send_email(['mail' => $email, 'subject' => $subject, 'common' => $common, 'message' => $mail_content]);
|
|
$this->myLogger->logme("info", $res);
|
|
|
|
if (json_decode($res)->status == 'success') {
|
|
$result = ['user_verification' => true, 'message' => "Verified Successfully"];
|
|
return $this->respond(['status' => 'success', 'code' => 200, 'data' => $result], 200);
|
|
} else {
|
|
$result = ['user_verification' => false, 'message' => "Mail sending failed , try again"];
|
|
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => $result], 200);
|
|
}
|
|
} else {
|
|
|
|
$result = ['user_verification' => false, 'message' => "Verification failed , try again"];
|
|
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => $result], 200);
|
|
}
|
|
|
|
} else {
|
|
// Call the third-party API function
|
|
return $this->callThirdPartyAPI($this->request->getJSON(),'verifyHrWithEmail');
|
|
|
|
}
|
|
} catch (\Throwable $th) {
|
|
return $this->respond(['status' => 'failed','code' => 500,'data' => $th],500);
|
|
}
|
|
}
|
|
|
|
|
|
public function getVerifiedHrData()
|
|
{
|
|
try {
|
|
// mobile number
|
|
$otp_verification = isset($this->request->getJSON()->otp_verification) ? $this->request->getJSON()->otp_verification : null;
|
|
$mobile_number = isset($this->request->getJSON()->mobile_no) ? $this->request->getJSON()->mobile_no : null;
|
|
// email id
|
|
$otp = isset($this->request->getJSON()->otp) ? $this->request->getJSON()->otp : null;
|
|
$email = isset($this->request->getJSON()->email) ? $this->request->getJSON()->email : null;
|
|
|
|
if (isset($mobile_number))
|
|
{
|
|
$hrData = $this->hrModel->where('mobile', $mobile_number)->where('contact_type', 'client')->first();
|
|
}else{
|
|
$hrData = $this->hrModel->where('email', $email)->where('contact_type', 'client')->where('otp', $otp)->first();
|
|
}
|
|
|
|
if ($hrData && $otp_verification == true || $hrData && isset($this->request->getJSON()->otp) )
|
|
{
|
|
|
|
$auth = HttpRequestHelper::getRequestInfo();
|
|
if ($auth) {
|
|
$data = [
|
|
'user_id' => $hrData['id'],
|
|
'user_type' => 'hr',
|
|
'ip' => $auth['ip'],
|
|
'platform' => $auth['platform'],
|
|
'broswer' => $auth['browser'],
|
|
];
|
|
|
|
$this->authHistoryModel->insert($data);
|
|
|
|
}
|
|
|
|
|
|
if(isset($this->request->getJSON()->mobile_no)){
|
|
|
|
$hrData = $this->hrModel ->select('level_contacts.* , client_branch.client_id as client_id')
|
|
->join('client_branch', 'level_contacts.ref_id = client_branch.id', 'left')
|
|
->where('level_contacts.mobile', $mobile_number )
|
|
->where('level_contacts.contact_type', 'client')
|
|
->find();
|
|
|
|
}else if(isset($this->request->getJSON()->otp)){
|
|
|
|
$this->hrModel->where('email', $email)->where('otp', $otp)->where('contact_type', 'client')->set(['otp'=>null])->update();
|
|
|
|
$hrData = $this->hrModel ->select('level_contacts.* , client_branch.client_id as client_id')
|
|
->join('client_branch', 'level_contacts.ref_id = client_branch.id', 'left')
|
|
->where('level_contacts.email', $email )
|
|
->where('level_contacts.contact_type', 'client')
|
|
->find();
|
|
}
|
|
|
|
//call and get allowed module data from post enrollment
|
|
$queryParams = [
|
|
'hr_id' => $hrData['0']['id'],
|
|
'request_for' => 'pre_enrollment'
|
|
];
|
|
$HRAccessRes = $this->callThirdPartyGETAPI($queryParams,'getHRAccessData');
|
|
$HRAccessData = json_decode($HRAccessRes,true);
|
|
if(isset($HRAccessData['data']['allowed_modules'])){ $hrData['0']['allowed_modules'] = json_decode($HRAccessData['data']['allowed_modules'],true)['pre']; }else{ $hrData['0']['allowed_modules'] = []; }
|
|
|
|
$hrData['0']['token_type'] = 'pre';
|
|
$result = JWTToken::encode($hrData['0']);
|
|
|
|
// Call the third-party API function
|
|
$apiResponse = $this->callThirdPartyAPI($this->request->getJSON(),'getVerifiedHrData');
|
|
return $this->respond(['status' => 'success','code' => 200,'data' => $result , 'post_enrollment'=> json_decode($apiResponse, true)],200);
|
|
|
|
|
|
} else {
|
|
|
|
// Call the third-party API function
|
|
$apiResponse = $this->callThirdPartyAPI($this->request->getJSON(),'getVerifiedHrData');
|
|
return $this->respond(['status' => 'failed','code' => 404,'data' => "Invalid OTP" , 'post_enrollment'=> json_decode($apiResponse, true)],200);
|
|
}
|
|
} catch (\Exception $e) {
|
|
return $this->respond(['status' => 'failed','code' => 500,'data' => $e->getMessage()],500);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function getUserIdFromToken()
|
|
{
|
|
$id = JWTToken::getUserIdFromToken();
|
|
// echo $id['id'];
|
|
echo "Id----> ";
|
|
print_r($id);
|
|
$role = JWTToken::getUserRoleFromToken();
|
|
echo " Role----> ";
|
|
echo $role;
|
|
|
|
|
|
$tokenArray = JWTToken::decodeToken();
|
|
echo " TokenArray----> ";
|
|
print_r($tokenArray);
|
|
}
|
|
|
|
|
|
public function saveMpin()
|
|
{
|
|
try {
|
|
|
|
$mobile_number = isset($this->request->getJSON()->mobile_number) ? $this->request->getJSON()->mobile_number : null;
|
|
$email_id = isset($this->request->getJSON()->email_id) ? $this->request->getJSON()->email_id : null;
|
|
|
|
$empdata = RestAuthHelper::getPreAndPostDataByEmailOrMobile(['email_id' => $email_id, 'mobile_number' => $mobile_number ]);
|
|
// print_r($empdata); die;
|
|
|
|
if(empty($empdata)){
|
|
$result = ['user_verification' => false , 'message' => "User not found"];
|
|
return $this->respond(['status' => 'failed','code' => 404,'data' => $result],200);
|
|
}
|
|
|
|
if (isset($mobile_number)) {
|
|
// $employeeData = $this->employeeModel->where('mobile', $mobile_number)->where('relationship', 'self')->first();
|
|
$employeeData = $this->employeeModel
|
|
->select('employees.*')
|
|
->join('employee_polices', 'employees.id = employee_polices.employee_id')
|
|
->where('employees.mobile', $mobile_number)
|
|
->where('employees.relationship', 'Self')
|
|
->where('employee_polices.is_active', 1)
|
|
->whereIn('employee_polices.status', ['draft', 'enrolled'])
|
|
->where('employees.is_active', 1)
|
|
->whereIn('employees.emp_status', ['draft', 'enrolled'])
|
|
->orderBy('id', 'desc')
|
|
->first();
|
|
} else {
|
|
// $employeeData = $this->employeeModel->where('email_corporate', $email_id)->where('relationship', 'self')->first();
|
|
$employeeData = $this->employeeModel
|
|
->select('employees.*')
|
|
->join('employee_polices', 'employees.id = employee_polices.employee_id')
|
|
->where('employees.email_corporate', $email_id)
|
|
->where('employees.relationship', 'Self')
|
|
->where('employee_polices.is_active', 1)
|
|
->whereIn('employee_polices.status', ['draft', 'enrolled'])
|
|
->where('employees.is_active', 1)
|
|
->whereIn('employees.emp_status', ['draft', 'enrolled'])
|
|
->orderBy('id', 'desc')
|
|
->first();
|
|
}
|
|
|
|
$employeeData = [];
|
|
if (isset($empdata['pre']) && !empty($empdata['pre'])) {
|
|
$employeeData = $empdata['pre'];
|
|
}
|
|
|
|
$mpin = $this->request->getJSON()->mpin;
|
|
$is_mpin_skipped = $this->request->getJSON()->is_mpin_skipped;
|
|
$is_biometric_enabled = $this->request->getJSON()->is_biometric_enabled;
|
|
|
|
$mpin_data_to_updata = [
|
|
'mpin' => $mpin,
|
|
'is_mpin_skipped' => $is_mpin_skipped,
|
|
'is_biometric_enabled' => $is_biometric_enabled
|
|
];
|
|
|
|
$requestData = $this->request->getJSON();
|
|
if (isset($empdata['post']['client_id']) && !empty($empdata['post']['client_id'])) {
|
|
$requestData->client_id = $empdata['post']['client_id'];
|
|
$requestData->employee_id = $empdata['post']['employee_id'];
|
|
}
|
|
|
|
if ($employeeData) {
|
|
$id= $employeeData["id"];
|
|
$updateMpin = $this->employeeModel->where('id', $id)->set($mpin_data_to_updata)->update();
|
|
if($updateMpin){
|
|
$this->callThirdPartyAPI($requestData, 'saveMpin');
|
|
$result = ['user_verification' => true , 'message' => "Mpin Updated"];
|
|
return $this->respond(['status' => 'success','code' => 200,'data' => $result],200);
|
|
}else{
|
|
$result = ['user_verification' => true , 'message' => "Mpin Not Updated"];
|
|
return $this->respond(['status' => 'failed','code' => 404,'data' => $result],200);
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
return $this->callThirdPartyAPI($requestData, 'saveMpin');
|
|
}
|
|
|
|
} catch (\Throwable $th) {
|
|
$this->myLogger->logme("error", ($th->getMessage().' --- '.$th->getLine()));
|
|
return $this->respond(['status' => 'failed','code' => 500,'data' => $th],500);
|
|
}
|
|
}
|
|
|
|
public function updateMpin()
|
|
{
|
|
try {
|
|
|
|
$mobile_number = isset($this->request->getJSON()->mobile_number) ? $this->request->getJSON()->mobile_number : null;
|
|
$email_id = isset($this->request->getJSON()->email_id) ? $this->request->getJSON()->email_id : null;
|
|
$old_mpin = $this->request->getJSON()->old_mpin;
|
|
$mpin = $this->request->getJSON()->new_mpin;
|
|
|
|
$empdata = RestAuthHelper::getPreAndPostDataByEmailOrMobile(['email_id' => $email_id, 'mobile_number' => $mobile_number, 'old_mpin' => $old_mpin ]);
|
|
// print_r($empdata); die;
|
|
|
|
if(empty($empdata)){
|
|
$result = ['user_verification' => false , 'message' => "User not found"];
|
|
return $this->respond(['status' => 'failed','code' => 404,'data' => $result],200);
|
|
}
|
|
|
|
if (isset($mobile_number)) {
|
|
// $employeeData = $this->employeeModel->where('mobile', $mobile_number)->where('mpin', $old_mpin)->where('relationship', 'self')->first();
|
|
$employeeData = $this->employeeModel
|
|
->select('employees.*')
|
|
->join('employee_polices', 'employees.id = employee_polices.employee_id')
|
|
->where('employees.mobile', $mobile_number)
|
|
->where('employees.relationship', 'Self')
|
|
->where('employee_polices.is_active', 1)
|
|
->whereIn('employee_polices.status', ['draft', 'enrolled'])
|
|
->where('employees.is_active', 1)
|
|
->whereIn('employees.emp_status', ['draft', 'enrolled'])
|
|
->where('employees.mpin', $old_mpin)
|
|
->orderBy('id', 'desc')
|
|
->first();
|
|
} else {
|
|
// $employeeData = $this->employeeModel->where('email_corporate', $email_id)->where('mpin', $old_mpin)->where('relationship', 'self')->first();
|
|
$employeeData = $this->employeeModel
|
|
->select('employees.*')
|
|
->join('employee_polices', 'employees.id = employee_polices.employee_id')
|
|
->where('employees.email_corporate', $email_id)
|
|
->where('employees.relationship', 'Self')
|
|
->where('employee_polices.is_active', 1)
|
|
->whereIn('employee_polices.status', ['draft', 'enrolled'])
|
|
->where('employees.is_active', 1)
|
|
->whereIn('employees.emp_status', ['draft', 'enrolled'])
|
|
->where('employees.mpin', $old_mpin)
|
|
->orderBy('id', 'desc')
|
|
->first();
|
|
}
|
|
|
|
$employeeData = [];
|
|
if (isset($empdata['pre']) && !empty($empdata['pre'])) {
|
|
$employeeData = $empdata['pre'];
|
|
}
|
|
|
|
$requestData = $this->request->getJSON();
|
|
if (isset($empdata['post']['client_id']) && !empty($empdata['post']['client_id'])) {
|
|
$requestData->client_id = $empdata['post']['client_id'];
|
|
}
|
|
|
|
|
|
if ($employeeData) {
|
|
$id= $employeeData["id"];
|
|
$updateMpin = $this->employeeModel->where('id', $id)->set('mpin', $mpin)->update();
|
|
if($updateMpin){
|
|
$this->callThirdPartyAPI($requestData, 'updateMpin');
|
|
$result = ['mpin_verification' => true , 'message' => "Mpin Updated"];
|
|
return $this->respond(['status' => 'success','code' => 200,'data' => $result],200);
|
|
}else{
|
|
$result = ['mpin_verification' => true , 'message' => "Mpin Not Updated"];
|
|
return $this->respond(['status' => 'failed','code' => 404,'data' => $result],200);
|
|
}
|
|
|
|
|
|
} else {
|
|
return $this->callThirdPartyAPI($requestData, 'updateMpin');
|
|
|
|
}
|
|
} catch (\Throwable $th) {
|
|
$this->myLogger->logme("error", ($th->getMessage().' --- '.$th->getLine()));
|
|
return $this->respond(['status' => 'failed','code' => 500,'data' => $th],500);
|
|
}
|
|
}
|
|
|
|
public function verifyMpin()
|
|
{
|
|
try {
|
|
|
|
$mobile_number = isset($this->request->getJSON()->mobile_number) ? $this->request->getJSON()->mobile_number : null;
|
|
$email_id = isset($this->request->getJSON()->email_id) ? $this->request->getJSON()->email_id : null;
|
|
$mpin = $this->request->getJSON()->mpin;
|
|
|
|
$empdata = RestAuthHelper::getPreAndPostDataByEmailOrMobile(['email_id' => $email_id, 'mobile_number' => $mobile_number]);
|
|
// print_r($empdata); die;
|
|
|
|
if(empty($empdata)){
|
|
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => 'Invalid MPIN'], 200);
|
|
}
|
|
|
|
if (isset($mobile_number)) {
|
|
// $employeeData = $this->employeeModel->where('mobile', $mobile_number)->where('relationship', 'self')->first();
|
|
$employeeData = $this->employeeModel
|
|
->select('employees.*')
|
|
->join('employee_polices', 'employees.id = employee_polices.employee_id')
|
|
->where('employees.mobile', $mobile_number)
|
|
->where('employees.relationship', 'Self')
|
|
->where('employee_polices.is_active', 1)
|
|
->whereIn('employee_polices.status', ['draft', 'enrolled'])
|
|
->where('employees.is_active', 1)
|
|
->whereIn('employees.emp_status', ['draft', 'enrolled'])
|
|
->orderBy('id', 'desc')
|
|
->first();
|
|
} else {
|
|
// $employeeData = $this->employeeModel->where('email_corporate', $email_id)->where('relationship', 'self')->first();
|
|
$employeeData = $this->employeeModel
|
|
->select('employees.*')
|
|
->join('employee_polices', 'employees.id = employee_polices.employee_id')
|
|
->where('employees.email_corporate', $email_id)
|
|
->where('employees.relationship', 'Self')
|
|
->where('employee_polices.is_active', 1)
|
|
->whereIn('employee_polices.status', ['draft', 'enrolled'])
|
|
->where('employees.is_active', 1)
|
|
->whereIn('employees.emp_status', ['draft', 'enrolled'])
|
|
->orderBy('id', 'desc')
|
|
->first();
|
|
}
|
|
|
|
$employeeData = [];
|
|
if (isset($empdata['pre']) && !empty($empdata['pre'])) {
|
|
$employeeData = $empdata['pre'];
|
|
}
|
|
|
|
$requestData = $this->request->getJSON();
|
|
if (isset($empdata['post']['client_id']) && !empty($empdata['post']['client_id'])) {
|
|
$requestData->client_id = $empdata['post']['client_id'];
|
|
}
|
|
|
|
|
|
if ($employeeData && $mpin == $employeeData["mpin"]) {
|
|
$auth = HttpRequestHelper::getRequestInfo();
|
|
if ($auth) {
|
|
$data = [
|
|
'user_id' => $employeeData['id'],
|
|
'user_type' => 'employee',
|
|
'ip' => $auth['ip'],
|
|
'platform' => $auth['platform'],
|
|
'broswer' => $auth['browser'],
|
|
];
|
|
|
|
$authdata = $this->authHistoryModel->insert($data);
|
|
}
|
|
|
|
unset($employeeData['employee_id']);
|
|
$result = JWTToken::encode($employeeData);
|
|
// Call the third-party API function
|
|
$apiResponse = $this->callThirdPartyAPI($requestData, 'verifyMpin');
|
|
return $this->respond(['status' => 'success', 'code' => 200, 'data' => $result, 'post_enrollment' => json_decode($apiResponse, true)], 200);
|
|
} else {
|
|
|
|
// Call the third-party API function
|
|
|
|
$apiResponse = $this->callThirdPartyAPI($requestData, 'verifyMpin');
|
|
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => "Invalid OTP", 'post_enrollment' => json_decode($apiResponse, true)], 200);
|
|
|
|
//$apiResponse = $this->callThirdPartyAPI($this->request->getJSON(),'/getVerifiedUserData');
|
|
// return $this->respond(['status' => 'failed','code' => 404,'data' => "Invalid OTP", 'post_enrollment'=> json_decode($apiResponse, true)],200);
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
$this->myLogger->logme("error", ($e->getMessage().' --- '.$e->getLine()));
|
|
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500);
|
|
return $this->respond(['status' => 'failed', 'code' => 200, 'post_enrollment' => json_decode($apiResponse, true)], 200);
|
|
}
|
|
}
|
|
|
|
public function checkMpin()
|
|
{
|
|
try {
|
|
$mobile_number = isset($this->request->getJSON()->mobile_number) ? $this->request->getJSON()->mobile_number : null;
|
|
$email_id = isset($this->request->getJSON()->email_id) ? $this->request->getJSON()->email_id : null;
|
|
|
|
$empdata = RestAuthHelper::getPreAndPostDataByEmailOrMobile(['email_id' => $email_id, 'mobile_number' => $mobile_number, 'check_mpin' => true]);
|
|
// print_r($empdata); die;
|
|
|
|
if(empty($empdata)){
|
|
return $this->respond(['status' => 'failed','code' => 404,'data' => "Mpin - not found", 'Mpin' =>null],200);
|
|
}
|
|
|
|
|
|
if (isset($mobile_number))
|
|
{
|
|
// $employeeData = $this->employeeModel->where('mobile', $mobile_number)->where('relationship', 'self')->first();
|
|
$employeeData = $this->employeeModel
|
|
->select('employees.*')
|
|
->join('employee_polices', 'employees.id = employee_polices.employee_id')
|
|
->where('employees.mobile', $mobile_number)
|
|
->where('employees.relationship', 'Self')
|
|
->where('employee_polices.is_active', 1)
|
|
->whereIn('employee_polices.status', ['draft', 'enrolled'])
|
|
->where('employees.is_active', 1)
|
|
->whereIn('employees.emp_status', ['draft', 'enrolled'])
|
|
->orderBy('id', 'desc')
|
|
->first();
|
|
}else{
|
|
// $employeeData = $this->employeeModel->where('email_corporate', $email_id)->where('relationship', 'self')->first();
|
|
$employeeData = $this->employeeModel
|
|
->select('employees.*')
|
|
->join('employee_polices', 'employees.id = employee_polices.employee_id')
|
|
->where('employees.email_corporate', $email_id)
|
|
->where('employees.relationship', 'Self')
|
|
->where('employee_polices.is_active', 1)
|
|
->whereIn('employee_polices.status', ['draft', 'enrolled'])
|
|
->where('employees.is_active', 1)
|
|
->whereIn('employees.emp_status', ['draft', 'enrolled'])
|
|
->orderBy('id', 'desc')
|
|
->first();
|
|
}
|
|
|
|
|
|
$employeeData = [];
|
|
if (isset($empdata['pre']) && !empty($empdata['pre'])) {
|
|
$employeeData = $empdata['pre'];
|
|
}
|
|
|
|
$requestData = $this->request->getJSON();
|
|
if (isset($empdata['post']['client_id']) && !empty($empdata['post']['client_id'])) {
|
|
$requestData->client_id = $empdata['post']['client_id'];
|
|
}
|
|
|
|
|
|
if ($employeeData && $employeeData["mpin"] != null) {
|
|
|
|
return $this->respond(['status' => 'success','code' => 200,'data' => "Mpin - exist" , 'Mpin' =>$employeeData["mpin"], 'is_mpin_skipped' => $employeeData['is_mpin_skipped'], 'is_biometric_enabled' => $employeeData['is_biometric_enabled']],200);
|
|
} else {
|
|
// return $this->respond(['status' => 'failed','code' => 404,'data' => "Mpin - not found", 'Mpin' =>null],200);
|
|
return $this->callThirdPartyAPI($requestData, 'checkMpin');
|
|
|
|
}
|
|
} catch (\Exception $e) {
|
|
$this->myLogger->logme("error", ($e->getMessage().' --- '.$e->getLine()));
|
|
return $this->respond(['status' => 'failed','code' => 500,'data' => $e->getMessage()],500);
|
|
}
|
|
}
|
|
|
|
public function forgotMPIN()
|
|
{
|
|
try {
|
|
log_message('info', 'forgotMPIN() called.');
|
|
|
|
$json = $this->request->getJSON();
|
|
$mobile_number = $json->mobile_number ?? null;
|
|
$email_id = $json->email_id ?? null;
|
|
|
|
log_message('info', 'Received input - Mobile: ' . var_export($mobile_number, true) . ', Email: ' . var_export($email_id, true));
|
|
|
|
// Step 1: Check input
|
|
if (!$mobile_number && !$email_id) {
|
|
log_message('error', 'Mobile number and email ID are both missing.');
|
|
return $this->respond([
|
|
'status' => 'failed',
|
|
'code' => 400,
|
|
'message' => 'Mobile number or email ID is required.'
|
|
], 400);
|
|
}
|
|
|
|
$empdata = RestAuthHelper::getPreAndPostDataByEmailOrMobile(['email_id' => $email_id, 'mobile_number' => $mobile_number]);
|
|
// print_r($empdata); die;
|
|
|
|
if(empty($empdata)){
|
|
return $this->respond(['status' => 'failed','code' => 404,'data' => "Mpin - not found", 'Mpin' =>null],200);
|
|
}
|
|
|
|
// Step 2: Fetch employee data
|
|
if ($mobile_number) {
|
|
log_message('info', 'Looking up employee by mobile number: ' . $mobile_number);
|
|
$employeeData = $this->employeeModel
|
|
->select('employees.*')
|
|
->join('employee_polices', 'employees.id = employee_polices.employee_id')
|
|
->where('employees.mobile', $mobile_number)
|
|
->where('employees.relationship', 'Self')
|
|
->where('employee_polices.is_active', 1)
|
|
->whereIn('employee_polices.status', ['draft', 'enrolled'])
|
|
->where('employees.is_active', 1)
|
|
->whereIn('employees.emp_status', ['draft', 'enrolled'])
|
|
->orderBy('id', 'desc')
|
|
->first();
|
|
} else {
|
|
log_message('info', 'Looking up employee by email: ' . $email_id);
|
|
$employeeData = $this->employeeModel
|
|
->select('employees.*')
|
|
->join('employee_polices', 'employees.id = employee_polices.employee_id')
|
|
->where('employees.email_corporate', $email_id)
|
|
->where('employees.relationship', 'Self')
|
|
->where('employee_polices.is_active', 1)
|
|
->whereIn('employee_polices.status', ['draft', 'enrolled'])
|
|
->where('employees.is_active', 1)
|
|
->whereIn('employees.emp_status', ['draft', 'enrolled'])
|
|
->orderBy('id', 'desc')
|
|
->first();
|
|
}
|
|
|
|
$employeeData = [];
|
|
if (isset($empdata['pre']) && !empty($empdata['pre'])) {
|
|
$employeeData = $empdata['pre'];
|
|
}
|
|
|
|
if (isset($empdata['post']['client_id']) && !empty($empdata['post']['client_id'])) {
|
|
$json->client_id = $empdata['post']['client_id'];
|
|
}
|
|
|
|
// Step 3: Found employee
|
|
if (!empty($employeeData)) {
|
|
log_message('info', 'Employee found: ID = ' . $employeeData['id']);
|
|
|
|
$updateData = [
|
|
'mpin' => null,
|
|
'is_mpin_skipped' => null,
|
|
'is_biometric_enabled' => null
|
|
];
|
|
|
|
log_message('info', 'Updating employee MPIN fields to null for ID: ' . $employeeData['id']);
|
|
|
|
$updated = $this->employeeModel
|
|
->where('id', $employeeData['id'])
|
|
->set($updateData)
|
|
->update();
|
|
|
|
if ($updated) {
|
|
$this->callThirdPartyAPI($json, 'forgotMPIN');
|
|
log_message('info', 'MPIN reset successful for employee ID: ' . $employeeData['id']);
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'code' => 200,
|
|
'message' => 'MPIN reset successfully.'
|
|
], 200);
|
|
} else {
|
|
log_message('error', 'Failed to update MPIN fields for employee ID: ' . $employeeData['id']);
|
|
return $this->respond([
|
|
'status' => 'failed',
|
|
'code' => 500,
|
|
'message' => 'Could not reset MPIN values.',
|
|
], 500);
|
|
}
|
|
} else {
|
|
// Step 4: Not found in local DB — call external fallback
|
|
log_message('warning', 'Employee not found locally. Falling back to third-party API.');
|
|
return $this->callThirdPartyAPI($json, 'forgotMPIN');
|
|
}
|
|
} catch (\Throwable $e) {
|
|
$this->myLogger->logme("error", ($e->getMessage().' --- '.$e->getLine()));
|
|
return $this->respond([
|
|
'status' => 'failed',
|
|
'code' => 500,
|
|
'message' => 'Server error: ' . $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
} |