account activation almost ready
[living-lab-site.git] / application / models / users_model.php
1 <?php
2
3 /**
4  * Class Users_model models user information from DB
5  * 
6  * @category    Model
7  * @author              calinburloiu
8  *
9  */
10 class Users_model extends CI_Model {
11         public $db = NULL;
12
13         public function __construct()
14         {
15                 parent::__construct();
16
17                 if ($this->db === NULL)
18                 {
19                         $this->load->library('singleton_db');
20                         $this->db = $this->singleton_db->connect();
21                 }
22         }
23
24         /**
25          * Check authentication credentials. $username can be username or e-mail.
26          * 
27          * @param string $username
28          * @param string $password
29          * @return mixed can return FALSE if authentication failed, a DB row as an
30          * associative array if authentication was succesful or an associative
31          * array with LDAP user information if authentication with LDAP was
32          * successful but the user logged in for the first time and it does not
33          * have an entry in `users` table yet. The key 'auth_src' distinguishes
34          * which associative array was returned:
35          * <ul>
36          *   <li>'internal' or 'ldap': a DB row</li>
37          *   <li>'ldap_first_time': LDAP user information</li>
38          * </ul>
39          */
40         public function login($username, $password)
41         {
42                 $this->load->helper('email');
43                 
44                 // User logs with e-mail address.
45                 if (! valid_email($username))
46                         $cond_user = "username = '$username'";
47                 else
48                         $cond_user = "email = '$username'";
49                 
50                 $enc_password = sha1($password);
51                 
52                 // TODO select only required fields.
53                 $query = $this->db->query("SELECT u.*, a.activation_code
54                         FROM `users` u LEFT JOIN `users_unactivated` a ON (u.id = a.user_id)
55                         WHERE $cond_user
56                                 AND (auth_src = 'ldap' OR password = '$enc_password')");
57                 
58                 // It is possible that the user has a LDAP account but he's
59                 // authenticating here for the first time so it does not have an entry
60                 // in `users` table.
61                 if ($query->num_rows() === 0)
62                 {
63                         $ldap_userdata = $this->ldap_login($username, $password);
64                         if ($ldap_userdata === FALSE)
65                                 return FALSE;
66                         $userdata = $this->convert_ldap_userdata($ldap_userdata);
67                         $this->register($userdata);
68                         
69                         $user = $this->login($username, $password);
70                         $user['import'] = TRUE;
71                         return $user;
72                 }
73                 
74                 $user = $query->row_array();
75                 
76                 // Authenticate with LDAP.
77                 if ($user['auth_src'] == 'ldap'
78                                 && ! $this->ldap_login($username, $password))
79                         return FALSE; 
80                 
81                 // Update last login time.
82                 $this->db->query("UPDATE `users`
83                         SET last_login = UTC_TIMESTAMP()
84                         WHERE username = '$username'");
85                 
86                 // If we are here internal authentication has successful.
87                 return $user;
88         }
89         
90         /**
91          * Converts an array returned by LDAP login to an array which contains
92          * user data ready to be used in `users` DB.
93          * 
94          * @param array $ldap_userdata
95          * @return array
96          */
97         public function convert_ldap_userdata($ldap_userdata)
98         {
99                 $userdata['username'] = $ldap_userdata['uid'][0];
100                 $userdata['email'] = $ldap_userdata['mail'][0];
101                 $userdata['first_name'] = $ldap_userdata['givenname'][0];
102                 $userdata['last_name'] = $ldap_userdata['sn'][0];
103                 
104                 $userdata['auth_src'] = 'ldap';
105                 
106                 return $userdata;
107         }
108         
109         /**
110         * Login with LDAP.
111         *
112         * @param string $username
113         * @param string $password
114         * @return boolean
115         * @author  Alex Herișanu, Răzvan Deaconescu, Călin-Andrei Burloiu
116         */
117         public function ldap_login($username, $password)
118         {
119                 $this->config->load('ldap');
120                 
121                 // First connection: binding.
122                 // TODO exception
123                 $ds = ldap_connect($this->config->item('ldap_server')) or die("Can't connect to ldap server.\n");
124                 if (!@ldap_bind($ds, $this->config->item('ldap_bind_user'),
125                         $this->config->item('ldap_bind_password'))) 
126                 {
127                         ldap_close($ds);
128                         die("Can't connect to ".$this->config->item('ldap_server')."\n");
129                         return FALSE;
130                 }
131                 $sr = ldap_search($ds, "dc=cs,dc=curs,dc=pub,dc=ro", "(uid=" . $username . ")");
132                 if (ldap_count_entries($ds, $sr) > 1)
133                 die("Multiple entries with the same uid in LDAP database??");
134                 if (ldap_count_entries($ds, $sr) < 1) {
135                         ldap_close($ds);
136                         return FALSE;
137                 }
138                 
139                 $info = ldap_get_entries($ds, $sr);
140                 $dn = $info[0]["dn"];
141                 ldap_close($ds);
142                 
143                 // Second connection: connect with user's credentials.
144                 $ds = ldap_connect($this->config->item('ldap_server')) or die("Can't connect to ldap server\n");
145                 if (!@ldap_bind($ds, $dn, $password) or $password == '') {
146                         ldap_close($ds);
147                         return FALSE;
148                 }
149                 
150                 // Verifify if DN belongs to the requested OU.
151                 $info[0]['ou_ok'] = $this->ldap_dn_belongs_ou( $dn, $this->config->item('ldap_req_ou') );
152                 
153                 // Set authentication source.
154                 $info[0]['auth_src'] = 'ldap_first_time';
155                 
156                 return $info[0];
157         }
158         
159         /**
160         * Verify if a user belongs to a group.
161         * 
162         * @param string $dn = "ou=Student,ou=People..."
163         * @param array $ou = array ("Student", etc
164         * @return TRUE or FALSE
165         * @author  Răzvan Herișanu, Răzvan Deaconescu, Călin-Andrei Burloiu
166         */
167         public function ldap_dn_belongs_ou($dn, $ou)
168         {
169                 if (!is_array($ou))
170                         $ou = array ($ou);
171                 
172                 $founded = FALSE;
173                 $words = explode(',', $dn);
174                 foreach ($words as $c) {
175                         $parts = explode("=", $c);
176                         $key = $parts[0];
177                         $value = $parts[1];
178                 
179                         if (strtolower($key) == "ou" && in_array($value, $ou) )
180                                 $founded = TRUE;
181                 }
182                 
183                 return $founded;
184         }
185         
186         /**
187          * Adds a new user to DB.
188          * Do not add join_date and last_login column, they will be automatically
189          * added.
190          * 
191          * @param array $data   corresponds to DB columns
192          */
193         public function register($data)
194         {
195                 $this->load->helper('array');
196                 
197                 // TODO verify mandatory data existance
198                 
199                 // Process data.
200                 if (isset($data['password']))
201                         $data['password'] = sha1($data['password']);
202                 // TODO picture data: save, convert, make it thumbnail
203                 
204                 $cols = '';
205                 $vals = '';
206                 foreach ($data as $col=> $val)
207                 {
208                         $cols .= "$col, ";
209                         if (is_int($val))
210                                 $vals .= "$val, ";
211                         else
212                                 $vals .= "'$val', ";
213                 }
214                 $cols = substr($cols, 0, -2);
215                 $vals = substr($vals, 0, -2);
216                 
217                 $query = $this->db->query("INSERT INTO `users`
218                         ($cols, registration_date, last_login)
219                         VALUES ($vals, utc_timestamp(), utc_timestamp())");
220                 
221                 if ($query === FALSE)
222                         return FALSE;
223                 
224                 // If the registered with internal authentication it needs to activate
225                 // the account.
226                 $activation_code = Users_model::gen_activation_code();
227                 $user_id = $this->get_user_id($data['username']);
228                 $query = $this->db->query("INSERT INTO `users_unactivated`
229                         (user_id, activation_code)
230                         VALUES ($user_id, '$activation_code')");
231                 
232                 // TODO exception on failure
233                 return $query;
234         }
235         
236         public function get_user_id($username)
237         {
238                 $query = $this->db->query("SELECT id FROM `users`
239                         WHERE username = '$username'");
240                 
241                 if ($query->num_rows() === 0)
242                         return FALSE;
243                 
244                 return $query->row()->id;
245         }
246         
247         // TODO cleanup account activation
248         public function cleanup_account_activation()
249         {
250                 
251         }
252         
253         /**
254          * Activated an account for an user having $user_id with $activation_code.
255          * 
256          * @param int $user_id
257          * @param string $activation_code       hexa 16 characters string
258          * @return returns TRUE if activation was successful and FALSE otherwise
259          */
260         public function activate_account($user_id, $activation_code)
261         {
262                 $query = $this->db->query("SELECT * FROM `users_unactivated`
263                         WHERE user_id = $user_id
264                                 AND activation_code = '$activation_code'");
265                 
266                 if ($query->num_rows() === 0)
267                         return FALSE;
268                 
269                 $this->db->query("DELETE FROM `users_unactivated`
270                         WHERE user_id = $user_id");
271                 
272                 return TRUE;
273         }
274         
275         /**
276          * Returns data from `users` table. If $user is int it is used as an
277          * id, if it is string it is used as an username.
278          * 
279          * @param mixed $user
280          * @param string $table_cols    (optional) string with comma separated
281          * `users` table column names. Use a.activation_code to check user's
282          * account activation_code. If this value is NULL than the account is
283          * active.
284          * @return array        associative array with userdata from DB
285          */
286         public function get_userdata($user, $table_cols = '*')
287         {
288                 if (is_int($user))
289                         $cond = "id = $user";
290                 else
291                         $cond = "username = '$user'";
292                 
293                 $query = $this->db->query("SELECT $table_cols
294                         FROM `users` u LEFT JOIN `users_unactivated` a
295                                 ON (u.id = a.user_id)
296                         WHERE $cond");
297                 
298                 if ($query->num_rows() === 0)
299                         return FALSE;
300                 
301                 return $query->row_array();
302         }
303         
304         /**
305          * Modifies data from `users` table for user with $user_id.
306          * 
307          * @param int $user_id
308          * @param array $data   key-value pairs with columns and new values to be
309          * modified
310          */
311         public function set_userdata($user_id, $data)
312         {
313                 // TODO verify mandatory data existance
314                 
315                 // Process data.
316                 if (isset($data['password']))
317                         $data['password'] = sha1($data['password']);
318                 // TODO picture data: save, convert, make it thumbnail
319                 
320                 $set = '';
321                 foreach ($data as $col => $val)
322                 {
323                         if (is_int($val))
324                                 $set .= "$col = $val, ";
325                         else
326                                 $set .= "$col = '$val', ";
327                 }
328                 $set = substr($set, 0, -2);
329                 
330                 $query_str = "UPDATE `users`
331                         SET $set WHERE id = $user_id";
332                 //echo "<p>$query_str</p>";
333                 $query = $this->db->query($query_str);
334                 
335                 // TODO exception
336                 return $query;
337         }
338         
339         public static function gen_activation_code($str = '')
340         {
341                 $ci =& get_instance();
342                 
343                 $activation_code = substr(
344                         sha1(''. $str. $ci->config->item('encryption_key')
345                                 . mt_rand()),
346                         0,
347                         16);
348                 
349                 return $activation_code;
350         }
351         
352         public static function roles_to_string($roles)
353         {
354                 $ci =& get_instance();
355                 $ci->lang->load('user');
356                 
357                 if ($roles == USER_ROLE_STANDARD)
358                         return $ci->lang->line('user_role_standard');
359                 else
360                 {
361                         $str_roles = '';
362                         
363                         if ($roles & USER_ROLE_ADMIN)
364                                 $str_roles .= $ci->lang->line('user_role_admin') . '; ';
365                 }
366                 
367                 return $str_roles;
368         }
369 }
370
371 /* End of file users_model.php */
372 /* Location: ./application/models/users_model.php */