+ /**
+ * Bind to the given LDAP connection using the given credentials.
+ * MUST throw an exception on failure.
+ *
+ * @param resource $connection
+ *
+ * @throws LdapFailedBindException
+ */
+ protected function bindGivenUser($connection, string $dn = null, string $password = null): void
+ {
+ $ldapBind = $this->ldap->bind($connection, $dn, $password);
+
+ if (!$ldapBind) {
+ throw new LdapFailedBindException('Failed to bind with given user details');
+ }
+ }
+
+ /**
+ * Bind the system user to the LDAP connection using the configured credentials otherwise anonymous
+ * access is attempted. MUST throw an exception on failure.
+ *
+ * @param resource $connection
+ *
+ * @throws LdapFailedBindException
+ */
+ protected function bindSystemUser($connection): void
+ {
+ $ldapDn = $this->config['dn'];
+ $ldapPass = $this->config['pass'];
+
+ $isAnonymous = ($ldapDn === false || $ldapPass === false);
+ if ($isAnonymous) {
+ $ldapBind = $this->ldap->bind($connection);
+ } else {
+ $ldapBind = $this->ldap->bind($connection, $ldapDn, $ldapPass);
+ }
+
+ if (!$ldapBind) {
+ throw new LdapFailedBindException(($isAnonymous ? trans('errors.ldap_fail_anonymous') : trans('errors.ldap_fail_authed')));
+ }
+ }
+