Closed kirkaracha closed 2 years ago
Hi @kirkaracha! Thanks so much for the sponsorship ❤️
Are you binding LdapSearchService
into Laravel's IoC, or are you letting it be resolved automatically? I.e. binding it into the container via:
// ../AppServiceProvider.php
public function bind()
{
$this->app->bind(LdapSearchService::class, function () {
return new LdapSearchService(Container::getInstance());
});
}
The LdapRecord Container
is meant to be a singleton. It's self-managed, and its instance can be accessed like so:
use LdapRecord\Container;
$container = Container::getInstance();
What's likely happening here is that your LdapSearchService
is getting a new Container
instance instead of the singleton instance containing the default connection.
This should resolve your issue:
final class LdapSearchService
{
// ...
public function __construct() {
$this->container = Container::getInstance();
$this->connection = $this->container->getDefaultConnection();
}
Let me know if that works! 👍
Note: You can see how the container is utilized and how connections are registered in LdapRecord-Laravel via the
LdapServiceProvider
here. Only static calls are used, since the container is a singleton:
Good news, not-so-good news. The Container::getInstance()
change searches successfully, but I'm having trouble accessing the values in the results.
The query returns an array of data for each user like this:
Array
(
[objectclass] => Array
(
[count] => 4
[0] => top
[1] => person
[2] => organizationalPerson
[3] => user
)
[0] => objectclass
[sn] => Array
(
[count] => 1
[0] => Test 5
)
[1] => sn
[givenname] => Array
(
[count] => 1
[0] => Munis
)
[2] => givenname
[distinguishedname] => Array
(
[count] => 1
[0] => CN=Munis Test 5,OU=Users,OU=Information Technology,OU=ALAMEDA,DC=coa,DC=domain
)
[3] => distinguishedname
[displayname] => Array
(
[count] => 1
[0] => Munis Test 5
)
[4] => displayname
[samaccountname] => Array
(
[count] => 1
[0] => munistest5
)
[5] => samaccountname
[mail] => Array
(
[count] => 1
[0] => munistest5@alamedaca.gov
)
[6] => mail
[count] => 7
[dn] => CN=Munis Test 5,OU=Users,OU=Information Technology,OU=ALAMEDA,DC=coa,DC=domain
)
Doing a foreach on users $this->info($user['sn'][0]);
results in a Undefined array key "sn"
error.
I was able to access the values that way in the earlier version of LdapRecord.
Doing a foreach on users $this->info($user['sn'][0]); results in a Undefined array key "sn" error.
That sounds like you have a user that does not have a surname (sn
) attribute. You will have to use the null coalesce operator so PHP doesn't thrown an exception when it's null:
$this->info($user['sn'][0] ?? null);
If you use LdapRecord models, it's much easier to work with query results, rather than running raw queries:
use LdapRecord\Models\Entry;
$objects = Entry::get();
foreach ($objects as $object) {
// Safely returns `null` if the attribute doesn't exist.
$object->getFirstAttribute('sn');
}
The user does have a sn attribute (see sample array of data above), I'm just having trouble with the syntax to access it.
I'm not tied to that approach though. What would you recommend for an automated job that gets users by department?
The user does have a sn attribute (see sample array of data above), I'm just having trouble with the syntax to access it.
Doing a foreach on users $this->info($user['sn'][0]); results in a Undefined array key "sn" error.
You mentioned you're doing a foreach
on a result of many users. One of those users must not have an sn
.
I'm not tied to that approach though. What would you recommend for an automated job that gets users by department?
I would recommend using LdapRecord models over raw queries unless you're doing something unsupported by the LdapRecord query builder. Working with results will be much easier. It's the equivalent of using DB::table('users')->...
vs User::get()
in Laravel.
Closing due to inactivity.
I have these settings in my .env file; I do not have a config file:
Running
php artisan ldap:test
shows a successful connection:I wrote an LdapSearchService:
I call this service from a command:
When I call my command using
php artisan directory:import-users
, I get an error saying the default connection doesn't exist, even though the test command shows the default connection does exist.For now I'm just trying to get a department's users in the search service. I need to import by department to assign roles and permissions, etc.
The same LdapSearchService code worked with v1.7.6 of directorytree/ldaprecord-laravel.
I know LdapRecord-Laravel has an import, but filtering doesn't work (I opened this issue):
Running
php artisan ldap:import
by itself doesn't do anything.I've been trying to get past this for a couple of weeks off and on and I need to resolve it this week or go to Plan B.
Environment: