@ -36,6 +36,12 @@ class OC_USER_LDAP extends OC_User_Backend {
protected $ldap_tls;
protected $ldap_nocase;
protected $ldap_display_name;
protected $ldap_quota;
protected $ldap_quota_def;
protected $ldap_email;
// will be retrieved from LDAP server
protected $ldap_dc = false;
function __construct() {
$this->ldap_host = OC_Appconfig::getValue('user_ldap', 'ldap_host','');
@ -47,6 +53,9 @@ class OC_USER_LDAP extends OC_User_Backend {
$this->ldap_tls = OC_Appconfig::getValue('user_ldap', 'ldap_tls', 0);
$this->ldap_nocase = OC_Appconfig::getValue('user_ldap', 'ldap_nocase', 0);
$this->ldap_display_name = OC_Appconfig::getValue('user_ldap', 'ldap_display_name', OC_USER_BACKEND_LDAP_DEFAULT_DISPLAY_NAME);
$this->ldap_quota_attr = OC_Appconfig::getValue('user_ldap', 'ldap_quota_attr','');
$this->ldap_quota_def = OC_Appconfig::getValue('user_ldap', 'ldap_quota_def','');
$this->ldap_email_attr = OC_Appconfig::getValue('user_ldap', 'ldap_email_attr','');
if( !empty($this->ldap_host)
& & !empty($this->ldap_port)
@ -66,6 +75,28 @@ class OC_USER_LDAP extends OC_User_Backend {
ldap_unbind($this->ds);
}
private function setQuota( $uid ) {
if( !$this->ldap_dc )
return false;
$quota = $this->ldap_dc[$this->ldap_quota_attr][0];
$quota = $quota != -1 ? $quota : $this->ldap_quota_def;
OC_Preferences::setValue($uid, 'files', 'quota', $quota);
}
private function setEmail( $uid ) {
if( !$this->ldap_dc )
return false;
$email = OC_Preferences::getValue($uid, 'settings', 'email', '');
if ( !empty( $email ) )
return false;
$email = $this->ldap_dc[$this->ldap_email_attr][0];
OC_Preferences::setValue($uid, 'settings', 'email', $email);
}
//Connect to LDAP and store the resource
private function getDs() {
if(!$this->ds) {
$this->ds = ldap_connect( $this->ldap_host, $this->ldap_port );
@ -74,18 +105,19 @@ class OC_USER_LDAP extends OC_User_Backend {
if($this->ldap_tls)
ldap_start_tls($this->ds);
}
//TODO: Not necessary to perform a bind each time, is it?
// login
if(!empty($this->ldap_dn)) {
$ldap_login = @ldap_bind( $this->ds, $this->ldap_dn, $this->ldap_password );
if(!$ldap_login)
if(!$ldap_login) {
return false;
}
}
return $this->ds;
}
private function getDn ( $uid ) {
private function getDc ( $uid ) {
if(!$this->configured)
return false;
@ -99,31 +131,43 @@ class OC_USER_LDAP extends OC_User_Backend {
$sr = ldap_search( $this->getDs(), $this->ldap_base, $filter );
$entries = ldap_get_entries( $this->getDs(), $sr );
if( $entries['count'] == 0 )
if( $entries['count'] == 0 ) {
return false;
}
$this->ldap_dc = $entries[0];
return $entries[0]['dn'];
return $this->ldap_dc ;
}
public function checkPassword( $uid, $password ) {
if(!$this->configured){
return false;
}
$dn = $this->getDn ( $uid );
if( !$dn )
$dc = $this->getDc ( $uid );
if( !$dc )
return false;
if (!@ldap_bind( $this->getDs(), $dn, $password ))
if (!@ldap_bind( $this->getDs(), $dc['d n'] , $password )) {
return false;
}
if(!empty($this->ldap_quota) & & !empty($this->ldap_quota_def)) {
$this->setQuota($uid);
}
if(!empty($this->ldap_email_attr)) {
$this->setEmail($uid);
}
if($this->ldap_nocase) {
$filter = str_replace('%uid', $uid, $this->ldap_filter);
$sr = ldap_search( $this->getDs(), $this->ldap_base, $filter );
$entries = ldap_get_entries( $this->getDs(), $sr );
if( $entries['count'] == 1 ) {
foreach($entries as $row) {
$ldap_display_name = strtolower($this->ldap_display_name);
if(isset($row[$ldap_display_name])) {
$ldap_display_name = strtolower($this->ldap_display_name);
if(isset($row[$ldap_display_name])) {
return $row[$ldap_display_name][0];
}
}
@ -131,12 +175,12 @@ class OC_USER_LDAP extends OC_User_Backend {
else {
return $uid;
}
}
else {
return $uid;
}
}
public function userExists( $uid ) {
@ -146,17 +190,17 @@ class OC_USER_LDAP extends OC_User_Backend {
$dn = $this->getDn($uid);
return !empty($dn);
}
public function getUsers()
{
if(!$this->configured)
return false;
// connect to server
$ds = $this->getDs();
if( !$ds )
return false;
// get users
$filter = 'objectClass=person';
$sr = ldap_search( $this->getDs(), $this->ldap_base, $filter );
@ -169,7 +213,7 @@ class OC_USER_LDAP extends OC_User_Backend {
// TODO ldap_get_entries() seems to lower all keys => needs review
$ldap_display_name = strtolower($this->ldap_display_name);
if(isset($row[$ldap_display_name])) {
$users[] = $row[$ldap_display_name][0];
$users[] = $row[$ldap_display_name][0];
}
}
// TODO language specific sorting of user names