WIP develop ldap fixes (errors, check if disabled, parsing in one place) (#6500)

* Fix errors and exception when ldap settings are empty (even with ldap disabled)

* Re-add newline at the end of file
This commit is contained in:
Steffen 2018-12-12 06:01:11 +01:00 committed by snipe
parent 93947b09c5
commit 28edf13457
2 changed files with 58 additions and 30 deletions

View file

@ -54,9 +54,10 @@ class LdapAd extends LdapAdConfiguration
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();
if($this->isLdapEnabled()) {
$this->ldap = new Adldap(); $this->ldap = new Adldap();
$this->ldap->addProvider($this->ldapConfig); $this->ldap->addProvider($this->ldapConfig);
}
} }
/** /**

View file

@ -43,14 +43,16 @@ class LdapAdConfiguration
public function __construct() public function __construct()
{ {
$this->ldapSettings = $this->getSnipeItLdapSettings(); $this->ldapSettings = $this->getSnipeItLdapSettings();
$this->setSnipeItConfig(); if ($this->isLdapEnabled()) {
$this->setSnipeItConfig();
}
} }
/** /**
* Merge the default Adlap config with the SnipeIT config. * Merge the default Adlap config with the SnipeIT config.
* *
* @author Wes Hulette <jwhulette@gmail.com> * @author Wes Hulette <jwhulette@gmail.com>
* *
* @since 5.0.0 * @since 5.0.0
*/ */
private function setSnipeItConfig() private function setSnipeItConfig()
@ -65,7 +67,7 @@ class LdapAdConfiguration
* @author Wes Hulette <jwhulette@gmail.com> * @author Wes Hulette <jwhulette@gmail.com>
* *
* @since 5.0.0 * @since 5.0.0
* *
* @return \Illuminate\Support\Collection * @return \Illuminate\Support\Collection
*/ */
private function getSnipeItLdapSettings(): Collection private function getSnipeItLdapSettings(): Collection
@ -80,8 +82,9 @@ class LdapAdConfiguration
if (in_array($key, self::LDAP_BOOLEAN_SETTINGS)) { if (in_array($key, self::LDAP_BOOLEAN_SETTINGS)) {
return boolval($item); return boolval($item);
} }
// Decrypt the admin password // Decrypt the admin password
if (('ldap_pword' === $key) && ($item!='')) { if ('ldap_pword' === $key && !empty($item)) {
try { try {
return decrypt($item); return decrypt($item);
} catch (Exception $e) { } catch (Exception $e) {
@ -89,6 +92,10 @@ class LdapAdConfiguration
} }
} }
if ('ldap_server' === $key) {
return collect(parse_url($item));
}
return $item; return $item;
}); });
@ -122,7 +129,7 @@ class LdapAdConfiguration
* @author Wes Hulette <jwhulette@gmail.com> * @author Wes Hulette <jwhulette@gmail.com>
* *
* @since 5.0.0 * @since 5.0.0
* *
* @return array * @return array
*/ */
private function setLdapConnectionConfiguration(): array private function setLdapConnectionConfiguration(): array
@ -184,15 +191,10 @@ class LdapAdConfiguration
*/ */
private function getPort(): int private function getPort(): int
{ {
$ldapUrl = $this->ldapSettings['ldap_server']; $port = $this->getLdapServerData('port');
if ($ldapUrl) { if ($port && is_int($port)) {
$port = parse_url($ldapUrl, PHP_URL_PORT); return $port;
if (is_int($port)) {
return $port;
}
} }
return self::LDAP_PORT; return self::LDAP_PORT;
} }
@ -207,15 +209,10 @@ class LdapAdConfiguration
*/ */
private function isSsl(): bool private function isSsl(): bool
{ {
if ($this->ldapSettings['ldap_server']) { $scheme = $this->getLdapServerData('scheme');
$scheme = explode('://', $this->ldapSettings['ldap_server']); if ($scheme && 'ldaps' === strtolower($scheme)) {
if ('ldap' === strtolower($scheme[0])) {
return false;
}
return true; return true;
} }
return false; return false;
} }
@ -236,13 +233,43 @@ class LdapAdConfiguration
})->toArray(); })->toArray();
} }
if ($this->ldapSettings['ldap_server']) { $url = $this->getLdapServerData('host');
$parts = explode('//', $this->ldapSettings['ldap_server']); return $url ? [$url] : [];
return [ }
$parts[1],
]; /**
* Get ldap enabled setting
*
* @author Steffen Buehl <sb@sbuehl.com>
*
* @since 5.0.0
*
* @return bool
*/
protected function isLdapEnabled(): bool
{
return $this->ldapSettings && $this->ldapSettings->get('ldap_enabled');
}
/**
* Get parsed ldap server information
*
* @author Steffen Buehl <sb@sbuehl.com>
*
* @since 5.0.0
*
* @param $key
* @return mixed|null
*/
protected function getLdapServerData($key)
{
if ($this->ldapSettings) {
$ldapServer = $this->ldapSettings->get('ldap_server');
if ($ldapServer && $ldapServer instanceof Collection) {
return $ldapServer->get($key);
}
} }
return []; return null;
} }
} }