visa_app/application/controllers/Login_Controller.php
venbatechnologies@gmail.com 769edb667b profile
2018-08-09 10:27:59 +05:30

299 lines
9.9 KiB
PHP
Executable File

<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Class : Login_Controller
* Login_Controller class to control to authenticate user credentials and starts user's session.
* @author : Suren
* @version : 1.1
* @since : 30 July 2018
*/
class Login_Controller extends CI_Controller
{
/**
* This is default constructor of the class
*/
public function __construct()
{
parent::__construct();
$this->load->model('Login_Model');
}
/**
* Index Page for this controller.
*/
public function index()
{
$this->isLoggedIn();
}
/**
* This function used to check the user is logged in or not
*/
function isLoggedIn()
{
$isLoggedIn = $this->session->userdata('isLoggedIn');
if(!isset($isLoggedIn) || $isLoggedIn != TRUE)
{
$this->load->view('login');
}
else
{
redirect('/Dashboard');
}
}
/**
* This function used to logged in user
*/
public function loginMe()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|max_length[128]|trim');
$this->form_validation->set_rules('password', 'Password', 'required|max_length[32]');
if($this->form_validation->run() == FALSE)
{
$this->index();
}
else
{
$email = $this->security->xss_clean($this->input->post('email'));
$password = $this->input->post('password');
$result = $this->Login_Model->loginMe($email, $password);
if(!empty($result))
{
$lastLogin = $this->Login_Model->lastLoginInfo($result->userId);
$sessionArray = array('userId'=>$result->userId,
'role'=>$result->roleId,
'roleText'=>$result->role,
'name'=>$result->name,
'lastLogin'=> $lastLogin->createdDtm,
'isLoggedIn' => TRUE
);
$this->session->set_userdata($sessionArray);
unset($sessionArray['userId'], $sessionArray['isLoggedIn'], $sessionArray['lastLogin']);
$loginInfo = array("userId"=>$result->userId, "sessionData" => json_encode($sessionArray), "machineIp"=>$_SERVER['REMOTE_ADDR'], "userAgent"=>getBrowserAgent(), "agentString"=>$this->agent->agent_string(), "platform"=>$this->agent->platform());
$this->Login_Model->lastLogin($loginInfo);
redirect('/Dashboard');
}
else
{
$this->session->set_flashdata('error', 'Email or password mismatch');
redirect('/Login_Controller');
}
}
}
/**
* This function used to load forgot password view
*/
// public function forgotPassword()
// {
// $isLoggedIn = $this->session->userdata('isLoggedIn');
// if(!isset($isLoggedIn) || $isLoggedIn != TRUE)
// {
// $this->load->view('forgotPassword');
// }
// else
// {
// redirect('/Dashboard');
// }
// }
/**
* This function used to generate reset password request link
*/
function resetPasswordUser()
{
$status = '';
$this->load->library('form_validation');
$this->form_validation->set_rules('login_email','Email','trim|required|valid_email');
if($this->form_validation->run() == FALSE)
{
$this->forgotPassword();
}
else
{
$email = $this->security->xss_clean($this->input->post('login_email'));
if($this->Login_Model->checkEmailExist($email))
{
$encoded_email = urlencode($email);
$this->load->helper('string');
$data['email'] = $email;
$data['activation_id'] = random_string('alnum',15);
$data['createdDtm'] = date('Y-m-d H:i:s');
$data['agent'] = getBrowserAgent();
$data['client_ip'] = $this->input->ip_address();
$save = $this->Login_Model->resetPasswordUser($data);
if($save)
{
$data1['reset_link'] = base_url() . "resetPasswordConfirmUser/" . $data['activation_id'] . "/" . $encoded_email;
$userInfo = $this->Login_Model->getCustomerInfoByEmail($email);
if(!empty($userInfo)){
$data1["name"] = $userInfo[0]->name;
$data1["email"] = $userInfo[0]->email;
$data1["message"] = "Reset Your Password";
}
$sendStatus = resetPasswordEmail($data1);
if($sendStatus){
$status = "send";
setFlashData($status, "Reset password link sent successfully, please check mails.");
} else {
$status = "notsend";
setFlashData($status, "Email has been failed, try again.");
}
}
else
{
$status = 'unable';
setFlashData($status, "It seems an error while sending your details, try again.");
}
}
else
{
$status = 'invalid';
setFlashData($status, "This email is not registered with us.");
}
redirect('/forgotPassword');
}
}
/**
* This function used to reset the password
* @param string $activation_id : This is unique id
* @param string $email : This is user email
*/
function resetPasswordConfirmUser($activation_id, $email)
{
// Get email and activation code from URL values at index 3-4
$email = urldecode($email);
// Check activation id in database
$is_correct = $this->Login_Model->checkActivationDetails($email, $activation_id);
$data['email'] = $email;
$data['activation_code'] = $activation_id;
if ($is_correct == 1)
{
$this->load->view('newPassword', $data);
}
else
{
redirect('/Login_Controller');
}
}
/**
* This function used to create new password for user
*/
function createPasswordUser()
{
$status = '';
$message = '';
$email = $this->input->post("email");
$activation_id = $this->input->post("activation_code");
$this->load->library('form_validation');
$this->form_validation->set_rules('password','Password','required|max_length[20]');
$this->form_validation->set_rules('cpassword','Confirm Password','trim|required|matches[password]|max_length[20]');
if($this->form_validation->run() == FALSE)
{
$this->resetPasswordConfirmUser($activation_id, urlencode($email));
}
else
{
$password = $this->input->post('password');
$cpassword = $this->input->post('cpassword');
// Check activation id in database
$is_correct = $this->Login_Model->checkActivationDetails($email, $activation_id);
if($is_correct == 1)
{
$this->Login_Model->createPasswordUser($email, $password);
$status = 'success';
$message = 'Password changed successfully';
}
else
{
$status = 'error';
$message = 'Password changed failed';
}
setFlashData($status, $message);
redirect("/Login_Controller");
}
}
/**
* This function used to load the Register page
*/
function Register()
{
$this->global['pageTitle'] = 'Visa : Register';
$this->load->View('register', $this->global,NULL , NULL);
}
/**
* To Store Register Details
*/
function Add_Register(){
$Registerdata =
array( 'Name'=>$this->input->post('Name'),
'PhoneNo'=>$this->input->post('PhoneNo'),
'Email'=>$this->input->post('Email'),
'AlternateEmail'=>$this->input->post('AlternateEmail'),
'Designation'=>$this->input->post('Designation'),
'CompanyName'=>$this->input->post('CompanyName'),
'CityName'=>$this->input->post('CityName'),
'CountryName'=>$this->input->post('CountryName'),
'Address'=>$this->input->post('Address'));
$Userdata =
array( 'email'=>$this->input->post('Email'),
'password'=>getHashedPassword($this->input->post('Password')),
'mobile'=>$this->input->post('PhoneNo'),
'roleId'=>REGISTER_USERS,
'name'=>$this->input->post('Name'),
'createdDtm'=>date('Y-m-d H:i:s'));
$register= $this->Login_Model->Add_Register($Registerdata);
$user= $this->Login_Model->Add_User($Userdata);
if($register != ''&& $user !='')
{
echo"<script>alert('Created Successfully');window.location.href='Login_Controller';</script>";
}
else{
echo"<script>alert('Not Saved Try Again');window.location.href='register';</script>";
}
}
}
?>