login works; working at register
[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 * FROM `users` 
54                         WHERE $cond_user
55                                 AND (auth_src = 'ldap' OR password = '$enc_password')");
56                 
57                 // It is possible that the user has a LDAP account but he's
58                 // authenticating here for the first time so it does not have an entry
59                 // in `users` table.
60                 if ($query->num_rows() !== 1)
61                         return $this->ldap_login($username, $password);
62                 
63                 $user = $query->row_array();
64                 
65                 // Authenticate with LDAP.
66                 if ($user['auth_src'] == 'ldap')
67                         return ($this->ldap_login($username, $password) !== FALSE 
68                                 ? $user : FALSE);
69                 
70                 // If we are here internal authentication has successful.
71                 return $user;
72         }
73         
74         /**
75         * Login with LDAP.
76         *
77         * @param string $username
78         * @param string $password
79         * @return boolean
80         * @author  Alex Herișanu, Răzvan Deaconescu, Călin-Andrei Burloiu
81         */
82         public function ldap_login($username, $password)
83         {
84                 $this->config->load('ldap');
85                 
86                 // First connection: binding.
87                 // TODO exception
88                 $ds = ldap_connect($this->config->item('ldap_server')) or die("Can't connect to ldap server.\n");
89                 if (!@ldap_bind($ds, $this->config->item('ldap_bind_user'),
90                         $this->config->item('ldap_bind_password'))) 
91                 {
92                         ldap_close($ds);
93                         die("Can't connect to ".$this->config->item('ldap_server')."\n");
94                         return FALSE;
95                 }
96                 $sr = ldap_search($ds, "dc=cs,dc=curs,dc=pub,dc=ro", "(uid=" . $username . ")");
97                 if (ldap_count_entries($ds, $sr) > 1)
98                 die("Multiple entries with the same uid in LDAP database??");
99                 if (ldap_count_entries($ds, $sr) < 1) {
100                         ldap_close($ds);
101                         return FALSE;
102                 }
103                 
104                 $info = ldap_get_entries($ds, $sr);
105                 $dn = $info[0]["dn"];
106                 ldap_close($ds);
107                 
108                 // Second connection: connect with user's credentials.
109                 $ds = ldap_connect($this->config->item('ldap_server')) or die("Can't connect to ldap server\n");
110                 if (!@ldap_bind($ds, $dn, $password) or $password == '') {
111                         ldap_close($ds);
112                         return FALSE;
113                 }
114                 
115                 // Verifify if DN belongs to the requested OU.
116                 $info[0]['ou_ok'] = $this->ldap_dn_belongs_ou( $dn, $this->config->item('ldap_req_ou') );
117                 
118                 // Set authentication source.
119                 $info[0]['auth_src'] = 'ldap_first_time';
120                 
121                 return $info[0];
122         }
123         
124         /**
125         * Verify if a user belongs to a group.
126         * 
127         * @param string $dn = "ou=Student,ou=People..."
128         * @param array $ou = array ("Student", etc
129         * @return TRUE or FALSE
130         * @author  Răzvan Herișanu, Răzvan Deaconescu, Călin-Andrei Burloiu
131         */
132         public function ldap_dn_belongs_ou($dn, $ou)
133         {
134                 if (!is_array($ou))
135                 $ou = array ($ou);
136                 
137                 $founded = FALSE;
138                 $words = explode(',', $dn);
139                 foreach ($words as $c) {
140                         $parts = explode("=", $c);
141                         $key = $parts[0];
142                         $value = $parts[1];
143                 
144                         if (strtolower($key) == "ou" && in_array($value, $ou) )
145                         $founded = TRUE;
146                 }
147                 return $founded;
148         }
149 }
150
151 /* End of file users_model.php */
152 /* Location: ./application/models/users_model.php */