471 lines
20 KiB
PHP
Executable File
471 lines
20 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 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 verifyEmployeeWithMobileNumber()
|
|
{
|
|
try {
|
|
$mobile_number = $this->request->getJSON()->mobile_number;
|
|
|
|
|
|
$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'])
|
|
->first();
|
|
|
|
|
|
|
|
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 {
|
|
$email = $this->request->getJSON()->email;
|
|
|
|
|
|
$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.email_corporate', $email)
|
|
->where('EP.is_active', 1)
|
|
->whereIn('EP.status', ['draft', 'enrolled'])
|
|
->first();
|
|
|
|
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();
|
|
if($update)
|
|
{
|
|
|
|
$common = [
|
|
'client_id' => $employeeData['client_id'],
|
|
'client_branch_id' => $employeeData['client_branch_id'],
|
|
'client_policy_id' => null,
|
|
'employee_policy_id' => null,
|
|
'employee_id' => $employeeData['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 {
|
|
// Call the third-party API function
|
|
return $this->callThirdPartyAPI($this->request->getJSON(),'verifyEmployeeEmailId');
|
|
}
|
|
} catch (\Throwable $th) {
|
|
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{
|
|
if (isset($mobile_number))
|
|
{
|
|
$employeeData = $this->employeeModel->where('mobile', $mobile_number)->where('relationship', 'self')->where('emp_status !=', 'truncated')->where('is_active', 1)->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();
|
|
}
|
|
}
|
|
|
|
|
|
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);
|
|
}
|
|
|
|
|
|
$result = JWTToken::encode($employeeData);
|
|
|
|
if(isset($this->request->getJSON()->otp)){
|
|
$this->employeeModel->where('email_corporate', $email_id)->where('otp', $otp)->where('relationship', 'self')->set(['otp'=>null])->update();
|
|
}
|
|
|
|
// Call the third-party API function
|
|
$apiResponse = $this->callThirdPartyAPI($this->request->getJSON(),'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($this->request->getJSON(),'getVerifiedUserData');
|
|
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 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 {
|
|
$result = ['user_verification' => false , 'message' => "User not found"];
|
|
return $this->respond(['status' => 'failed','code' => 404,'data' => $result],200);
|
|
|
|
}
|
|
} catch (\Throwable $th) {
|
|
return $this->respond(['status' => 'failed','code' => 500,'data' => $th],500);
|
|
}
|
|
}
|
|
|
|
|
|
public function getVerifiedHrData()
|
|
{
|
|
try {
|
|
$mobile_number = $this->request->getJSON()->mobile_number;
|
|
$otp_verification = $this->request->getJSON()->otp_verification;
|
|
|
|
$hrData = $this->hrModel->where('mobile', $mobile_number)->first();
|
|
if ($hrData && $otp_verification == true) {
|
|
$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);
|
|
|
|
}
|
|
|
|
|
|
$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 )
|
|
->find();
|
|
|
|
$result = JWTToken::encode($hrData['0']);
|
|
return $this->respond(['status' => 'success','code' => 200,'data' => $result],200);
|
|
} else {
|
|
return $this->respond(['status' => 'failed','code' => 404,'data' => "Invalid OTP"],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;
|
|
|
|
if (isset($mobile_number)) {
|
|
$employeeData = $this->employeeModel->where('mobile', $mobile_number)->where('relationship', 'self')->first();
|
|
} else {
|
|
$employeeData = $this->employeeModel->where('email_corporate', $email_id)->where('relationship', 'self')->first();
|
|
}
|
|
|
|
$mpin = $this->request->getJSON()->mpin;
|
|
|
|
|
|
|
|
|
|
if ($employeeData) {
|
|
$id= $employeeData["id"];
|
|
$updateMpin = $this->employeeModel->where('id', $id)->set('mpin', $mpin)->update();
|
|
if($updateMpin){
|
|
$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 {
|
|
$result = ['user_verification' => false , 'message' => "User not found"];
|
|
return $this->respond(['status' => 'failed','code' => 404,'data' => $result],200);
|
|
|
|
}
|
|
} catch (\Throwable $th) {
|
|
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;
|
|
|
|
if (isset($mobile_number))
|
|
{
|
|
$employeeData = $this->employeeModel->where('mobile', $mobile_number)->where('mpin', $old_mpin)->where('relationship', 'self')->first();
|
|
}else{
|
|
$employeeData = $this->employeeModel->where('email_corporate', $email_id)->where('mpin', $old_mpin)->where('relationship', 'self')->first();
|
|
}
|
|
|
|
|
|
|
|
if ($employeeData) {
|
|
$id= $employeeData["id"];
|
|
$updateMpin = $this->employeeModel->where('id', $id)->set('mpin', $mpin)->update();
|
|
if($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 {
|
|
$result = ['mpin_verification' => false , 'message' => "Wrong Mpin"];
|
|
return $this->respond(['status' => 'failed','code' => 404,'data' => $result],200);
|
|
|
|
}
|
|
} catch (\Throwable $th) {
|
|
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;
|
|
|
|
if (isset($mobile_number))
|
|
{
|
|
$employeeData = $this->employeeModel->where('mobile', $mobile_number)->where('relationship', 'self')->first();
|
|
}else{
|
|
$employeeData = $this->employeeModel->where('email_corporate', $email_id)->where('relationship', 'self')->first();
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
$result = JWTToken::encode($employeeData);
|
|
// Call the third-party API function
|
|
$apiResponse = $this->callThirdPartyAPI($this->request->getJSON(),'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($this->request->getJSON(),'getVerifiedUserData');
|
|
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 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;
|
|
|
|
if (isset($mobile_number))
|
|
{
|
|
$employeeData = $this->employeeModel->where('mobile', $mobile_number)->where('relationship', 'self')->first();
|
|
}else{
|
|
$employeeData = $this->employeeModel->where('email_corporate', $email_id)->where('relationship', 'self')->first();
|
|
}
|
|
|
|
if ($employeeData && $employeeData["mpin"] != null) {
|
|
|
|
return $this->respond(['status' => 'success','code' => 200,'data' => "Mpin - exist" , 'Mpin' =>$employeeData["mpin"]],200);
|
|
} else {
|
|
return $this->respond(['status' => 'failed','code' => 404,'data' => "Mpin - not found", 'Mpin' =>null],200);
|
|
}
|
|
} catch (\Exception $e) {
|
|
return $this->respond(['status' => 'failed','code' => 500,'data' => $e->getMessage()],500);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
} |