chg: reduce complexity of authenticate function

* create function getEmailAddress to reduce the complexity of the authenticate function
* fix indentation of comments in the authenticate function
pull/3589/head
iwitz 2018-09-06 11:32:29 +02:00 committed by GitHub
parent f7283cda65
commit c42a5cc6f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 11 deletions

View File

@ -38,6 +38,17 @@ class ApacheAuthenticate extends BaseAuthenticate
}
return $returnCode;
}
private function getEmailAddress($ldapEmailField, $ldapUserData)
{
// return the email address of an LDAP user if one of the fields in $ldapEmaiLField exists
foreach($ldapEmailField as $field) {
if (isset($ldapUserData[0][$field][0])) {
return $ldapUserData[0][$field][0];
}
}
return NULL;
}
public function authenticate(CakeRequest $request, CakeResponse $response)
{
@ -81,23 +92,18 @@ class ApacheAuthenticate extends BaseAuthenticate
or die("Error in LDAP search query: " . ldap_error($ldapconn));
$ldapUserData = ldap_get_entries($ldapconn, $result);
// find the email address in the query's result
// first if the ldapEmailField option is not specified, look for the email address in the default field
if (!isset($ldapEmailField) && isset($ldapUserData[0]['mail'][0])) {
// first if the ldapEmailField option is not specified, look for the email address in the default field
if (!isset($ldapEmailField) && isset($ldapUserData[0]['mail'][0])) {
// assign the real user for MISP
$mispUsername = $ldapUserData[0]['mail'][0];
} else if (isset($ldapEmailField)) {
// if the ldapEmailField is set, use it to find the email address
foreach ($ldapEmailField as $field) {
if (isset($ldapUserData[0][$field][0])) {
$mispUsername = $ldapUserData[0][$field][0];
break;
}
}
} else {
$mispUsername = $this->getEmailAddress($ldapEmailField, $ldapUserData);
} else {
die("User not found in LDAP");
}
// close LDAP connection
ldap_close($ldapconn);
}