DirectoryTree / LdapRecord-Laravel

Multi-domain LDAP Authentication & Management for Laravel.
https://ldaprecord.com/docs/laravel/v3
MIT License
509 stars 54 forks source link

[Bug] FindOrFail failling with ldap_read() error #418

Closed grunk closed 2 years ago

grunk commented 2 years ago

Environment:

Describe the bug: Using User::findOrFail() throw an error while using User::where() works fine

Example :

$connection = Container::getDefaultConnection();
$user = User::findOrFail('samaccountname', 'myname');
if ($connection->auth()->attempt($user->getDn(), 'usersecret')) {
    dd("ok");
}

throws :

ldap_read(): Search: Invalid DN syntax

While

$connection = Container::getDefaultConnection();
$user = User::where('samaccountname', '=', 'myname')->first();
if ($connection->auth()->attempt($user->getDn(), 'usersecret')) {
    dd("ok");
}

Return "ok" as expected

Configuration is very basic :

LdapRecord\Configuration\DomainConfiguration {
  #options: array:11 [
    "hosts" => array:1 [
      0 => "10.190.0.21"
    ]
    "timeout" => "5"
    "version" => 3
    "port" => "389"
    "base_dn" => "OU=My Company,DC=my-domain,DC=com"
    "username" => "myadminuser@my-domain.com"
    "password" => "supersecret"
    "use_ssl" => false
    "use_tls" => false
    "follow_referrals" => false
    "options" => []
  ]
}
stevebauman commented 2 years ago

Hi @grunk,

You're looking for the findByOrFail() method. The findOrFail() method only accepts a distinguished name (hence the invalid DN syntax error):

https://ldaprecord.com/docs/core/v2/searching-api/#findbyorfail

$query = $connection->query();

try {
    $entry = $query->findByOrFail('samaccountname', 'johndoe');
} catch (\LdapRecord\Models\ModelNotFoundException $ex) {
    // Not found.
}