working at user login
[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          */
30         public function login($username, $password)
31         {
32                 $this->load->helper('email');
33                 
34                 // User logs with e-mail address.
35                 if (! valid_email($username))
36                         $cond_user = "username = '$username'";
37                 else
38                         $cond_user = "email = '$username'";
39                 
40                 $enc_password = sha1($password);
41                 
42                 // TODO select only required fields.
43                 $query = $this->db->query("SELECT * FROM `users` 
44                         WHERE $cond_user AND password = '$enc_password'");
45                 
46                 if ($query->num_rows() !== 1)
47                         return FALSE;
48
49                 return $query->row_array();
50         }
51 }
52
53 /* End of file users_model.php */
54 /* Location: ./application/models/users_model.php */