login works; working at register
[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         private $ldap_user_info = NULL;
15
16         public function __construct()
17         {
18                 parent::__construct();
19
20                 $this->lang->load('user');
21         }
22
23         public function index()
24         {
25         }
26
27         /**
28         * Login a user and then redirect it to the last page which must be encoded
29         * in $redirect.
30         *
31         * @param string $redirect       contains the last page URI segments encoded
32         * with helper url_encode_segments.
33         */
34         public function login($redirect = '')
35         {
36                 $this->load->library('form_validation');
37                 $this->load->model('users_model');
38                         
39                 $this->form_validation->set_error_delimiters('<span class="error">',
40                         '</span>');
41
42                 if ($this->form_validation->run('signin') === FALSE)
43                 {
44                         $params = array(        'title' =>
45                                                                         $this->lang->line('ui_nav_menu_login')
46                                                                                 .' &ndash; '
47                                                                                 . $this->config->item('site_name'),
48                                                                 //'metas' => array('description'=>'')
49                         );
50                         $this->load->library('html_head_params', $params);
51                                 
52                         // **
53                         // ** LOADING VIEWS
54                         // **
55                         $this->load->view('html_begin', $this->html_head_params);
56                         $this->load->view('header', array('selected_menu' => 'login'));
57                                 
58                         $this->load->view('user/login_view', array(
59                                 'redirect'=> $redirect
60                         ));
61                                 
62                         $this->load->view('footer');
63                         $this->load->view('html_end');
64                 }
65                 else
66                 {
67                         if ($this->user_id !== NULL)
68                         {
69                                 $this->session->set_userdata(array(
70                                         'user_id'=> $this->user_id,
71                                         'username'=> $this->username
72                                 ));
73                                 
74                                 // Redirect to last page before login. 
75                                 header('Location: '. site_url(urldecode_segments($redirect)));
76                         }
77                         else
78                         {
79                                 $this->session->set_userdata(array(
80                                         'username'=> $this->username
81                                 ));
82                                 
83                                 // Redirect to register page because an user authenticates here
84                                 // for the first time with LDAP.
85                                 // TODO
86                                 header('Location: '. site_url(urldecode_segments($redirect)));
87                         }
88                 }
89         }
90         
91         /**
92          * Logout user and then redirect it to the last page which must be encoded
93          * in $redirect.
94          * 
95          * @param string $redirect      contains the last page URI segments encoded
96          * with helper url_encode_segments.
97          */
98         public function logout($redirect = '')
99         {
100                 $this->session->unset_userdata('user_id');
101                 $this->session->unset_userdata('username');
102                 
103                 header('Location: '. site_url(urldecode_segments($redirect)));
104         }
105         
106         public function register($redirect = '')
107         {
108                 $this->load->library('form_validation');
109                 $this->load->model('users_model');
110                 $this->load->helper('localization');
111                 $this->load->helper('date');
112                         
113                 $this->form_validation->set_error_delimiters('<span class="error">',
114                                         '</span>');
115                 
116                 if ($this->form_validation->run('register') === FALSE)
117                 {
118                         $params = array('title' =>
119                                                                 $this->lang->line('ui_nav_menu_register')
120                                                                         .' &ndash; '
121                                                                         . $this->config->item('site_name'),
122                                                         //'metas' => array('description'=>'')
123                         );
124                         $this->load->library('html_head_params', $params);
125                 
126                         // **
127                         // ** LOADING VIEWS
128                         // **
129                         $this->load->view('html_begin', $this->html_head_params);
130                         $this->load->view('header', array('selected_menu' => 'register'));
131                 
132                         $this->load->view('user/register_view', array(
133                                 'redirect'=> $redirect
134                         ));
135                 
136                         $this->load->view('footer');
137                         $this->load->view('html_end');
138                 }
139                 else
140                 {
141                         if ($this->user_id !== NULL)
142                         {
143                                 $this->session->set_userdata(array(
144                                                         'user_id'=> $this->user_id,
145                                                         'username'=> $this->username
146                                 ));
147                 
148                                 // Redirect to last page before login.
149                                 header('Location: '. site_url(urldecode_segments($redirect)));
150                         }
151                         else
152                         {
153                                 $this->session->set_userdata(array(
154                                                         'username'=> $this->username
155                                 ));
156                 
157                                 // Redirect to register page because an user authenticates here
158                                 // for the first time with LDAP.
159                                 // TODO
160                                 header('Location: '. site_url(urldecode_segments($redirect)));
161                         }
162                 }
163         }
164         
165         public function _valid_username($username)
166         {
167                 return (preg_match('/^[a-z0-9\._]+$/', $username) == 1);
168         }
169
170         public function _valid_username_or_email($username)
171         {
172                 $this->load->helper('email');
173
174                 if (valid_email($username))
175                         return TRUE;
176                 else
177                         return $this->_valid_username($username);
178         }
179
180         public function _do_login($username, $field_password)
181         {
182                 $password = $this->input->post('password');
183
184                 $this->load->model('users_model');
185                 $user = $this->users_model->login($username, $password);
186
187                 // Authentication failed
188                 if ($user === FALSE)
189                         return FALSE;
190                 
191                 // First authentication of a user with LDAP, i.e. the user does not
192                 // have an user_id in `users` DB table yet.
193                 if ($user['auth_src'] == 'ldap_first_time')
194                 {
195                         $this->ldap_user_info = $user;
196                         $this->username = $user['uid'][0];
197                         $this->email = $user['mail'][0];
198                         return TRUE;
199                 }
200                 
201                 // Authentication when the user has an user_id in the DB.
202                 $this->username = $user['username'];
203                 $this->email = $user['email'];
204                 $this->user_id = $user['id'];
205                 
206                 return TRUE;
207         }
208 }
209
210 /* End of file user.php */
211 /* Location: ./application/controllers/user.php */