1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 25/12/2015
* Time: 2:22 CH
*/
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Account extends CI_Controller
{
public function __construct()
{
parent::__construct();
date_default_timezone_set("Asia/Ho_Chi_Minh");
//
$this->load->model('admin_model');
$this->load->model('userlog_model');
}
public function index()
{
// Thiết lập validate
$config = array(
array(
'field' => 'txtUsername',
'label' => 'Username',
'rules' => 'trim|required|xss_clean'
),
array(
'field' => 'txtPassword',
'label' => 'Password',
'rules' => 'trim|required|xss_clean'
)
);
$this->form_validation->set_message('required', '<li>Bạn chưa nhập %s!</li>');
$this->form_validation->set_message('matches', '<li>Bạn nhập sai %s!</li>');
$this->form_validation->set_rules($config);
// Xử lý form đăng nhập
if ($this->form_validation->run($this) == FALSE) {
$this->load->view('backend/login_view');
} else {
$username = trim($this->input->post('txtUsername'));
$password = MyHelper::genKeyCode(trim($this->input->post('txtPassword')));
$checkUserAdmin = $this->admin_model->getUserAdminLogin($username, $password);
//MyHelper::echoPreDie($checkUserAdmin);
if ($checkUserAdmin) {
// Thiết lập session
$sessionData = array(
'id' => $checkUserAdmin[0]['id'],
'group_id' => $checkUserAdmin[0]['group_id'],
'fullname' => $checkUserAdmin[0]['fullname'],
'username' => $checkUserAdmin[0]['username'],
'email' => $checkUserAdmin[0]['email'],
'is_active' => $checkUserAdmin[0]['is_active'],
'is_super' => $checkUserAdmin[0]['is_super'],
);
// Ghi log
$user_log_id = $this->userlog_model->add('LOGIN/LOGOUT', $this->input->post('txtUsername'), 'SUCCESS');
$sessionData['user_log_id'] = $user_log_id;
$sessionData['time_login'] = date('Y-m-d H:i:s');
$this->session->set_userdata($sessionData);
// Điều hướng đến trang chủ admin
redirect(base_url('backend/home/index'));
} else {
// Ghi log
$this->userlog_model->add('LOGIN', $this->input->post('txtUsername'), 'FAILED');
// Chuyển trang
$this->session->set_flashdata('loginErrorMsg', 'Đăng nhập không thành công!');
redirect(base_url('backend/account/index'));
}
}
}
public function home()
{
//$this->output->cache(1*24*60);
//
$this->_data['functionName'] = 'Trang chủ CMS';
$this->_data['action'] = 'home';
$this->_data['titlePage'] = 'Trang chủ CMS';
$this->_data['loadPage'] = 'home_view';
$this->load->view($this->_data['path'], $this->_data);
}
public function logout()
{
// Xóa session
if ($this->session->userdata('username')) {
// Ghi log
$this->userlog_model->update($this->session->userdata('user_log_id'), $this->session->userdata('time_login'));
// Xóa session
$this->session->sess_destroy();
}
// Điều hướng về trang đăng nhập
redirect(base_url('backend/account/index'));
}
/**
* Action xử lý lỗi quyền truy cập
* Hiển thị trang thông báo lỗi không có quyền truy cập
*/
public function accessDeny()
{
//$this->output->cache(1*24*60);
//
$this->_data['action'] = 'Không có quyền';
$this->_data['titlePage'] = 'Không có quyền';
$this->_data['loadPage'] = 'access_deny_view';
$this->load->view($this->_data['path'], $this->_data);
}
public function page404()
{
//$this->output->cache(1*24*60);
//
$this->_data['action'] = '404';
$this->_data['titlePage'] = '404';
$this->_data['loadPage'] = 'page404_view';
$this->load->view($this->_data['path'], $this->_data);
}
}