GWM : oauth fix

This commit is contained in:
Gowtham M 2025-07-24 11:50:58 +05:30
parent be6b89a2c0
commit 5e9b34a40b
4 changed files with 201 additions and 75 deletions

View File

@ -21,6 +21,7 @@ $routes->get('metadata', 'SamlController::metadata');
$routes->post('auth/login', 'AuthController::login'); $routes->post('auth/login', 'AuthController::login');
$routes->get('auth/oauthClient', 'AuthController::oauthClient'); $routes->get('auth/oauthClient', 'AuthController::oauthClient');
$routes->post('auth/oauthlogin', 'AuthController::oauthlogin'); $routes->post('auth/oauthlogin', 'AuthController::oauthlogin');
$routes->get('auth/msRedirectionHandler', 'AuthController::msRedirectionHandler');
//forgot password //forgot password

View File

@ -40,7 +40,9 @@ class AuthController extends ResourceController
$this->provider = new Azure([ $this->provider = new Azure([
'clientId' => "9af66888-eb85-48ec-b3a0-ba857734f52b", 'clientId' => "9af66888-eb85-48ec-b3a0-ba857734f52b",
'clientSecret' => "N2_8Q~FBEXtoZft2UgJ6FQZCh3ZIbJydYgxREbUb", 'clientSecret' => "N2_8Q~FBEXtoZft2UgJ6FQZCh3ZIbJydYgxREbUb",
'redirectUri' => "https://apitest.tripapprovaltool.com/tstat/index.html#/authredirection", // 'redirectUri' => "https://apitest.tripapprovaltool.com/tstat/authredirection",
// 'redirectUri' => "https://apitest.tripapprovaltool.com/tstat_be/auth/msRedirectionHandler",
'redirectUri' => env('MS_REDIRECTION_URL'),
'tenant' => "284bf2b6-6dd8-4b46-881c-de45fde35736", 'tenant' => "284bf2b6-6dd8-4b46-881c-de45fde35736",
]); ]);
$this->provider->scope = [ $this->provider->scope = [
@ -314,5 +316,13 @@ class AuthController extends ResourceController
return googleOAuthLogin(false); return googleOAuthLogin(false);
} }
public function msRedirectionHandler()
{
$FeURL = env('FE_URL') . '#/authredirection?code=' . $this->request->getGet('code');
return redirect()->to($FeURL);
}
} }

View File

@ -1434,8 +1434,8 @@ class PlanController extends ResourceController
{ {
echo '<pre>'; echo '<pre>';
sendPlanCreationMail(5); // sendPlanCreationMail(23);
print_r(getPlanApproverAction(5)); die; print_r(getPlanApproverAction(32)); die;
// echo '<pre>'; // echo '<pre>';
// print_r(getAllowedClassForUser(2,4)['hotel']); die; // print_r(getAllowedClassForUser(2,4)['hotel']); die;

View File

@ -299,33 +299,139 @@ if (!function_exists('sendUserCreationMail')) {
} }
if (!function_exists('send_email')) { if (!function_exists('send_email')) {
function send_email($org_id , $toEmailId, $template , $userData , $planData = null, $url = null) // function send_email($org_id , $toEmailId, $template , $userData , $planData = null, $url = null)
// {
// try {
// // Fetch Organization data
// $organizationModel = new OrganizationModel();
// $orgData = $organizationModel->where('org_id', $org_id )->first();
// // Fetch Template data
// $mailTemplateModel = new MailTemplateModel();
// $templateData = $mailTemplateModel->where('org_id', $org_id )->where('template_name', $template )->first();
// log_message('info', "Template : ". $template ." , ToMail : ". $toEmailId);
// //replace the placeholder
// foreach ($userData as $key => $value) {
// $placeHolder = '%'.$key.'%';
// $templateData['body_html'] = str_replace($placeHolder, $value ?? '', $templateData['body_html']);
// }
// if(!empty($planData)){
// foreach ($planData as $key => $value) {
// $placeHolder = '%'.$key.'%';
// $templateData['body_html'] = str_replace($placeHolder, $value ?? '', $templateData['body_html']);
// }
// if($planData['exceptional_plan_reason'] != null && $planData['exceptional_plan_reason'] != '')
// {
// $reasonHtmlString = '<table style="width:100%; border-collapse:collapse; font-family:Arial, sans-serif;">
// <tr>
// <td style="text-align:left;background-color:#ffdbdb;color:#960000;padding:7px 15px;font-weight:bold;border-top-left-radius:5px;border-top-right-radius:5px;border-bottom:1px solid #c65e56;">
// Reason for travel policy exception
// </td>
// </tr>
// <tr>
// <td style="text-align:left;background-color:#fff5f5;color:#960000ab;padding:10px 15px;border-bottom-left-radius:5px;border-bottom-right-radius:5px;">
// '.$planData['exceptional_plan_reason'].'
// </td>
// </tr>
// </table>';
// $templateData['body_html'] = $templateData['body_html'].''.$reasonHtmlString;
// }
// }
// if($url != null){
// $placeHolder = '%trip_review_link%';
// $templateData['body_html'] = str_replace($placeHolder, $url , $templateData['body_html']);
// }
// $subject = $templateData['subject'];
// $html = $templateData['body_html'];
// $email = \Config\Services::email(false);
// $email->initialize([
// 'protocol' => 'smtp',
// 'SMTPHost' => $orgData['mail_host'],
// 'SMTPUser' => $orgData['mail_user_name'],
// 'SMTPPass' => $orgData['mail_password'],
// 'SMTPPort' => (int)$orgData['mail_port'],
// 'SMTPCrypto' => 'tls',
// 'mailType' => 'html',
// 'charset' => 'utf-8',
// 'newline' => "\r\n",
// 'wordWrap' => true,
// ]);
// $email->setFrom($orgData['sender_email'], $orgData['name']);
// $email->setTo($toEmailId);
// $email->setSubject($subject);
// $email->setMessage($html);
// if(!empty($planData)){
// $email->attach($planData['pdf_file_path']);
// }
// // Try sending email
// if ($email->send()) {
// log_message('info', "[EMAIL SENT] To approver/traveller : {$toEmailId}, Template: {$template}");
// return ['status' => 'success', 'code' => 200, 'message' => 'Email sent successfully', 'data' => $toEmailId];
// } else {
// // Email send failed, log clean debug info
// $debugInfo = $email->printDebugger(['headers', 'subject', 'body']);
// log_message('error', "[EMAIL FAILED] To approver/traveller : {$toEmailId}, Template: {$template}\nError:\n" . strip_tags($debugInfo));
// return ['status' => 'failed', 'code' => 500, 'message' => 'Email sending failed', 'data' => $toEmailId];
// }
// } catch (\Exception $e) {
// log_message('critical', "[EMAIL EXCEPTION] To approver/traveller : {$toEmailId}, Error: " . $e->getMessage().$e->getLine());
// return ['status' => 'failed', 'code' => 500, 'message' => 'Exception: ' . $e->getMessage(), 'data' => $toEmailId];
// }
// }
if (!function_exists('send_email')) {
function send_email($org_id, $toEmailId, $template, $userData, $planData = null, $url = null)
{ {
try { try {
// Load models
$organizationModel = new \App\Models\OrganizationModel();
$mailTemplateModel = new \App\Models\MailTemplateModel();
// Fetch Organization data // Fetch organization & template
$organizationModel = new OrganizationModel(); $orgData = $organizationModel->where('org_id', $org_id)->first();
$orgData = $organizationModel->where('org_id', $org_id )->first(); $templateData = $mailTemplateModel->where('org_id', $org_id)->where('template_name', $template)->first();
// Fetch Template data
$mailTemplateModel = new MailTemplateModel();
$templateData = $mailTemplateModel->where('org_id', $org_id )->where('template_name', $template )->first();
log_message('info', "Template : ". $template ." , ToMail : ". $toEmailId);
//replace the placeholder if (!$orgData || !$templateData) {
log_message('error', "[EMAIL ERROR] Missing org/template data for org_id={$org_id}, template={$template}");
return ['status' => 'failed', 'code' => 404, 'message' => 'Missing organization or template data'];
}
// Replace placeholders from user data
foreach ($userData as $key => $value) { foreach ($userData as $key => $value) {
$placeHolder = '%'.$key.'%'; $templateData['body_html'] = str_replace('%' . $key . '%', $value ?? '', $templateData['body_html']);
$templateData['body_html'] = str_replace($placeHolder, $value ?? '', $templateData['body_html']);
} }
if(!empty($planData)){ // Replace placeholders from plan data
if (!empty($planData)) {
foreach ($planData as $key => $value) { foreach ($planData as $key => $value) {
$placeHolder = '%'.$key.'%'; $templateData['body_html'] = str_replace('%' . $key . '%', $value ?? '', $templateData['body_html']);
$templateData['body_html'] = str_replace($placeHolder, $value ?? '', $templateData['body_html']);
} }
if($planData['exceptional_plan_reason'] != null && $planData['exceptional_plan_reason'] != '') // Exceptional reason block
{ if (!empty($planData['exceptional_plan_reason'])) {
$reasonHtmlString = '<table style="width:100%; border-collapse:collapse; font-family:Arial, sans-serif;"> $reasonHtml = '
<table style="width:100%; border-collapse:collapse; font-family:Arial, sans-serif;">
<tr> <tr>
<td style="text-align:left;background-color:#ffdbdb;color:#960000;padding:7px 15px;font-weight:bold;border-top-left-radius:5px;border-top-right-radius:5px;border-bottom:1px solid #c65e56;"> <td style="text-align:left;background-color:#ffdbdb;color:#960000;padding:7px 15px;font-weight:bold;border-top-left-radius:5px;border-top-right-radius:5px;border-bottom:1px solid #c65e56;">
Reason for travel policy exception Reason for travel policy exception
@ -333,65 +439,74 @@ if (!function_exists('send_email')) {
</tr> </tr>
<tr> <tr>
<td style="text-align:left;background-color:#fff5f5;color:#960000ab;padding:10px 15px;border-bottom-left-radius:5px;border-bottom-right-radius:5px;"> <td style="text-align:left;background-color:#fff5f5;color:#960000ab;padding:10px 15px;border-bottom-left-radius:5px;border-bottom-right-radius:5px;">
'.$planData['exceptional_plan_reason'].' ' . esc($planData['exceptional_plan_reason']) . '
</td> </td>
</tr> </tr>
</table>'; </table>';
$templateData['body_html'] .= $reasonHtml;
$templateData['body_html'] = $templateData['body_html'].''.$reasonHtmlString; }
} }
// Add review URL if present
if ($url !== null) {
$templateData['body_html'] = str_replace('%trip_review_link%', $url, $templateData['body_html']);
} }
if($url != null){ // Initialize email
$email = \Config\Services::email(false);
$placeHolder = '%trip_review_link%';
$templateData['body_html'] = str_replace($placeHolder, $url , $templateData['body_html']);
}
$subject = $templateData['subject'];
$html = $templateData['body_html'];
$email = \Config\Services::email(true);
$email->initialize([ $email->initialize([
'protocol' => 'smtp', 'protocol' => 'smtp',
'SMTPHost' => $orgData['mail_host'], 'SMTPHost' => $orgData['mail_host'],
'SMTPUser' => $orgData['mail_user_name'], 'SMTPUser' => $orgData['mail_user_name'],
'SMTPPass' => $orgData['mail_password'], 'SMTPPass' => $orgData['mail_password'], // Use app password if Gmail
'SMTPPort' => (int)$orgData['mail_port'], 'SMTPPort' => (int) $orgData['mail_port'],
'mailType' => 'html' 'SMTPCrypto' => 'tls', // ensure STARTTLS
'mailType' => 'html',
'charset' => 'utf-8',
'newline' => "\r\n",
'wordWrap' => true,
]); ]);
$email->setFrom($orgData['sender_email'], $orgData['name']);
$email->setTo($toEmailId);
$email->setSubject($subject);
$email->setMessage($html);
if(!empty($planData)){ $email->setFrom($orgData['sender_email'], $orgData['name'] ?? 'TripApprovalTool');
$email->attach($planData['pdf_file_path']); $email->setTo($toEmailId);
$email->setSubject($templateData['subject']);
$email->setMessage($templateData['body_html']);
// Attach file if exists
// if (!empty($planData['pdf_file_path']) && file_exists($planData['pdf_file_path'])) {
// $email->attach($planData['pdf_file_path']);
// }
if (!empty($planData['pdf_file_path']) && file_exists($planData['pdf_file_path']) && is_readable($planData['pdf_file_path'])) {
$email->attach(
$planData['pdf_file_path'],
'attachment',
basename($planData['pdf_file_path']),
'application/pdf'
);
} else {
log_message('error', "[EMAIL] PDF file not attached. Path: {$planData['pdf_file_path']}");
} }
// Send email
// Try sending email
if ($email->send()) { if ($email->send()) {
log_message('info', "[EMAIL SENT] To approver/traveller : {$toEmailId}, Template: {$template}"); log_message('info', "[EMAIL SENT] To: {$toEmailId}, Template: {$template}");
return ['status' => 'success', 'code' => 200, 'message' => 'Email sent successfully', 'data' => $toEmailId]; return ['status' => 'success', 'code' => 200, 'message' => 'Email sent successfully', 'data' => $toEmailId];
} else { } else {
// Email send failed, log clean debug info // Log full debugger info
$debugInfo = $email->printDebugger(['headers', 'subject', 'body']); $debug = $email->printDebugger(['headers', 'subject', 'body']);
log_message('error', "[EMAIL FAILED] To approver/traveller : {$toEmailId}, Template: {$template}\nError:\n" . strip_tags($debugInfo)); log_message('error', "[EMAIL FAILED] To: {$toEmailId}, Template: {$template}\n" . strip_tags($debug));
return ['status' => 'failed', 'code' => 500, 'message' => 'Email sending failed', 'debug' => strip_tags($debug)];
return ['status' => 'failed', 'code' => 500, 'message' => 'Email sending failed', 'data' => $toEmailId];
} }
} catch (\Exception $e) { } catch (\Throwable $e) {
log_message('critical', "[EMAIL EXCEPTION] To approver/traveller : {$toEmailId}, Error: " . $e->getMessage().$e->getLine()); log_message('critical', "[EMAIL EXCEPTION] To: {$toEmailId}, Error: " . $e->getMessage() . ' on line ' . $e->getLine());
return ['status' => 'failed', 'code' => 500, 'message' => 'Exception: ' . $e->getMessage(), 'data' => $toEmailId]; return ['status' => 'failed', 'code' => 500, 'message' => 'Exception: ' . $e->getMessage()];
} }
} }
}
} }
if (!function_exists('send_email_agent')) { if (!function_exists('send_email_agent')) {