I am trying to perform an LDAP search from hook_form_validation()
in a webform using Drupal 9 and drupal/ldap 4.3.
I have configured the LDAP module on my site, but I am not using it for authentication. I can however use ldap query and perform a test of the ldap server on /admin/config/people/ldap/server successfully.
At the bottom of API usage they say you can use the following code.
$factory = \Drupal::service('ldap.servers');
$server = $factory->getServerByIdEnabled('my_server');
I have been enlightend cliefen that the service is ldap.bridge. Here is my current code.
use Drupal\user\Entity\User;
use Drupal\taxonomy\Entity\Term;
use Drupal\ldap_servers\Entity\Server;
function drp_form_alter( &$form, &$form_state, $form_id ) {
if ( $form_id === 'webform_submission__myform_node_5930_add_form' ) {
$userid = \Drupal::currentUser()->id();
$username = \Drupal::currentUser()->getAccountName();
$server = \Drupal::service('entity_type.manager')->getStorage( 'ldap_server')->load('myserver');
$bridge = \Drupal::service('ldap.bridge');
$bridge->setServer( $server );
if ($bridge->bind()) {
// $dn ='ou=accounts,ou=departments,dc=servername,dc=someplace';
// $filter="(uid=xxxx1)";
// $returnattr = array( "sn", "givenname", "mail");
}
}
}
I was able to successfully bind the ldap server.
Question: how do I actually make an ldap search, using the drupal/ldap code, inside the bridge->bind if statement?
I did not use the $factory-> code that the API mentioned. Do I need to?
Thanks for all your help, I am struggling, but learning :)