322 lines
12 KiB
PHP
Executable File
322 lines
12 KiB
PHP
Executable File
<?php
|
|
|
|
// declare(strict_types=1);
|
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
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........";
|
|
}
|
|
|
|
|
|
public function verifyEmployeeWithMobileNumber()
|
|
{
|
|
try {
|
|
$mobile_number = $this->request->getJSON()->mobile_number;
|
|
|
|
|
|
$employeeData = $this->employeeModel->where('mobile', $mobile_number)->where('relationship', 'self')->first();
|
|
|
|
if ($employeeData) {
|
|
|
|
$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 getVerifiedUserData()
|
|
{
|
|
try {
|
|
$mobile_number = $this->request->getJSON()->mobile_number;
|
|
$otp_verification = $this->request->getJSON()->otp_verification;
|
|
|
|
$employeeData = $this->employeeModel->where('mobile', $mobile_number)->where('relationship', 'self')->first();
|
|
|
|
if ($employeeData && $otp_verification == true || $employeeData && isset($this->request->getJSON()->login_by_hr)) {
|
|
$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);
|
|
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')->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 = $this->request->getJSON()->mobile_number;
|
|
$mpin = $this->request->getJSON()->mpin;
|
|
|
|
|
|
$employeeData = $this->employeeModel->where('mobile', $mobile_number)->where('relationship', 'self')->first();
|
|
|
|
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 = $this->request->getJSON()->mobile_number;
|
|
$old_mpin = $this->request->getJSON()->old_mpin;
|
|
$mpin = $this->request->getJSON()->new_mpin;
|
|
|
|
|
|
$employeeData = $this->employeeModel->where('mobile', $mobile_number)->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 = $this->request->getJSON()->mobile_number;
|
|
$mpin = $this->request->getJSON()->mpin;
|
|
|
|
$employeeData = $this->employeeModel->where('mobile', $mobile_number)->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);
|
|
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 checkMpin()
|
|
{
|
|
try {
|
|
$mobile_number = $this->request->getJSON()->mobile_number;
|
|
|
|
|
|
$employeeData = $this->employeeModel->where('mobile', $mobile_number)->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);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
} |