Clean code and fix potential issues

This commit is contained in:
Développeur égaré 2015-03-06 22:34:42 +01:00
parent 8712ebc0a3
commit e5252af61b

View file

@ -66,10 +66,13 @@ 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'])) {
$results = get_existing_account($b['username']);
if(! empty($results)){
$b['user_record'] = $results[0]; $b['user_record'] = $results[0];
$b['authenticated'] = 1; $b['authenticated'] = 1;
} }
}
return; return;
} }
@ -85,11 +88,6 @@ function ldapauth_authenticate($username,$password) {
$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'))
&& (strlen($ldap_server)))) && (strlen($ldap_server))))
@ -123,8 +121,16 @@ function ldapauth_authenticate($username,$password) {
if(! @ldap_bind($connect,$dn,$password)) if(! @ldap_bind($connect,$dn,$password))
return false; return false;
$emailarray = [];
$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); $emailarray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_emailattribute);
$namearray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_nameattribute); $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]);
@ -160,7 +166,9 @@ function ldapauth_authenticate($username,$password) {
} }
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"){
$results = get_existing_account($username);
if(empty($results)){
if (strlen($email) > 0 && strlen($name) > 0){ if (strlen($email) > 0 && strlen($name) > 0){
$arr = array('username'=>$name,'nickname'=>$username,'email'=>$email,'password'=>$password,'verified'=>1); $arr = array('username'=>$name,'nickname'=>$username,'email'=>$email,'password'=>$password,'verified'=>1);
$result = create_user($arr); $result = create_user($arr);
@ -173,12 +181,9 @@ function ldap_autocreateaccount($ldap_autocreateaccount,$username,$password,$ema
logger("ldapauth: unable to create account, no email or nickname found"); 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;
} }