GWM : logout api and passport download api
This commit is contained in:
parent
15198ed89c
commit
6f37f65968
@ -22,7 +22,6 @@ $routes->group('api', ['filter' => 'appSignature'], function ($routes) {
|
||||
|
||||
// Login api
|
||||
$routes->post('auth/login', 'AuthController::login');
|
||||
$routes->get('auth/logout', 'AuthController::logout');
|
||||
$routes->get('auth/oauthClient', 'AuthController::oauthClient');
|
||||
$routes->post('auth/oauthlogin', 'AuthController::oauthlogin');
|
||||
|
||||
@ -44,6 +43,10 @@ $routes->group('api', ['filter' => 'appSignature'], function ($routes) {
|
||||
|
||||
$routes->group('api', ['filter' => ["jwtAuth:Org Admin,Travel Admin,Travel Agent,User","appSignature"] ], function ($routes) {
|
||||
|
||||
$routes->get('auth/logout', 'AuthController::logout');
|
||||
|
||||
$routes->get('downloadPassport', 'UserController::downloadPassport');
|
||||
|
||||
$routes->get('organizations/find/(:num)', 'OrganizationController::find/$1'); // Org admin, Travel admin, Travel agent,user
|
||||
$routes->get('users/find/(:num)', 'UserController::find/$1'); // Org admin, Travel admin, Travel agent,user
|
||||
$routes->post('plans/createOrEditPlan', 'PlanController::createOrEditPlan'); // Org admin, Travel admin, Travel agent,user
|
||||
|
||||
@ -411,14 +411,45 @@ class AuthController extends ResourceController
|
||||
public function logout()
|
||||
{
|
||||
$user_id = $this->request->getGet('user_id');
|
||||
$headerToken = $this->request->getHeaderLine('Authorization'); // Get the full header
|
||||
|
||||
// Remove "Bearer " prefix if present
|
||||
$token = null;
|
||||
if (!empty($headerToken)) {
|
||||
$token = str_replace('Bearer ', '', $headerToken);
|
||||
}
|
||||
|
||||
// Validate user existence and active status
|
||||
$user = $this->userModel->where('user_id', $user_id)->where('is_active', 1)->first();
|
||||
|
||||
if (!$user) {
|
||||
return $this->respond([
|
||||
'status' => 400,
|
||||
'message' => 'User not found or inactive'
|
||||
], 400);
|
||||
}
|
||||
|
||||
// Check token match
|
||||
if ($user['current_token'] !== $token) {
|
||||
return $this->respond([
|
||||
'status' => 401,
|
||||
'message' => 'Unknown login or invalid token'
|
||||
], 401);
|
||||
}
|
||||
|
||||
// Token matches — logout success
|
||||
$this->userModel->update($user_id, ['current_token' => null]);
|
||||
|
||||
return $this->respond(['message' => 'Logout successful']);
|
||||
return $this->respond([
|
||||
'status' => 200,
|
||||
'message' => 'Logout successful'
|
||||
], 200);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//old login code
|
||||
// public function login() { $rules = [ 'email' => 'required|valid_email', 'password' => 'required' ]; if (!$this->validate($rules)) { return $this->failValidationErrors($this->validator->getErrors()); } $data = $this->request->getJSON(); $user = $this->userModel->where('email', $data->email)->where('is_active',1)->first(); // print_r($user);die; if (!$user) { return $this->failUnauthorized('Invalid email or password'); } // Verify password (assuming it's hashed) if (!password_verify($data->password, $user['password'])) { return $this->failUnauthorized('Invalid email or password'); } //update last login time in last_login_at column $this->userModel->update($user['user_id'], ['last_login_at' => date('Y-m-d H:i:s')]); $user = $this->userModel->where('email', $data->email)->first(); if (!empty($user['last_login_at'])) { $user['last_login_at'] = date('d, M-Y h:iA', strtotime($user['last_login_at'])); } else { $user['last_login_at'] = null; } //get service for the organization $orgService = $this->organizationModel->find($user['org_id']); $user['service'] = json_decode($orgService['services_ids'],true); foreach ($user['service'] as $key => $value) { $serviceData = $this->serviceModel->find($value['service_id']); $user['service'][$key]['name'] = $serviceData['name']; $user['service'][$key]['icon'] = $serviceData['icon']; $user['service'][$key]['order'] = $serviceData['order']; } //get role $user['role'] = $this->userModel->getRole($user['role_id']); //find user has the all access $user['plan_action'] = getUserPlanCreationRestrictionStatus($user); // Generate JWT Token $token = generateJWT($user); return $this->respond([ 'status' => 200, 'message' => 'Login successful', 'token' => $token ]); }
|
||||
|
||||
|
||||
@ -871,6 +871,43 @@ public function userUploadTemplate()
|
||||
}
|
||||
|
||||
|
||||
public function downloadPassport()
|
||||
{
|
||||
$user_id = $this->request->getGet('user_id');
|
||||
|
||||
|
||||
|
||||
// Get user data
|
||||
$user = $this->userModel->select('passport_document')->where('user_id', $user_id)->first();
|
||||
|
||||
if (!$user || empty($user['passport_document'])) {
|
||||
return $this->respond([
|
||||
'status' => 404,
|
||||
'message' => 'Passport document not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
// Full path from DB
|
||||
$filePath = $user['passport_document'];
|
||||
|
||||
// If file path stored includes domain or base path, extract only the actual file path
|
||||
// Example: /home3/venbaehn/apitest.tripapprovaltool.com/tstat_be/assets/images/passport/filename.pdf
|
||||
if (!file_exists($filePath)) {
|
||||
return $this->respond([
|
||||
'status' => 404,
|
||||
'message' => 'File not found on server'
|
||||
], 404);
|
||||
}
|
||||
|
||||
// Extract just the filename (for download name)
|
||||
$fileName = basename($filePath);
|
||||
|
||||
// Return file as downloadable response
|
||||
return $this->response->download($filePath, null)->setFileName($fileName);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user