Kirk.Franklin's avatar

LdapRecord: Connection doesn't exist error after successful test

When I run php artisan:test the default connection is successful, but when I run php artisan I get an error saying the connection doesn't exist.

config/ldap.php settings:

'default' => env('LDAP_CONNECTION', 'default'),

    'connections' => [
        'default' => [
            'hosts' => [env('LDAP_HOST', '127.0.0.1')],
            'username' => env('LDAP_USERNAME', 'cn=user,dc=local,dc=com'),
            'password' => env('LDAP_PASSWORD', 'secret'),
            'port' => env('LDAP_PORT', 389),
            'base_dn' => env('LDAP_BASE_DN', 'dc=local,dc=com'),
            'timeout' => env('LDAP_TIMEOUT', 5),
            'use_ssl' => env('LDAP_SSL', false),
            'use_tls' => env('LDAP_TLS', false),
        ],
    ],

.env settings (username and password copied from working environment)

LDAP_LOGGING=true
LDAP_CONNECTION=default
LDAP_CONNECTIONS=default

LDAP_DEFAULT_HOSTS=******.***.domain
LDAP_DEFAULT_USERNAME=COA\*********
LDAP_DEFAULT_PASSWORD=wEN2%By9lO2i29Vy%Qrg6NUZ5airjBOf$Oz

LDAP_DEFAULT_PORT=389
LDAP_DEFAULT_BASE_DN="dc=***,dc=domain"
LDAP_DEFAULT_TIMEOUT=5
LDAP_DEFAULT_SSL=false
LDAP_DEFAULT_TLS=false

Laravel 9.19 PHP 8.1 directorytree/ldaprecord-laravel 2.5.6

Configuration docs: https://ldaprecord.com/docs/laravel/v2/configuration#using-an-environment-file-env

I uninstalled and reinstalled LdapRecord with the same result.

0 likes
11 replies
Kirk.Franklin's avatar

@neilstee Clearing the config fixed the error showing up when I run just php artisan, but I still get the connection doesn't exist error.

ImportUsers command:

    public function handle(LdapSearchService $ldapSearchService): void
    {
        $users = $ldapSearchService->getUsersByTeamLdapName('IT');

        foreach ($users as $user) {
            $this->info($user['samaccountname'][0]);
        }
    }

LdapSearchService:

    protected Connection $connection;
    protected Container $container;

    public function __construct(Container $container) {
        $this->container = $container;
        $this->connection = $this->container->getDefaultConnection();
    }

    public function getUsersByTeamLdapName(string $teamLdapName): Collection|array
    {
        $query = $this->connection
            ->query()
            ->setDn("ou=Users,ou=$teamLdapName,ou=ALAMEDA,dc=coa,dc=domain")
            ->select(self::LDAP_ATTRIBUTES);

        return $query->get();
    }

In the console:

λ php artisan ldap:test
Testing LDAP connection [default]...
| Connection | Successful | Username          | Message                 | Response Time |
| default    | ✔ Yes      | COA\anchoradadmin | Successfully connected. | 36.24ms       |

C:\inetpub\the-anchor (main -> origin)
λ php artisan directory:import-users

   LdapRecord\ContainerException

  The LDAP connection [default] does not exist.

  at C:\inetpub\the-anchor\vendor\directorytree\ldaprecord\src\ConnectionManager.php:162
    158▕         if ($this->exists($name = $name ?? $this->default)) {
    159▕             return $this->connections[$name];
    160▕         }
    161▕
  ➜ 162▕         throw new ContainerException("The LDAP connection [$name] does not exist.");
    163▕     }
    164▕
    165▕     /**
    166▕      * Return the default connection.

  1   C:\inetpub\the-anchor\vendor\directorytree\ldaprecord\src\ConnectionManager.php:172
      LdapRecord\ConnectionManager::get()

  2   C:\inetpub\the-anchor\vendor\directorytree\ldaprecord\src\ConnectionManager.php:101
      LdapRecord\ConnectionManager::getDefault()
Sinnbeck's avatar

@Kirk.Franklin if you are using the package, why not use the built in import? I just wrote a command that calls the build in command with the proper parameters

Kirk.Franklin's avatar

@Sinnbeck I'd like to use the built-in import, but I need to import by departments to assign roles and permissions. I asked the repo maintainer, but his example doesn't return any results.

> php artisan ldap:import users --filter "(distinguishedName=*ou\=City Clerk*)"`

There were no users found to import.

Is that different than your command?

Sinnbeck's avatar

Can you show the exact error and the code that triggers it?

psrz's avatar

Maybe I'm completly off, but in your ldap.php there are entries reading variables like 'LDAP_HOST' AND 'LDAP_BASE_DN' but in your .env file apparently there is only 'LDAP_DEFAULT_HOSTS', 'LDAP_DEFAULT_BASE_DN'. I doubt that's going to work.

If you open a tinker session, what does config('ldap.connections.default') show ? Is that array properly populated ?

Edit: by "properly" I mean with the values you have in your .env file, not with the default ones that are set on the calls to the env() function in ldap.php

1 like
Kirk.Franklin's avatar

@psrz Thanks, good catch. Per the repo maintainer, you should use either .env or a config file, not both.

I tried with just putting settings only in .env (preferred), but I get an error.

> php artisan ldap:test
No LDAP connections have been defined.

If I only have the settings in a config file, php artisan ldap:test shows a successful connection, and tinker shows the correct settings from the config file.

psrz's avatar
psrz
Best Answer
Level 10

@Kirk.Franklin Damn, kinda strange they say the config file and the .env file are mutually exclusive. The way I see it, the .env is mostly to complement config files in your laravel project

We have ldaprecord 2.5.6 on a Laravel 8 project and we haven't had any issues and we are using both files.

In the ldap.php everything is hardcoded except LDAP_USERNAME and LDAP_PASSWORD both which we are setting in our .env

Like I said, we have no issues using both files but maybe that's where "may lead to unexpected results" comes in. I guess we fell on the good side of the unexpected.

Kirk.Franklin's avatar

Storing only the username and password in the .env file and moving all other settings to config/ldap.php fixed the issue.

Please or to participate in this conversation.