DirectoryTree / LdapRecord-Discussions

A place to ask questions, get help, or share what you've built with LdapRecord.
4 stars 1 forks source link

Deleting a user after searching with the findBy () method does not recognize the delete () function. #2

Closed yuriang85 closed 4 years ago

yuriang85 commented 4 years ago

$user = User::findBy('mail', 'user@local.com');

if (!$user->delete()) { return false; } else return true;

returns this: Call to a member function delete () on null.

stevebauman commented 4 years ago

Hi @yuriang85,

The findBy method will return null when no results are returned from your LDAP directory.

You must use:

$user = User::findBy('mail', 'user@local.com');

if ($user) {
    $user->delete();
}

Conversely, if you want to avoid checking for null, you can instead use the findByOrFail method that will throw an exception instead:

try {
    $user = User::findByOrFail('mail', 'user@local.com');

    $user->delete();
} catch (LdapRecord\Models\ModelNotFoundException $e) {
    // User not found.
}

Docs:

https://ldaprecord.com/docs/searching/#executing-searches

I would check your directory to ensure there is indeed a user with the given mail attribute you are looking for.

I would also ensure that your User model does not have any global scopes that may be affecting the lookup of this user in your directory.

yuriang85 commented 4 years ago

OK thank you very much. resolved!

stevebauman commented 4 years ago

Glad to help @yuriang85! Feel free to create another issue if you have any further questions :smile: