working at user login
[living-lab-site.git] / application / controllers / user.php
1 <?php
2
3 /**
4  * Class User controls video hierarchy and searching
5  *
6  * @category    Controller
7  * @author              Călin-Andrei Burloiu
8  */
9 class User extends CI_Controller {
10
11         private $username = NULL;
12         private $email = NULL;
13         private $user_id = NULL;
14
15         public function __construct()
16         {
17                 parent::__construct();
18
19                 $this->lang->load('user');
20         }
21
22         public function index()
23         {
24         }
25
26         public function login()
27         {
28                 $this->load->library('form_validation');
29                 $this->load->model('users_model');
30
31                 $username = $this->input->post('username');
32                 $password = $this->input->post('password');
33                         
34                 $form_validation_config = array(
35                 array(
36                                 'field'=>'username',
37                                 'label'=>'lang:user_username_or_email',
38                                 'rules'=>'trim|required|min_length[5]|max_length[32]'
39                 . '|strtolower|callback__valid_username'
40                 . '|callback__do_login[password]'
41                 ),
42                 array(
43                                 'field'=>'password',
44                                 'label'=>'lang:user_password',
45                                 'rules'=>'required|min_length[5]|max_length[32]'
46                 )
47                 );
48                 $this->form_validation->set_rules($form_validation_config);
49                 $this->form_validation->set_error_delimiters('<span class="error">',
50                         '</span>');
51
52                 if ($this->form_validation->run() === FALSE)
53                 {
54                         $params = array(        'title' => $this->config->item('site_name'),
55                                                                                 'css' => array(
56                                                                                         'catalog.css'
57                         ),
58                         //'js' => array(),
59                         //'metas' => array('description'=>'')
60                         );
61                         $this->load->library('html_head_params', $params);
62                                 
63                         // **
64                         // ** LOADING VIEWS
65                         // **
66                         $this->load->view('html_begin', $this->html_head_params);
67                         $this->load->view('header', array('selected_menu' => 'login'));
68                                 
69                         $this->load->view('user/login_view', array());
70                                 
71                         $this->load->view('footer');
72                         $this->load->view('html_end');
73                 }
74                 else
75                 {
76                         if ($this->user_id !== NULL)
77                         {
78                                 $this->session->set_userdata(array(
79                                         'user_id'=> $this->user_id,
80                                         'username'=> $this->username
81                                 ));
82                         }
83                         
84                         header('Location: '. site_url());
85                         return;
86                 }
87         }
88
89         public function _valid_username($username)
90         {
91                 $this->load->helper('email');
92
93                 if (valid_email($username))
94                 return TRUE;
95                 else
96                 return (preg_match('/^[a-z0-9\._]+$/', $username) == 1);
97         }
98
99         public function _do_login($username, $field_password)
100         {
101                 $password = $this->input->post('password');
102
103                 $this->load->model('users_model');
104                 $res_login = $this->users_model->login($username, $password);
105
106                 // First authentication of a user with LDAP, i.e. the user does not
107                 // have an user_id in `users` DB table yet.
108                 if ($res_login === TRUE)
109                         return TRUE;
110                 // Authentication failed
111                 else if ($res_login === FALSE)
112                         return FALSE;
113                 
114                 // Authentication when the user has an user_id in the DB.
115                 $this->username = $res_login['username'];
116                 $this->email = $res_login['email'];
117                 $this->user_id = $res_login['id'];
118
119                 return TRUE;
120         }
121 }
122
123 /* End of file user.php */
124 /* Location: ./application/controllers/user.php */