4 * Class Users_model models user information from DB
10 class Users_model extends CI_Model {
13 public function __construct()
15 parent::__construct();
17 if ($this->db === NULL)
19 $this->load->library('singleton_db');
20 $this->db = $this->singleton_db->connect();
25 * Check authentication credentials. $username can be username or e-mail.
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:
36 * <li>'internal' or 'ldap': a DB row</li>
37 * <li>'ldap_first_time': LDAP user information</li>
40 public function login($username, $password)
42 $this->load->helper('email');
44 // User logs with e-mail address.
45 if (! valid_email($username))
46 $cond_user = "username = '$username'";
48 $cond_user = "email = '$username'";
50 $enc_password = sha1($password);
52 // TODO select only required fields.
53 $query = $this->db->query("SELECT * FROM `users`
55 AND (auth_src = 'ldap' OR password = '$enc_password')");
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
60 if ($query->num_rows() !== 1)
62 $ldap_userdata = $this->ldap_login($username, $password);
63 $userdata = $this->convert_ldap_userdata($ldap_userdata);
64 $this->register($userdata);
66 $user = $this->login($username, $password);
67 $user['import'] = TRUE;
70 /* foreach ($ldap_userdata as $k => $v)
78 $user = $query->row_array();
80 // Authenticate with LDAP.
81 if ($user['auth_src'] == 'ldap')
82 return ($this->ldap_login($username, $password) !== FALSE
85 // If we are here internal authentication has successful.
90 * Converts an array returned by LDAP login to an array which contains
91 * user data ready to be used in `users` DB.
93 * @param array $ldap_userdata
96 public function convert_ldap_userdata($ldap_userdata)
98 $userdata['username'] = $ldap_userdata['uid'][0];
99 $userdata['email'] = $ldap_userdata['mail'][0];
100 $userdata['first_name'] = $ldap_userdata['givenname'][0];
101 $userdata['last_name'] = $ldap_userdata['sn'][0];
103 $userdata['auth_src'] = 'ldap';
111 * @param string $username
112 * @param string $password
114 * @author Alex Herișanu, Răzvan Deaconescu, Călin-Andrei Burloiu
116 public function ldap_login($username, $password)
118 $this->config->load('ldap');
120 // First connection: binding.
122 $ds = ldap_connect($this->config->item('ldap_server')) or die("Can't connect to ldap server.\n");
123 if (!@ldap_bind($ds, $this->config->item('ldap_bind_user'),
124 $this->config->item('ldap_bind_password')))
127 die("Can't connect to ".$this->config->item('ldap_server')."\n");
130 $sr = ldap_search($ds, "dc=cs,dc=curs,dc=pub,dc=ro", "(uid=" . $username . ")");
131 if (ldap_count_entries($ds, $sr) > 1)
132 die("Multiple entries with the same uid in LDAP database??");
133 if (ldap_count_entries($ds, $sr) < 1) {
138 $info = ldap_get_entries($ds, $sr);
139 $dn = $info[0]["dn"];
142 // Second connection: connect with user's credentials.
143 $ds = ldap_connect($this->config->item('ldap_server')) or die("Can't connect to ldap server\n");
144 if (!@ldap_bind($ds, $dn, $password) or $password == '') {
149 // Verifify if DN belongs to the requested OU.
150 $info[0]['ou_ok'] = $this->ldap_dn_belongs_ou( $dn, $this->config->item('ldap_req_ou') );
152 // Set authentication source.
153 $info[0]['auth_src'] = 'ldap_first_time';
159 * Verify if a user belongs to a group.
161 * @param string $dn = "ou=Student,ou=People..."
162 * @param array $ou = array ("Student", etc
163 * @return TRUE or FALSE
164 * @author Răzvan Herișanu, Răzvan Deaconescu, Călin-Andrei Burloiu
166 public function ldap_dn_belongs_ou($dn, $ou)
172 $words = explode(',', $dn);
173 foreach ($words as $c) {
174 $parts = explode("=", $c);
178 if (strtolower($key) == "ou" && in_array($value, $ou) )
186 * Adds a new user to DB.
188 * @param array $data corresponds to DB columns
190 public function register($data)
192 $this->load->helper('array');
194 // TODO verify mandatory data existance
197 if (isset($data['password']))
198 $data['password'] = sha1($data['password']);
199 // TODO picture data: save, convert, make it thumbnail
203 foreach ($data as $col=> $val)
211 $cols = substr($cols, 0, -2);
212 $vals = substr($vals, 0, -2);
214 $query = $this->db->query("INSERT INTO `users`
218 // TODO exception on failure
223 * Returns data from `users` table for user with $user_id.
225 * @param int $user_id
227 public function get_userdata($user_id)
229 $query = $this->db->query("SELECT * from `users`
230 WHERE id = $user_id");
232 if ($query->num_rows() === 0)
235 return $query->row_array();
239 * Modifies data from `users` table for user with $user_id.
241 * @param int $user_id
242 * @param array $data key-value pairs with columns and new values to be
245 public function set_userdata($user_id, $data)
247 // TODO verify mandatory data existance
250 if (isset($data['password']))
251 $data['password'] = sha1($data['password']);
252 // TODO picture data: save, convert, make it thumbnail
255 foreach ($data as $col => $val)
258 $set .= "$col = $val, ";
260 $set .= "$col = '$val', ";
262 $set = substr($set, 0, -2);
264 $query_str = "UPDATE `users`
265 SET $set WHERE id = $user_id";
266 //echo "<p>$query_str</p>";
267 $query = $this->db->query($query_str);
274 /* End of file users_model.php */
275 /* Location: ./application/models/users_model.php */