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)
61 return $this->ldap_login($username, $password);
63 $user = $query->row_array();
65 // Authenticate with LDAP.
66 if ($user['auth_src'] == 'ldap')
67 return ($this->ldap_login($username, $password) !== FALSE
70 // If we are here internal authentication has successful.
77 * @param string $username
78 * @param string $password
80 * @author Alex Herișanu, Răzvan Deaconescu, Călin-Andrei Burloiu
82 public function ldap_login($username, $password)
84 $this->config->load('ldap');
86 // First connection: binding.
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')))
93 die("Can't connect to ".$this->config->item('ldap_server')."\n");
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) {
104 $info = ldap_get_entries($ds, $sr);
105 $dn = $info[0]["dn"];
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 == '') {
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') );
118 // Set authentication source.
119 $info[0]['auth_src'] = 'ldap_first_time';
125 * Verify if a user belongs to a group.
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
132 public function ldap_dn_belongs_ou($dn, $ou)
138 $words = explode(',', $dn);
139 foreach ($words as $c) {
140 $parts = explode("=", $c);
144 if (strtolower($key) == "ou" && in_array($value, $ou) )
151 /* End of file users_model.php */
152 /* Location: ./application/models/users_model.php */