File: /home/cafsindia/login_cafsindia_com/application/controllers/Login.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function index(){
if($this->is_logged_in()){
redirect('home');
}else{
$this->form_validation->set_rules('username', 'lang:login_undername', 'callback_login_check');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
if($this->form_validation->run() == FALSE){
$this->load->view('login');
}else{
redirect('home');
}
}
}
public function is_logged_in(){
return ($this->session->userdata('logged_id') != FALSE);
}
// EMPLOYEE LOGIN
public function corp_login(){
$user_name = $this->input->post('corp_user_name');
$password = $this->input->post('corp_password');
$enc_pass = $this->Module->cryptoEncrypt(md5($password)); // [MS 08-11-2024]
$query = $this->db->get_where('employees', array('user_name' => $user_name, 'password' => $enc_pass, 'trans_status' => 1), 1);
if($query->num_rows() == 1){
$logged_user_info = $query->row();
// $this->set_session_value("EMPLOYEE",$logged_user_info);
$login_with_otp = (int)$logged_user_info->login_with_otp;
$login_email = $logged_user_info->email;
$employee_status = (int)$logged_user_info->employee_status;
$resend = "generate_otp";
if($employee_status === 1){
//IF CONDITION FOR CHECK A OTP GENERATE
// $login_with_otp = 2;
if((int)$login_with_otp === 1){
$otp_code_msg = $this->send_otp_mail($login_email,$user_name,$password,$resend);
echo json_encode(array('success' => $otp_code_msg['success'], 'message' => $otp_code_msg['message'],'login_with_otp' => $login_with_otp));
}else{
$this->set_session_value("EMPLOYEE",$logged_user_info);
echo json_encode(array('success' => TRUE, 'message' => "Login Success !!!",'login_with_otp' => $login_with_otp));
}
}else{
echo json_encode(array('success' => false, 'message' => "You are not an Authorised Employee..! Please Contact Admin?"));
}
}else{
echo json_encode(array('success' => false, 'message' => "Invalid user name / password"));
}
}
//FUNCTION FOR RESEND OTP TO LOGIN MAIL
public function resend_otp(){
$user_name = $_SESSION['user_name'];
$password = $_SESSION['password'];
$login_email = $_SESSION['email'];
$resend = "resend_otp";
$otp_code_msg = $this->send_otp_mail($login_email,$user_name,$password,$resend);
echo json_encode(array('success' => $otp_code_msg['success'], 'message' => $otp_code_msg['message']));
// }else{
// echo json_encode(array('success' => false, 'message' => "Invalid user name / password"));
// }
}
//MAIL OTP CHECK FOR LOGIN VALID OR NOT
public function corp_login_otp(){
$login_otp = $this->input->post('corp_otp');
$user_name = $_SESSION['user_name'];
$password = $_SESSION['password'];
$session_otp = $_SESSION['login_otp'];
$enc_pass = $this->Module->cryptoEncrypt(md5($password)); // [MS 08-11-2024]
//CHECK SESSION OTP IS EQUAL TO LOGIN OTP FOR LOGIN STATUS SUCCESS OR NOT
if("$session_otp" === "$login_otp"){
$query = $this->db->get_where('employees', array('user_name' => $user_name, 'password' => $enc_pass, 'trans_status' => 1), 1);
if($query->num_rows() == 1){
$logged_user_info = $query->row();
$this->set_session_value("EMPLOYEE",$logged_user_info);
echo json_encode(array('success' => TRUE, 'message' => "Login Success !!!"));
}
}else{
echo json_encode(array('success' => false, 'message' => "Invalid OTP"));
}
}
// SET ALL SESSION VALUE FOR BOTH CUSTOMER AND EMPLOYEE
public function set_session_value($logged_type,$logged_user_info){
$this->session->set_userdata('logged_type',$logged_type);
$this->session->set_userdata('logged_id', $logged_user_info->prime_employees_id);
$this->session->set_userdata('logged_role', $logged_user_info->role);
$this->session->set_userdata('employee_code', $logged_user_info->employee_code);
//MAPPED EMPLOYEE
$this->db->select('GROUP_CONCAT(prime_employees_id) as logged_repot_to');
$this->db->from('employees');
$this->db->where('reporting_to', $logged_user_info->prime_employees_id);
$emp_mapped_rslt = $this->db->get()->result();
$this->session->set_userdata('logged_repot_to', $emp_mapped_rslt[0]->logged_repot_to);
$this->session->set_userdata('access_data', $this->get_all_access($logged_type,$logged_user_info->prime_employees_id));
}
// GET ALL ACCESS FOR BOTH CUSTOMER AND EMPLOYEE
public function get_all_access($logged_type,$logged_id){
$this->db->select('permission_id,access_add,access_update,access_delete,access_search,access_export,access_import');
$this->db->from('grants');
$this->db->where('prime_employees_id', $logged_id);
$access_rslt = $this->db->get()->result();
$access_info = array();
if($access_rslt){
foreach($access_rslt as $key=>$value){
$permission_id = $value->permission_id;
$access_add = $value->access_add;
$access_update = $value->access_update;
$access_delete = $value->access_delete;
$access_search = $value->access_search;
$access_export = $value->access_export;
$access_import = $value->access_import;
$access_info[$permission_id] = array("access_add"=>$access_add,"access_update"=>$access_update,"access_delete"=>$access_delete,"access_search"=>$access_search,"access_export"=>$access_export,"access_import"=>$access_import);
}
}
return $access_info;
}
// SEND A OTP TO MAIL
public function send_otp_mail($login_email,$user_name,$login_password,$resend){
$login_otp = "";
if($resend === "generate_otp"){
$login_otp = rand(100000, 999999);
}else
if($resend === "resend_otp"){
$login_otp = $_SESSION['login_otp'];
}
if($login_email){
$config_query = 'SELECT smtp_server,sender_name,bcc,port_no,sender_email,mail_username,mail_password FROM cw_mail_configurations WHERE trans_status = 1';
$config_info = $this->db->query("CALL sp_a_run ('SELECT','$config_query')");
$config_result = $config_info->result();
$config_info->next_result();
$smtp_server = $config_result[0]->smtp_server;
$sender_name = $config_result[0]->sender_name;
$port_no = $config_result[0]->port_no;
$sender_email = $config_result[0]->sender_email;
$username = $config_result[0]->mail_username;
$password = $config_result[0]->mail_password;
require('./phpmailer/class.phpmailer.php');
try{
$mail = new PHPMailer();
$mail->SMTPDebug = 1;
$mail->IsSMTP();
$mail->Host = $smtp_server; // Your SMTP PArameter
$mail->Port = $port_no; // Your Outgoing Port
$mail->SMTPAuth = true; // This Must Be True
$mail->Username = $username; // Your Email Address
$mail->Password = $password; // Your Password
$mail->SMTPSecure = 'tls'; // Check Your Server's Connections for TLS or SSL
$mail->From = $sender_email;
$mail->FromName = $sender_name;
$mail->AddAddress($login_email);
$mail->AddAddress("sathish@cafsinfotech.in");
$mail->IsHTML(true);
$mail->Subject = 'Login Cafsindia (OTP)';
$message_body = date("d-M-Y H:i:s").'<p> Your One Time Password(OTP) : <b style = "color:blue;">'.$login_otp.'</b>. For Verify Your Login From </p>
<p>Login Cafsindia</p>';
$mail->Body = $message_body;
$mail = $mail->Send();
if($mail){
$status = 1;
}else{
$status = 0;
}
}catch(phpmailerException $e){
$status = 0;
return array('success'=>false,'message'=>"Mail Not Sent");
}catch(Exception $e){
$status = 0;
return array('success'=>false,'message'=>"Mail Not Sent");
}
if($status){
//TO STORE A SESSION FOR GET A USER NAME AND PASSWORD
$this->session->set_userdata('login_otp', $login_otp);
$this->session->set_userdata('user_name', $user_name);
$this->session->set_userdata('password', $login_password);
$this->session->set_userdata('email', $login_email);
return array('success'=>true,'message'=>"Successfully OTP Send to Your Mail !!");
}else{
return array('success'=>false,'message'=>"Mail Not Sent");
}
}else{
return array('success'=>false,'message'=>"Email ID Not Exist.. Please Contact MIS..!!");
}
}
}
?>