<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model("Login_model");
$this->load->library('session');
}
public function user_register()
{
if ($this->session->userdata('NAME'))
{
$role = $this->session->userdata('ID');
if ($role >=1)
{
$this->load->helper('form');
$this->load->library('form_validation');
$email["email"] = $this->input->post('email');
if ($this->input->post()) {
$this->form_validation->set_rules('user_name', 'Username', 'required|is_unique[app_user.user_name]|min_length[5]|max_length[12]');
$this->form_validation->set_rules('email', 'Email', 'required|is_unique[app_user.email]');
$this->form_validation->set_rules('role', 'User Role', 'required|numeric');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]|md5');
$this->form_validation->set_rules('password_confirm', 'Password Confirmation', 'trim|required|matches[password]|md5');
$this->input->post(0);
if ($this->form_validation->run() === TRUE) {
$this->Login_model->user_create();
echo "User has been saved";
redirect('login/app_user_list');
}
}
$this->load->view('includes/header');
$this->load->view('includes/top_header');
$this->load->view('includes/left_nav');
$this->load->view('login/user_register');
$this->load->view('includes/footer');
$this->load->view('includes/settings');
}
else{
$this->session->set_flashdata('message_name', 'You don\'t have permission to access this page');
redirect('user/index');
}
}
else
{
$this->session->set_flashdata('message_name', 'Your session has been expired. Please Login');
redirect('login/employee_login');
}
}
function app_user_list()
{
//Check user login information
if ($this->session->userdata('NAME'))
{
$role = $this->session->userdata('ROLE');
//Check user rights before redirect admin area
if ($role >= 1)
{
$user_data["user"] = $this->Login_model->user_list();
$this->load->view('includes/header');
$this->load->view('includes/top_header');
$this->load->view('includes/left_nav');
$this->load->view('login/user_list', $user_data);
$this->load->view('includes/footer');
$this->load->view('includes/settings');
}else{
$this->session->set_flashdata('message_name', 'You don\'t have permission to access this page ');
redirect('user/index');
}
} else{
$this->session->set_flashdata('message_name', 'Your session has been expired. Please Login');
redirect('login/employee_login');
}
}
function user_update($id)
{
if ($this->session->userdata('NAME'))
{
$role = $this->session->userdata('ID');
if ($role >=1)
{
$this->load->helper('form');
$this->load->library('form_validation');
if ($this->input->post()) {
$this->form_validation->set_rules('user_name', 'Username', 'required|is_unique[app_user.user_name]|min_length[5]|max_length[12]');
$this->form_validation->set_rules('role', 'User Role', 'required|numeric');
$this->input->post(0);
if ($this->form_validation->run() === TRUE) {
$this->Login_model->user_edit($id);
echo "User has been saved";
redirect('login/app_user_list');
}
}
$data["row"] = $this->Login_model->user_data($id);
if ($data["row"] == null)
{
$this->load->view('includes/header');
$this->load->view('includes/top_header');
$this->load->view('includes/left_nav');
$this->load->view('error_page/404');
$this->load->view('includes/footer');
$this->load->view('includes/settings');
}
$this->load->view('includes/header');
$this->load->view('includes/top_header');
$this->load->view('includes/left_nav');
$this->load->view('login/user_edit', $data);
$this->load->view('includes/footer');
$this->load->view('includes/settings');
}
else{
$this->session->set_flashdata('message_name', 'You din\'t have permission to access this page');
redirect('user/index');
}
}
else
{
$this->session->set_flashdata('message_name', 'Your session has been expired. Please Login');
redirect('login/employee_login');
}
}
public function employee_login()
{
$this->load->view('login/login');
}
public function authenticate() {
$ok = $this->Login_model->auth();
if ($ok) {
$role = $this->session->userdata('ROLE');
if ($role==0){
redirect("user/index");
}
elseif ($role>=1){
redirect('dashboard/index');
}
} else {
$this->session->set_flashdata('message_name', 'Your Email or Password Incorrect');
redirect('login/employee_login');
}
//var_dump($ok); die();
}
public function logout() {
$this->Login_model->login_desable();
$this->session->set_flashdata('message_name', 'You are successfully logout..');
redirect('login/employee_login');
}
function reset_password()
{
$this->load->helper('form');
$this->load->library('form_validation');
if ($this->input->post()) {
$this->form_validation->set_rules('email','Current Email Address','trim|required|valid_email[app_user.email]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]|md5');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
if ($this->form_validation->run() === TRUE) {
$this->Login_model->password_reset();
redirect('login/employee_login');
}
}
$this->session->set_flashdata('message_name', 'Your password has been reset');
$this->load->view('login/reset_password');
}
}