nhance/app/Controllers/RestAuthenticationController.php
2025-07-10 14:46:14 +05:30

1236 lines
52 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 App\Models\HRAccessControlModel;
use App\Models\ClientModel;
use Firebase\JWT\JWT;
// require_once('../vendor/autoload.php');
class RestAuthenticationController extends AdminController
{
use ResponseTrait;
protected $myLogger;
protected $employeeModel;
protected $authHistoryModel;
protected $hrModel;
protected $hrAccessControlModel;
protected $clientModel;
public function __construct()
{
// set_session_context('Client');
$this->myLogger = \Config\Services::mylogger();
$this->employeeModel = new EmployeeModel();
$this->authHistoryModel = new AuthHistoryModel();
$this->hrModel = new LevelContactModel();
$this->hrAccessControlModel = new HRAccessControlModel();
$this->clientModel = new ClientModel();
}
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........";
}
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', ['active', 'expired'])
->first();
if (isset($employeeData['employee_id']))
{
$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) {
$this->myLogger->logme("error", ($th->getMessage().' --- '.$th->getLine()));
return $this->respond(['status' => 'failed','code' => 500,'data' => $th],500);
}
}
public function verifyEmployeeWithEmailId()
{
try {
log_message('error', json_encode($this->request->getJSON()));
$email = $this->request->getJSON()->email;
$client_id = $this->request->getJSON()->client_id ?? null;
$builder = $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')
->whereIn('employees.emp_status', ['active', 'expired'])
->where('employees.email_corporate', $email)
->where('EP.is_active', 1)
->whereIn('EP.status', ['active', 'expired']);
if (!empty($client_id)) {
$builder->where('employees.client_id', $client_id);
}
$builder->orderBy('employees.id', 'desc');
$employeeData = $builder->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();
$builder = $this->employeeModel
->where('email_corporate', $email)
->where('relationship', 'Self')
->where('is_active', 1)
->where('id', $employeeData['employee_id']);
if (!empty($client_id)) {
$builder->where('client_id', $client_id);
}
$update = $builder->set(['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['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 {
$result = ['user_verification' => false, 'message' => "User not found"];
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => $result], 200);
}
} catch (\Throwable $th) {
$this->myLogger->logme("error", ($th->getMessage().' --- '.$th->getLine()));
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $th], 500);
}
}
public function updateEmpOTP()
{
$email = $this->request->getJSON()->email;
$otp = $this->request->getJSON()->otp;
$client_id = $this->request->getJSON()->client_id ?? null;
$employee_id = $this->request->getJSON()->employee_id ?? null;
// $builder = $this->employeeModel
// ->where('email_corporate', $email)
// ->where('relationship', 'Self')
// ->where('is_active', 1);
$builder = $this->employeeModel
->join('employee_polices EP', 'EP.employee_id = employees.id', 'inner')
->where('employees.email_corporate', $email)
->where('employees.relationship', 'Self')
->where('employees.is_active', 1)
->whereIn('employees.emp_status', ['active', 'expired'])
->where('EP.is_active', 1)
->whereIn('EP.status', ['active', 'expired']);
if (!empty($client_id)) {
$builder->where('client_id', $client_id);
}
if (!empty($employee_id)) {
$builder->where('id', $employee_id);
}
$builder->set(['otp' => $otp])->update();
return true;
}
// public function updateEmpMPIN()
// {
// $requestData = $this->request->getJSON();
// print_r($requestData); die;
// $mobile_number = $requestData->mobile_number ?? null;
// $email_id = $requestData->email_id ?? null;
// $new_mpin = $requestData->new_mpin ?? $requestData->mpin ?? null;
// $old_mpin = $requestData->old_mpin ?? null;
// $is_mpin_skipped = $requestData->is_mpin_skipped ?? null;
// $is_biometric_enabled = $requestData->is_biometric_enabled ?? null;
// if (!$new_mpin) {
// return $this->response->setJSON(['status' => false, 'message' => 'MPIN is required.']);
// }
// $query = $this->employeeModel->where('relationship', 'self');
// if ($mobile_number) {
// $query->where('mobile', $mobile_number);
// } elseif ($email_id) {
// $query->where('email_corporate', $email_id);
// } else {
// return $this->response->setJSON(['status' => false, 'message' => 'Mobile number or Email ID is required.']);
// }
// if (!empty($old_mpin)) {
// $query->where('mpin', $old_mpin);
// }
// // Fetch employee data
// $employeeData = $query->first();
// if (!$employeeData) {
// return $this->response->setJSON(['status' => false, 'message' => 'Employee not found or invalid MPIN.']);
// }
// if(!empty($is_mpin_skipped) && !empty($is_biometric_enabled)){
// $mpin_data_to_updata = [
// 'mpin' => $new_mpin,
// 'is_mpin_skipped' => $is_mpin_skipped,
// 'is_biometric_enabled' => $is_biometric_enabled
// ];
// }else{
// $mpin_data_to_updata = ['mpin' => $new_mpin];
// }
// // Update MPIN
// $updated = $this->employeeModel->update($employeeData['id'], $mpin_data_to_updata);
// return true;
// }
public function updateEmpMPIN()
{
try {
log_message('info', 'MPIN update request received.');
$requestData = $this->request->getJSON();
log_message('debug', 'Request data: ' . json_encode($requestData));
$client_id = $requestData->client_id ?? null;
$employee_id = $requestData->employee_id ?? null;
$mpin = $requestData->mpin ?? null;
if (!$mpin) {
log_message('error', 'MPIN is missing.');
return $this->response->setJSON(['status' => false, 'message' => 'MPIN is required.']);
}
if (!$employee_id) {
log_message('error', 'employee_id is missing.');
return $this->response->setJSON(['status' => false, 'message' => 'employee_id is required.']);
}
$updated = $this->employeeModel->where('id', $employee_id)->set(['mpin' => $mpin])->update();
if ($updated) {
log_message('info', "MPIN updated successfully for employee ID: {$employee_id}");
return $this->response->setJSON([
'status' => true,
'message' => 'MPIN updated successfully.'
]);
} else {
log_message('error', "Failed to update MPIN for employee ID: {$employee_id}");
return $this->response->setJSON([
'status' => false,
'message' => 'Failed to update MPIN.'
]);
}
} catch (\Exception $e) {
log_message('critical', 'Exception in updateEmpMPIN: ' . $e->getMessage());
return $this->response->setJSON([
'status' => false,
'message' => 'Unexpected error occurred.',
'error' => $e->getMessage()
]);
}
}
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;
$client_id = $this->request->getJSON()->client_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();
$builder = $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', ['active', 'expired'])
->where('employees.is_active', 1)
->whereIn('employees.emp_status', ['active', 'expired']);
if (!empty($client_id)) {
$builder->where('employees.client_id', $client_id);
}
$employeeData = $builder->orderBy('employees.id', 'desc')->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();
$builder = $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', ['active', 'expired'])
->where('employees.is_active', 1)
->whereIn('employees.emp_status', ['active', 'expired'])
->where('otp', $otp);
if (!empty($client_id)) {
$builder->where('employees.client_id', $client_id);
}
$employeeData = $builder->orderBy('employees.id', 'desc')->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('id', $employeeData['id'])->where('otp', $otp)->where('relationship', 'self')->set(['otp'=>null])->update();
}
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 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 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;
$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 {
$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 updateHROTP()
{
$email = $this->request->getJSON()->email;
$otp = $this->request->getJSON()->otp;
$this->hrModel->where('email', $email) ->where('contact_type', 'client')
->where('is_active', 1)->set(array('otp' => $otp))
->update();
return true;
}
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_number) ? $this->request->getJSON()->mobile_number : 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_number)){
$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();
}
$HRAccessData = $this->getHRAccessData( $hrData['0']['id'] , 'post_enrollment');
if(isset($HRAccessData['allowed_modules'])){ $hrData['0']['allowed_modules'] = json_decode($HRAccessData['allowed_modules'],true)['post']; }else{ $hrData['0']['allowed_modules'] = []; }
$hrData['0']['token_type'] = 'post';
$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 getHRAccessData( $hr_id = null , $request_for = 'post_enrollment')
{
$hr_id = $this->request->getGet('hr_id') ?? $hr_id;
$request_for = $this->request->getGet('request_for') ?? $request_for;
if($request_for == 'pre_enrollment'){ $idField = 'pre_hr_id'; }else{ $idField = 'post_hr_id'; }
$data = $this->hrAccessControlModel->where($idField , $hr_id)->where('is_active' , 1)->first();
if($request_for == 'post_enrollment'){ return $data ?? []; }
return $this->respond(['status' => (($data) ? 'success' : 'failed'),'code' => (($data) ? 200 : 404),'data' => $data ], 200);
}
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;
$client_id = $this->request->getJSON()->client_id ?? null;
if (isset($mobile_number)) {
// $employeeData = $this->employeeModel->where('mobile', $mobile_number)->where('relationship', 'self')->first();
$builder = $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', ['active', 'expired'])
->where('employees.is_active', 1)
->whereIn('employees.emp_status', ['active', 'expired']);
if (!empty($client_id)) {
$builder->where('employees.client_id', $client_id);
}
$employeeData = $builder->orderBy('employees.id', 'desc')->first();
} else {
// $employeeData = $this->employeeModel->where('email_corporate', $email_id)->where('relationship', 'self')->first();
$builder = $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', ['active', 'expired'])
->where('employees.is_active', 1)
->whereIn('employees.emp_status', ['active', 'expired']);
if (!empty($client_id)) {
$builder->where('employees.client_id', $client_id);
}
$employeeData = $builder->orderBy('employees.id', 'desc')->first();
}
$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
];
if ($employeeData) {
$id= $employeeData["id"];
$updateMpin = $this->employeeModel->where('id', $id)->set( $mpin_data_to_updata)->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) {
$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;
$client_id = $this->request->getJSON()->client_id ?? null;
// 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 (isset($mobile_number)) {
// $employeeData = $this->employeeModel->where('mobile', $mobile_number)->where('mpin', $old_mpin)->where('relationship', 'self')->first();
$builder = $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', ['active'])
->where('employees.is_active', 1)
->whereIn('employees.emp_status', ['active'])
->where('employees.mpin', $old_mpin);
if (!empty($client_id)) {
$builder->where('employees.client_id', $client_id);
}
$employeeData = $builder->orderBy('employees.id', 'desc')->first();
} else {
// $employeeData = $this->employeeModel->where('email_corporate', $email_id)->where('mpin', $old_mpin)->where('relationship', 'self')->first();
$builder = $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', ['active'])
->where('employees.is_active', 1)
->whereIn('employees.emp_status', ['active'])
->where('employees.mpin', $old_mpin);
if (!empty($client_id)) {
$builder->where('employees.client_id', $client_id);
}
$employeeData = $builder->orderBy('employees.id', 'desc')->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) {
$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;
// 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', ['active'])
// ->where('employees.is_active', 1)
// ->whereIn('employees.emp_status', ['active'])
// ->get();
// }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('employee_polices.is_active', 1)
// ->whereIn('employee_polices.status', ['active'])
// ->where('employees.is_active', 1)
// ->whereIn('employees.emp_status', ['active'])
// ->where("employees.relationship", "Self") // RAW SQL condition
// ->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);
// // $result = $employeeData;
// 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 verifyMpin()
{
try {
$requestData = $this->request->getJSON(true);
$mobile_number = $requestData['mobile_number'] ?? null;
$email_id = $requestData['email_id'] ?? null;
$mpin = $requestData['mpin'] ?? null;
$client_id = $requestData['client_id'] ?? null;
// print_r($mpin);
if (!$mpin) {
return $this->respond(['status' => 'failed', 'code' => 400, 'data' => 'MPIN is required'], 400);
}
if ($mobile_number) {
$builder = $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', ['active'])
->where('employees.is_active', 1)
->whereIn('employees.emp_status', ['active']);
if (!empty($client_id)) {
$builder->where('employees.client_id', $client_id);
}
$employeeData = $builder->orderBy('employees.id', 'desc')->first();
} elseif ($email_id) {
$builder = $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', ['active'])
->where('employees.is_active', 1)
->whereIn('employees.emp_status', ['active']);
if (!empty($client_id)) {
$builder->where('employees.client_id', $client_id);
}
$employeeData = $builder->orderBy('employees.id', 'desc')->first();
} else {
return $this->respond(['status' => 'failed', 'code' => 400, 'data' => 'Mobile number or Email ID is required'], 400);
}
// print_r($employeeData); die;
if ($employeeData && isset($employeeData['mpin']) && $mpin === $employeeData['mpin']) {
$auth = HttpRequestHelper::getRequestInfo();
if ($auth) {
$this->authHistoryModel->insert([
'user_id' => $employeeData['id'],
'user_type' => 'employee',
'ip' => $auth['ip'],
'platform' => $auth['platform'],
'broswer' => $auth['browser'], // Note: typo in 'broswer' retained if it's your actual DB field
]);
}
$token = JWTToken::encode($employeeData);
return $this->respond([
'status' => 'success',
'code' => 200,
'data' => $token
], 200);
} else {
return $this->respond([
'status' => 'failed',
'code' => 404,
'data' => 'Invalid MPIN'
], 200);
}
} catch (\Exception $e) {
$this->myLogger->logme("error", ($e->getMessage().' --- '.$e->getLine()));
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;
// $mpin = $this->request->getJSON()->mpin;
$client_id = $this->request->getJSON()->client_id ?? null;
if (isset($mobile_number))
{
// $employeeData = $this->employeeModel->where('mobile', $mobile_number)->where('relationship', 'self')->first();
$builder = $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', ['active', 'expired'])
->where('employees.is_active', 1)
->whereIn('employees.emp_status', ['active', 'expired']);
if (!empty($client_id)) {
$builder->where('employees.client_id', $client_id);
}
$employeeData = $builder->orderBy('employees.id', 'desc')->first();
}else{
// $employeeData = $this->employeeModel->where('email_corporate', $email_id)->where('relationship', 'self')->first();
$builder = $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', ['active', 'expired'])
->where('employees.is_active', 1)
->whereIn('employees.emp_status', ['active', 'expired']);
if (!empty($client_id)) {
$builder->where('employees.client_id', $client_id);
}
$employeeData = $builder->orderBy('employees.id', 'desc')->first();
}
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);
}
} 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;
$client_id = $json->client_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);
}
// Step 2: Fetch employee data
if ($mobile_number) {
log_message('info', 'Looking up employee by mobile number: ' . $mobile_number);
$builder = $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)
->where('employee_polices.status', ['active'])
->where('employees.is_active', 1)
->where('employees.emp_status', ['active']);
if (!empty($client_id)) {
$builder->where('employees.client_id', $client_id);
}
$employeeData = $builder->orderBy('employees.id', 'desc')->first();
} else {
log_message('info', 'Looking up employee by email: ' . $email_id);
$builder = $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)
->where('employee_polices.status', ['active'])
->where('employees.is_active', 1)
->where('employees.emp_status', ['active']);
if (!empty($client_id)) {
$builder->where('employees.client_id', $client_id);
}
$employeeData = $builder->orderBy('employees.id', 'desc')->first();
}
log_message('info', '$employeeData : ' . json_encode($employeeData));
// 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) {
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->respond(['status' => 'failed','code' => 404,'data' => "Employee not found"],200); }
} catch (\Throwable $e) {
$this->myLogger->logme("error", ($e->getMessage().' --- '.$e->getLine()));
return $this->respond([
'status' => 'failed',
'code' => 500,
'message' => 'Server error: ' . $e->getMessage()
], 500);
}
}
// TokenController.php
public function bookstackLoginToken()
{
$session = \Config\Services::session();
$user_data = session()->get('userData');
$user = $user_data->email;
$secret = getenv('bookstack.token');
// $user = 'staff@staff.com';
$name = $user_data->first_name ?? 'User';
$payload = [
'email' => $user,
'name' => $name,
'exp' => time() + (60 * 60 * 24) // expires in 1 day
];
// dd($payload);
$token = base64_encode(json_encode($payload));
$signature = hash_hmac('sha256', $token, $secret);
$bookStackUrl = getenv('bookstack.baseURL');
$url = $bookStackUrl . urlencode($token) . '&sig=' . $signature;
return redirect()->to($url);
}
public function getPostEmployeeDataForAuth()
{
$params = $this->request->getJSON(true);
// print_r( $params); die;
$mobile_number = $params['mobile_number'] ?? null;
$email_id = $params['email_id'] ?? null;
$otp = $params['otp'] ?? null;
$old_mpin = $params['old_mpin'] ?? null;
if (empty($mobile_number) && empty($email_id)) {
return $this->respond([
'status' => 'failed',
'message' => 'Mobile number or Email ID is required.',
'data' => [],
], 400);
}
if (!empty($mobile_number)) {
$builder = $this->employeeModel
->select('employees.id as employee_id, employees.*')
->join('employee_polices EP', 'EP.employee_id = employees.id', 'inner')
->where('employees.is_active', 1)
->where("TRIM(employees.relationship) = 'self'", null, false)
->whereIn('employees.emp_status', ['active', 'expired'])
->where('employees.mobile', $mobile_number)
->where('EP.is_active', 1)
->whereIn('EP.status', ['active', 'expired']);
if (!empty($old_mpin)) {
$builder->where('employees.mpin', $old_mpin);
}
$employeeData = $builder->orderBy('employees.id', 'desc')->first();
} else {
$builder = $this->employeeModel
->select('employees.id as employee_id, employees.*')
->join('employee_polices EP', 'EP.employee_id = employees.id', 'inner')
->where('employees.is_active', 1)
->where("TRIM(employees.relationship) = 'self'", null, false)
->whereIn('employees.emp_status', ['active', 'expired'])
->where('employees.email_corporate', $email_id)
->where('EP.is_active', 1)
->whereIn('EP.status', ['active', 'expired']);
if (!empty($otp)) {
$builder->where('employees.otp', $otp);
}
if (!empty($old_mpin)) {
$builder->where('employees.mpin', $old_mpin);
}
$employeeData = $builder->orderBy('employees.id', 'desc')->first();
}
if ($employeeData) {
$client_data = $this->clientModel->where('is_active', 1)->where('id', $employeeData['client_id'])->first();
$employeeData['client_short_name'] = $client_data['short_name'];
return $this->respond([
'status' => 'success',
'data' => $employeeData,
], 200);
}
return $this->respond([
'status' => 'failed',
'message' => 'No employee found.',
'data' => [],
], 404);
}
}