Browse Source

Add LDAP test button

causefx 7 years ago
parent
commit
d3cf780c70

+ 1 - 1
api/functions/auth-functions.php

@@ -218,7 +218,7 @@ if (function_exists('ldap_connect')) {
 				'username' => (empty($GLOBALS['ldapBindUsername'])) ? null : $GLOBALS['ldapBindUsername'],
 				'password' => (empty($GLOBALS['ldapBindPassword'])) ? null : decrypt($GLOBALS['ldapBindPassword']),
 				// Optional Configuration Options
-				'schema' => (($GLOBALS['ldapType'] == 1) ? Adldap\Schemas\ActiveDirectory::class : (($GLOBALS['ldapType'] == 2) ? Adldap\Schemas\OpenLDAP::class : Adldap\Schemas\FreeIPA::class)),
+				'schema' => (($GLOBALS['ldapType'] == '1') ? Adldap\Schemas\ActiveDirectory::class : (($GLOBALS['ldapType'] == '2') ? Adldap\Schemas\OpenLDAP::class : Adldap\Schemas\FreeIPA::class)),
 				'account_prefix' => '',
 				'account_suffix' => '',
 				'port' => $ldapPort,

+ 55 - 0
api/functions/homepage-connect-functions.php

@@ -2233,6 +2233,61 @@ function testAPIConnection($array)
 				};
 			}
 			break;
+		case 'ldap':
+			if (!empty($GLOBALS['authBaseDN']) && !empty($GLOBALS['authBackendHost'])) {
+				$ad = new \Adldap\Adldap();
+				// Create a configuration array.
+				$ldapServers = explode(',', $GLOBALS['authBackendHost']);
+				$i = 0;
+				foreach ($ldapServers as $key => $value) {
+					// Calculate parts
+					$digest = parse_url(trim($value));
+					$scheme = strtolower((isset($digest['scheme']) ? $digest['scheme'] : 'ldap'));
+					$host = (isset($digest['host']) ? $digest['host'] : (isset($digest['path']) ? $digest['path'] : ''));
+					$port = (isset($digest['port']) ? $digest['port'] : (strtolower($scheme) == 'ldap' ? 389 : 636));
+					// Reassign
+					$ldapHosts[] = $host;
+					if ($i == 0) {
+						$ldapPort = $port;
+					}
+					$i++;
+				}
+				$config = [
+					// Mandatory Configuration Options
+					'hosts' => $ldapHosts,
+					'base_dn' => $GLOBALS['authBaseDN'],
+					'username' => (empty($GLOBALS['ldapBindUsername'])) ? null : $GLOBALS['ldapBindUsername'],
+					'password' => (empty($GLOBALS['ldapBindPassword'])) ? null : decrypt($GLOBALS['ldapBindPassword']),
+					// Optional Configuration Options
+					'schema' => (($GLOBALS['ldapType'] == '1') ? Adldap\Schemas\ActiveDirectory::class : (($GLOBALS['ldapType'] == '2') ? Adldap\Schemas\OpenLDAP::class : Adldap\Schemas\FreeIPA::class)),
+					'account_prefix' => '',
+					'account_suffix' => '',
+					'port' => $ldapPort,
+					'follow_referrals' => false,
+					'use_ssl' => false,
+					'use_tls' => false,
+					'version' => 3,
+					'timeout' => 5,
+					// Custom LDAP Options
+					'custom_options' => [
+						// See: http://php.net/ldap_set_option
+						//LDAP_OPT_X_TLS_REQUIRE_CERT => LDAP_OPT_X_TLS_HARD
+					]
+				];
+				// Add a connection provider to Adldap.
+				$ad->addProvider($config);
+				try {
+					// If a successful connection is made to your server, the provider will be returned.
+					$provider = $ad->connect();
+				} catch (\Adldap\Auth\BindException $e) {
+					writeLog('error', 'LDAP Function - Error: ' . $e->getMessage(), 'SYSTEM');
+					return $e->getMessage();
+					// There was an issue binding / connecting to the server.
+				}
+				return ($provider) ? true : false;
+			}
+			return false;
+			break;
 		default :
 			return false;
 	}

+ 8 - 0
api/functions/organizr-functions.php

@@ -591,6 +591,14 @@ function getSettingsMain()
 				'label' => 'Password',
 				'value' => $GLOBALS['ldapBindPassword']
 			),
+			array(
+				'type' => 'button',
+				'label' => 'Test Connection',
+				'icon' => 'fa fa-flask',
+				'class' => 'ldapAuth switchAuth',
+				'text' => 'Test Connection',
+				'attr' => 'onclick="testAPIConnection(\'ldap\')"'
+			),
 			array(
 				'type' => 'input',
 				'name' => 'embyURL',