DirectoryTree / LdapRecord-Laravel

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

[Bug] Use of undefined constant LDAP_ESCAPE_FILTER #339

Closed Nuvm closed 3 years ago

Nuvm commented 3 years ago

Environment:

Describe the bug: Whenever I try to log in through my normal auth routes, I get this warning logged as an error in my logs: dev.ERROR: Use of undefined constant LDAP_ESCAPE_FILTER - assumed 'LDAP_ESCAPE_FILTER' (this will throw an Error in a future version of PHP) - in vendor\directorytree\ldaprecord\src\Models\Attributes\EscapedValue.php:107 It's as if ldap\ldap.php wasn't loaded properly.. Sorry in advance if this is obvious; I'm not really good with the laravel Auth flow.

Stacktrace:

#0 \\vendor\\directorytree\\ldaprecord\\src\\Models\\Attributes\\EscapedValue.php(107): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(2, 'Use of undefine...', 'C:\\\\Users\\\
miyaz...', 107, Array)
#1 \\vendor\\directorytree\\ldaprecord\\src\\Query\\Builder.php(981): LdapRecord\\Models\\Attributes\\EscapedValue->both()
#2 \\vendor\\directorytree\\ldaprecord\\src\\Models\\Model.php(432): LdapRecord\\Query\\Builder->where('objectclass', '=', Object(LdapRecord\\Models\\Attributes\\EscapedValue))
#3 \\vendor\\directorytree\\ldaprecord\\src\\Models\\Model.php(399): LdapRecord\\Models\\Model->applyObjectClassScopes(Object(LdapRecord\\Query\\Model\\ActiveDirectoryBuilder))
#4 \\vendor\\directorytree\\ldaprecord\\src\\Models\\Model.php(296): LdapRecord\\Models\\Model->registerModelScopes(Object(LdapRecord\\Query\\Model\\ActiveDirectoryBuilder))
#5 \\vendor\\directorytree\\ldaprecord-laravel\\src\\LdapUserRepository.php(152): LdapRecord\\Models\\Model->newQuery()
#6 \\vendor\\directorytree\\ldaprecord-laravel\\src\\LdapUserRepository.php(116): LdapRecord\\Laravel\\LdapUserRepository->newModelQuery()
#7 \\vendor\\directorytree\\ldaprecord-laravel\\src\\LdapUserRepository.php(86): LdapRecord\\Laravel\\LdapUserRepository->query()
#8 \\vendor\\directorytree\\ldaprecord-laravel\\src\\Auth\\UserProvider.php(48): LdapRecord\\Laravel\\LdapUserRepository->findByCredentials(Array)
#9 [internal function]: LdapRecord\\Laravel\\Auth\\UserProvider->LdapRecord\\Laravel\\Auth\\{closure}(Array)
#10 \\vendor\\directorytree\\ldaprecord-laravel\\src\\Auth\\UserProvider.php(78): call_user_func(Object(Closure), Array)
#11 \\vendor\\directorytree\\ldaprecord-laravel\\src\\Auth\\DatabaseUserProvider.php(168): LdapRecord\\Laravel\\Auth\\UserProvider->fetchLdapUserByCredentials(Array)
#12 \\vendor\\laravel\\framework\\src\\Illuminate\\Support\\helpers.php(521): LdapRecord\\Laravel\\Auth\\DatabaseUserProvider->LdapRecord\\Laravel\\Auth\\{closure}()
#13 \\vendor\\directorytree\\ldaprecord-laravel\\src\\Auth\\DatabaseUserProvider.php(174): value(Object(Closure))
#14 \\vendor\\laravel\\framework\\src\\Illuminate\\Auth\\SessionGuard.php(357): LdapRecord\\Laravel\\Auth\\DatabaseUserProvider->retrieveByCredentials(Array)
[...]

I followed the database authentication and use the Active Directory built-in models. ldap:test succeeds.

LoginController


namespace CRMIDF\Http\Controllers\Auth;

use CRMIDF\Http\Controllers\Controller;
use CRMIDF\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use LdapRecord\Laravel\Auth\ListensForLdapBindFailure;

class LoginController extends Controller
{

    use AuthenticatesUsers, ListensForLdapBindFailure;

    protected $redirectTo = RouteServiceProvider::HOME;

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}

config/Auth.php:

<?php

return [

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],
    ],

    'providers' => [
        /*'users' => [
            'driver' => 'eloquent',
            'model' => CRMIDF\Models\User::class,
        ],*/
        'users' => [
            'driver' => 'ldap',
            'model' => LdapRecord\Models\ActiveDirectory\User::class,
            'rules' => [],
            'database' => [
                'model' => CRMIDF\Models\User::class,
                'sync_attributes' => [
                    'email' => 'mail',
                ],
                'sync_existing' => [ //Allows to sync using email if GUID is not found
                    'email' => 'mail',
                ],
            ],
        ],
    ],

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],
    ],

    'password_timeout' => 10800,

];

User model:

<?php

namespace CRMIDF\Models;

use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use LdapRecord\Laravel\Auth\AuthenticatesWithLdap;
use LdapRecord\Laravel\Auth\HasLdapUser;
use LdapRecord\Laravel\Auth\LdapAuthenticatable;

class User extends Authenticatable implements LdapAuthenticatable
{
    use SoftDeletes, Notifiable, AuthenticatesWithLdap;

    protected $fillable = [
        'name', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function employees()
    {
        return $this->hasMany(Employee::class);
    }
}

I've tried the User model with and without the HasLdapUser trait to no avail. Please note that our AD's distinguished names are in fact email addresses (email@domain.com).

stevebauman commented 3 years ago

Hi @Nuvm,

This is due to your web server having a PHP installation that does not have the ldap extension enabled. This is the only cause for this exception.

Please see:

Nuvm commented 3 years ago

I forgot about the webserver's php, you're right! Sorry for the bother and thank you!

stevebauman commented 3 years ago

No problem, happy to help @Nuvm! Glad you're up and running 😄