Merge pull request #245 from aymhce/patch-1

Clean code and fix potential issues
This commit is contained in:
fabrixxm 2015-03-09 12:08:53 +01:00
commit 17b963a701

View file

@ -66,11 +66,14 @@ function ldapauth_uninstall() {
function ldapauth_hook_authenticate($a,&$b) { function ldapauth_hook_authenticate($a,&$b) {
if(ldapauth_authenticate($b['username'],$b['password']) && is_existing_account($b['username'])) { if(ldapauth_authenticate($b['username'],$b['password'])) {
$b['user_record'] = $results[0]; $results = get_existing_account($b['username']);
$b['authenticated'] = 1; if(! empty($results)){
} $b['user_record'] = $results[0];
return; $b['authenticated'] = 1;
}
}
return;
} }
function ldapauth_authenticate($username,$password) { function ldapauth_authenticate($username,$password) {
@ -81,14 +84,9 @@ function ldapauth_authenticate($username,$password) {
$ldap_searchdn = get_config('ldapauth','ldap_searchdn'); $ldap_searchdn = get_config('ldapauth','ldap_searchdn');
$ldap_userattr = get_config('ldapauth','ldap_userattr'); $ldap_userattr = get_config('ldapauth','ldap_userattr');
$ldap_group = get_config('ldapauth','ldap_group'); $ldap_group = get_config('ldapauth','ldap_group');
$ldap_autocreateaccount = get_config('ldapauth','ldap_autocreateaccount'); $ldap_autocreateaccount = get_config('ldapauth','ldap_autocreateaccount');
$ldap_autocreateaccount_emailattribute = get_config('ldapauth','ldap_autocreateaccount_emailattribute'); $ldap_autocreateaccount_emailattribute = get_config('ldapauth','ldap_autocreateaccount_emailattribute');
$ldap_autocreateaccount_nameattribute = get_config('ldapauth','ldap_autocreateaccount_nameattribute'); $ldap_autocreateaccount_nameattribute = get_config('ldapauth','ldap_autocreateaccount_nameattribute');
if(! strlen($ldap_autocreateaccount_emailattribute))
$ldap_autocreateaccount_emailattribute = "mail";
if(! strlen($ldap_autocreateaccount_nameattribute))
$ldap_autocreateaccount_nameattribute = "givenName";
if(! ((strlen($password)) if(! ((strlen($password))
&& (function_exists('ldap_connect')) && (function_exists('ldap_connect'))
@ -122,14 +120,22 @@ function ldapauth_authenticate($username,$password) {
if(! @ldap_bind($connect,$dn,$password)) if(! @ldap_bind($connect,$dn,$password))
return false; return false;
$emailarray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_emailattribute); $emailarray = [];
$namearray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_nameattribute); $namearray = [];
if($ldap_autocreateaccount == "true"){
if(! strlen($ldap_autocreateaccount_emailattribute))
$ldap_autocreateaccount_emailattribute = "mail";
if(! strlen($ldap_autocreateaccount_nameattribute))
$ldap_autocreateaccount_nameattribute = "givenName";
$emailarray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_emailattribute);
$namearray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_nameattribute);
}
if(! strlen($ldap_group)){ if(! strlen($ldap_group)){
ldap_autocreateaccount($ldap_autocreateaccount,$username,$password,$emailarray[0],$namearray[0]); ldap_autocreateaccount($ldap_autocreateaccount,$username,$password,$emailarray[0],$namearray[0]);
return true; return true;
} }
$r = @ldap_compare($connect,$ldap_group,'member',$dn); $r = @ldap_compare($connect,$ldap_group,'member',$dn);
if ($r === -1) { if ($r === -1) {
@ -155,30 +161,29 @@ function ldapauth_authenticate($username,$password) {
return false; return false;
} }
ldap_autocreateaccount($ldap_autocreateaccount,$username,$password,$emailarray[0],$namearray[0]); ldap_autocreateaccount($ldap_autocreateaccount,$username,$password,$emailarray[0],$namearray[0]);
return true; return true;
} }
function ldap_autocreateaccount($ldap_autocreateaccount,$username,$password,$email,$name) { function ldap_autocreateaccount($ldap_autocreateaccount,$username,$password,$email,$name) {
if($ldap_autocreateaccount == "true" && !is_existing_account($username)){ if($ldap_autocreateaccount == "true"){
if (strlen($email) > 0 && strlen($name) > 0){ $results = get_existing_account($username);
$arr = array('username'=>$name,'nickname'=>$username,'email'=>$email,'password'=>$password,'verified'=>1); if(empty($results)){
$result = create_user($arr); if (strlen($email) > 0 && strlen($name) > 0){
if ($result['success']){ $arr = array('username'=>$name,'nickname'=>$username,'email'=>$email,'password'=>$password,'verified'=>1);
logger("ldapauth: account " . $username . " created"); $result = create_user($arr);
}else{ if ($result['success']){
logger("ldapauth: account " . $username . " was not created ! : " . implode($result)); logger("ldapauth: account " . $username . " created");
} }else{
}else{ logger("ldapauth: account " . $username . " was not created ! : " . implode($result));
logger("ldapauth: unable to create account, no email or nickname found"); }
} }else{
} logger("ldapauth: unable to create account, no email or nickname found");
}
}
}
} }
function is_existing_account($username){ function get_existing_account($username){
$results = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 AND `verified` = 1 LIMIT 1",$username); return q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 AND `verified` = 1 LIMIT 1",$username);
if(count($results)) {
return true;
}
return false;
} }