8686 * @property string ldapAdminGroup
8787 */
8888class Connection extends LDAPUtility {
89- /**
90- * @var resource|\LDAP\Connection|null
91- */
92- private $ ldapConnectionRes = null ;
93-
94- /**
95- * @var string
96- */
97- private $ configPrefix ;
98-
99- /**
100- * @var ?string
101- */
102- private $ configID ;
103-
104- /**
105- * @var bool
106- */
107- private $ configured = false ;
89+ private ?\LDAP \Connection $ ldapConnectionRes = null ;
90+ private string $ configPrefix ;
91+ private ?string $ configID ;
92+ private bool $ configured = false ;
10893
10994 /**
11095 * @var bool whether connection should be kept on __destruct
11196 */
112- private $ dontDestruct = false ;
97+ private bool $ dontDestruct = false ;
11398
11499 /**
115100 * @var bool runtime flag that indicates whether supported primary groups are available
@@ -241,14 +226,11 @@ public function init($force = false) {
241226 }
242227
243228 /**
244- * @return resource| \LDAP\Connection The LDAP resource
229+ * @return \LDAP\Connection The LDAP resource
245230 */
246- public function getConnectionResource () {
231+ public function getConnectionResource (): \ LDAP \ Connection {
247232 if (!$ this ->ldapConnectionRes ) {
248233 $ this ->init ();
249- } elseif (!$ this ->ldap ->isResource ($ this ->ldapConnectionRes )) {
250- $ this ->ldapConnectionRes = null ;
251- $ this ->establishConnection ();
252234 }
253235 if (is_null ($ this ->ldapConnectionRes )) {
254236 $ this ->logger ->error (
@@ -263,7 +245,7 @@ public function getConnectionResource() {
263245 /**
264246 * resets the connection resource
265247 */
266- public function resetConnectionResource () {
248+ public function resetConnectionResource (): void {
267249 if (!is_null ($ this ->ldapConnectionRes )) {
268250 @$ this ->ldap ->unbind ($ this ->ldapConnectionRes );
269251 $ this ->ldapConnectionRes = null ;
@@ -273,9 +255,8 @@ public function resetConnectionResource() {
273255
274256 /**
275257 * @param string|null $key
276- * @return string
277258 */
278- private function getCacheKey ($ key ) {
259+ private function getCacheKey ($ key ): string {
279260 $ prefix = 'LDAP- ' .$ this ->configID .'- ' .$ this ->configPrefix .'- ' ;
280261 if (is_null ($ key )) {
281262 return $ prefix ;
@@ -332,9 +313,8 @@ public function clearCache() {
332313 * Caches the general LDAP configuration.
333314 * @param bool $force optional. true, if the re-read should be forced. defaults
334315 * to false.
335- * @return null
336316 */
337- private function readConfiguration ($ force = false ) {
317+ private function readConfiguration (bool $ force = false ): void {
338318 if ((!$ this ->configured || $ force ) && !is_null ($ this ->configID )) {
339319 $ this ->configuration ->readConfiguration ();
340320 $ this ->configured = $ this ->validateConfiguration ();
@@ -406,7 +386,7 @@ public function getConfiguration() {
406386 return $ result ;
407387 }
408388
409- private function doSoftValidation () {
389+ private function doSoftValidation (): void {
410390 //if User or Group Base are not set, take over Base DN setting
411391 foreach (['ldapBaseUsers ' , 'ldapBaseGroups ' ] as $ keyBase ) {
412392 $ val = $ this ->configuration ->$ keyBase ;
@@ -461,13 +441,9 @@ private function doSoftValidation() {
461441 }
462442 }
463443
464- /**
465- * @return bool
466- */
467- private function doCriticalValidation () {
444+ private function doCriticalValidation (): bool {
468445 $ configurationOK = true ;
469- $ errorStr = 'Configuration Error (prefix ' .
470- (string )$ this ->configPrefix .'): ' ;
446+ $ errorStr = 'Configuration Error (prefix ' . $ this ->configPrefix . '): ' ;
471447
472448 //options that shall not be empty
473449 $ options = ['ldapHost ' , 'ldapUserDisplayName ' ,
@@ -552,7 +528,7 @@ private function doCriticalValidation() {
552528 * Validates the user specified configuration
553529 * @return bool true if configuration seems OK, false otherwise
554530 */
555- private function validateConfiguration () {
531+ private function validateConfiguration (): bool {
556532 if ($ this ->doNotValidate ) {
557533 //don't do a validation if it is a new configuration with pure
558534 //default values. Will be allowed on changes via __set or
@@ -575,7 +551,7 @@ private function validateConfiguration() {
575551 *
576552 * @throws ServerNotAvailableException
577553 */
578- private function establishConnection () {
554+ private function establishConnection (): ? bool {
579555 if (!$ this ->configuration ->ldapConfigurationActive ) {
580556 return null ;
581557 }
@@ -663,15 +639,18 @@ private function establishConnection() {
663639 /**
664640 * @param string $host
665641 * @param string $port
666- * @return bool
667642 * @throws \OC\ServerNotAvailableException
668643 */
669- private function doConnect ($ host , $ port ) {
644+ private function doConnect ($ host , $ port ): bool {
670645 if ($ host === '' ) {
671646 return false ;
672647 }
673648
674- $ this ->ldapConnectionRes = $ this ->ldap ->connect ($ host , $ port );
649+ $ this ->ldapConnectionRes = $ this ->ldap ->connect ($ host , $ port ) ?: null ;
650+
651+ if ($ this ->ldapConnectionRes === null ) {
652+ throw new ServerNotAvailableException ('Connection failed ' );
653+ }
675654
676655 if (!$ this ->ldap ->setOption ($ this ->ldapConnectionRes , LDAP_OPT_PROTOCOL_VERSION , 3 )) {
677656 throw new ServerNotAvailableException ('Could not set required LDAP Protocol version. ' );
0 commit comments