1: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2:
3: class User extends CI_Controller {
4: public function __construct() {
5: parent::__construct();
6: $this->load->library('session');
7: }
8:
9: public function index() {
10: header('Location: ' . $this->config->item('base_url') . 'user/login');
11: }
12:
13: public function login() {
14: $view['base_url'] = $this->config->item('base_url');
15: $view['dest_url'] = $this->session->userdata('dest_url');
16:
17: if (!$view['dest_url']) {
18: $view['dest_url'] = $view['base_url'] . 'backend';
19: }
20:
21: $this->load->view('user/login', $view);
22: }
23:
24: public function logout() {
25: $this->session->unset_userdata('user_id');
26: $this->session->unset_userdata('user_email');
27: $this->session->unset_userdata('role_slug');
28: $this->session->unset_userdata('username');
29: $this->session->unset_userdata('dest_url');
30:
31: $view['base_url'] = $this->config->item('base_url');
32: $this->load->view('user/logout', $view);
33: }
34:
35: public function forgot_password() {
36: $view['base_url'] = $this->config->item('base_url');
37: $this->load->view('user/forgot_password', $view);
38: }
39:
40: public function no_privileges() {
41: $view['base_url'] = $this->config->item('base_url');
42: $this->load->view('user/no_privileges', $view);
43: }
44:
45: 46: 47: 48: 49: 50: 51: 52: 53:
54: public function ajax_check_login() {
55: try {
56: if (!isset($_POST['username']) || !isset($_POST['password'])) {
57: throw new Exception('Invalid credentials given!');
58: }
59:
60: $this->load->model('user_model');
61: $user_data = $this->user_model->check_login($_POST['username'], $_POST['password']);
62:
63: if ($user_data) {
64: $this->session->set_userdata($user_data);
65: echo json_encode(AJAX_SUCCESS);
66: } else {
67: echo json_encode(AJAX_FAILURE);
68: }
69:
70: } catch(Exception $exc) {
71: echo json_encode(array(
72: 'exceptions' => array(exceptionToJavaScript($exc))
73: ));
74: }
75: }
76:
77: 78: 79: 80: 81: 82: 83:
84: public function ajax_forgot_password() {
85: try {
86: if (!isset($_POST['username']) || !isset($_POST['email'])) {
87: throw new Exception('You must enter a valid username and email address in '
88: . 'order to get a new password!');
89: }
90:
91: $this->load->model('user_model');
92: $this->load->model('settings_model');
93:
94: $new_password = $this->user_model->regenerate_password($_POST['username'], $_POST['email']);
95:
96: if ($new_password != FALSE) {
97: $this->load->library('notifications');
98: $company_settings = array(
99: 'company_name' => $this->settings_model->get_setting('company_name'),
100: 'company_link' => $this->settings_model->get_setting('company_link'),
101: 'company_email' => $this->settings_model->get_setting('company_email')
102: );
103: $this->notifications->send_password($new_password, $_POST['email'], $company_settings);
104: }
105:
106: echo ($new_password != FALSE) ? json_encode(AJAX_SUCCESS) : json_encode(AJAX_FAILURE);
107: } catch(Exception $exc) {
108: echo json_encode(array(
109: 'exceptions' => array(exceptionToJavaScript($exc))
110: ));
111: }
112: }
113: }
114:
115:
116: