Skip to content

A free open source IT asset/license management system

Hopefully fixes #8418 - migrations on new install go wonky because of LDAP service provider

snipecb5104c27d5a
diff --git a/app/Services/LdapAd.php b/app/Services/LdapAd.php
index d958da4b22..608bf829eb 100644
--- a/app/Services/LdapAd.php
+++ b/app/Services/LdapAd.php
@@ -57,7 +57,7 @@ class LdapAd extends LdapAdConfiguration
public function init()
{
// Already initialized
- if($this->ldap) {
+ if ($this->ldap) {
return true;
}
diff --git a/app/Services/LdapAdConfiguration.php b/app/Services/LdapAdConfiguration.php
index 01e9564d46..8c912edb53 100644
--- a/app/Services/LdapAdConfiguration.php
+++ b/app/Services/LdapAdConfiguration.php
@@ -52,10 +52,19 @@ class LdapAdConfiguration
* @since 5.0.0
*/
public function init() {
- $this->ldapSettings = $this->getSnipeItLdapSettings();
- if ($this->isLdapEnabled()) {
- $this->setSnipeItConfig();
+
+ // This try/catch is dumb, but is necessary to run initial migrations, since
+ // this service provider is booted even during migrations. :( - snipe
+ try {
+ $this->ldapSettings = $this->getSnipeItLdapSettings();
+ if ($this->isLdapEnabled()) {
+ $this->setSnipeItConfig();
+ }
+ } catch (\Exception $e) {
+ \Log::debug($e);
+ $this->ldapSettings = null;
}
+
}
/**
@@ -82,33 +91,35 @@ class LdapAdConfiguration
*/
private function getSnipeItLdapSettings(): Collection
{
- $ldapSettings = Setting::getLdapSettings()
- ->map(function ($item, $key) {
- // Trim the items
- if (is_string($item)) {
- $item = trim($item);
- }
- // Get the boolean value of the LDAP setting, makes it easier to work with them
- if (in_array($key, self::LDAP_BOOLEAN_SETTINGS)) {
- return boolval($item);
- }
-
- // Decrypt the admin password
- if ('ldap_pword' === $key && !empty($item)) {
- try {
- return decrypt($item);
- } catch (Exception $e) {
- throw new Exception('Your app key has changed! Could not decrypt LDAP password using your current app key, so LDAP authentication has been disabled. Login with a local account, update the LDAP password and re-enable it in Admin > Settings.');
+ $ldapSettings = collect();
+ if(Setting::first()) { // during early migration steps, there may be no settings table entry to start with
+ $ldapSettings = Setting::getLdapSettings()
+ ->map(function ($item, $key) {
+ // Trim the items
+ if (is_string($item)) {
+ $item = trim($item);
+ }
+ // Get the boolean value of the LDAP setting, makes it easier to work with them
+ if (in_array($key, self::LDAP_BOOLEAN_SETTINGS)) {
+ return boolval($item);
}
- }
- if ($item && 'ldap_server' === $key) {
- return collect(parse_url($item));
- }
+ // Decrypt the admin password
+ if ('ldap_pword' === $key && !empty($item)) {
+ try {
+ return decrypt($item);
+ } catch (Exception $e) {
+ throw new Exception('Your app key has changed! Could not decrypt LDAP password using your current app key, so LDAP authentication has been disabled. Login with a local account, update the LDAP password and re-enable it in Admin > Settings.');
+ }
+ }
- return $item;
- });
+ if ($item && 'ldap_server' === $key) {
+ return collect(parse_url($item));
+ }
+ return $item;
+ });
+ }
return $ldapSettings;
}
diff --git a/database/migrations/2020_02_04_172100_add_ad_append_domain_settings.php b/database/migrations/2020_02_04_172100_add_ad_append_domain_settings.php
index 0a786b7aed..aced0bf111 100644
--- a/database/migrations/2020_02_04_172100_add_ad_append_domain_settings.php
+++ b/database/migrations/2020_02_04_172100_add_ad_append_domain_settings.php
@@ -19,7 +19,7 @@ class AddAdAppendDomainSettings extends Migration
});
$s = Setting::first(); // we are deliberately *not* using the ::getSettings() method, as it caches things, and our Settings table is being migrated right now
- if($s->is_ad && $s->ldap_enabled && $s->ad_domain) { //backwards-compatibility setting; < v5 always appended AD Domains
+ if ($s && $s->is_ad && $s->ldap_enabled && $s->ad_domain) { //backwards-compatibility setting; < v5 always appended AD Domains
$s->ad_append_domain = 1;
$s->save();
}