Adldap2 / Adldap2-Laravel

LDAP Authentication & Management for Laravel
MIT License
910 stars 184 forks source link

Model binding is always null #692

Closed benjivm closed 5 years ago

benjivm commented 5 years ago

Description:

Model Binding always returns null. All other functionality works as expected: logging in, data synchronization, etc. Here is my User.php:

use Adldap\Laravel\Traits\HasLdapUser;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable, HasLdapUser;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'username', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

From reading the documentation I should now be able to access the LdapUser model via $user->ldap, however this always returns null.

stevebauman commented 5 years ago

Thanks @benjivm! I'm able to reproduce this in v6.0. Patch is coming shortly.

benjivm commented 5 years ago

Dang you're fast, thanks @stevebauman

stevebauman commented 5 years ago

Thanks @benjivm! I try, as I use Adldap2-Laravel in over 10 production applications internally.

Release is being created now, thanks again for bringing this up! My current tests didn't cover the byModel() method on the UserResolver, but I've now added one to cover it.

benjivm commented 5 years ago

Hmm, I am still having issues with this. The property works as expected on the Auth::user() class, however I cannot access $user->ldap when accessing it through the App\User class.

I am also no longer seeing the correct error messages for failed logins, what used to be the "password is required" message, for example, is now just showing validation.required.

stevebauman commented 5 years ago

Hi @benjivm,

The property works as expected on the Auth::user() class, however I cannot access $user->ldap when accessing it through the App\User class.

The property is only set on the currently authenticated users model. Calling a User::get() method on your User eloquent model won't populate the ldap property on all returned users.

If you'd like to query for users in your directory, call the root Adldap instance:

use Adldap\Laravel\Facades\Adldap;

$users = Adldap::search()->users()->get();

For further example:

// This works:
var_dump(Auth::user()->ldap); // Returns instance of Adldap\Models\User

// This will not:
$user = User::where('username', '=', 'jdoe')->first();

var_dump($user->ldap); // Returns NULL

For your second issue:

I am also no longer seeing the correct error messages for failed logins, what used to be the "password is required" message, for example, is now just showing validation.required.

This doesn't seem to be related to the above issue, it seems you may have modified your validation rules for your login request?

Let me know if you would like further explanation or if you still need any help, thanks!

benjivm commented 5 years ago

Ahh, thanks for clearing up the HasLdapUser trait for me.

The validation messages were busted because I accidentally deleted the resources\lang\en directory, whoops!

stevebauman commented 5 years ago

No problem @benjivm! :smile:

viniciusraupp commented 5 years ago

Laravel Version: 5.8.16 Adldap2-Laravel Version: 6.0.5 PHP Version: 7.2 LDAP Type: Active Directory

Hi, model binding it's not working in my project, am I doing something wrong?

Model User.php

use Adldap\Laravel\Traits\HasLdapUser;

class User extends Authenticatable
{
use HasLdapUser, [...];

Controller

use Illuminate\Support\Facades\Auth;

class ProfileController extends Controller
{
    public function index()
    {
        $profile = Auth::user();

        // dd($profile); WORK return only DB data
        dd($profile->ldap); // DON'T WORK - return null
    }
stevebauman commented 5 years ago

Hi @viniciusraupp,

Are you logging in with the ldap auth driver?

EDIT: Also:

viniciusraupp commented 5 years ago

Hi, thank you for your attention.

Config: auth.php

'providers' => [
        'users' => [
            'driver' => 'ldap',
            'model' => App\User::class,
        ],

Migration file: 2014_10_12_000000_create_users_table.php

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('objectguid')->nullable();
            $table->string('name');
            $table->string('samaccountname');
            $table->string('username')->unique();
            $table->string('userprincipalname');
            $table->string('email');
           [...]

config('ldap')

[
     "logging" => false,
     "connections" => [
       "default" => [
         "auto_connect" => true,
         "connection" => "Adldap\Connections\Ldap",
         "settings" => [
           "schema" => "App\Ldap\Schemas\LdapSchema",
           "account_prefix" => "",
           "account_suffix" => "",
           "hosts" => [
             "IP",
           ],
           "port" => "636",
           "timeout" => 5,
           "base_dn" => "dc=domain,dc=com",
           "username" => "DOMAIN\admin",
           "password" => "pwd",
           "follow_referrals" => false,
           "use_ssl" => true,
           "use_tls" => false,
           "custom_options" => [
             24582 => 0,
           ],
         ],
       ],
     ],
   ]

config('ldap_auth')

[
     "connection" => "default",
     "provider" => "Adldap\Laravel\Auth\DatabaseUserProvider",
     "model" => "App\User",
     "rules" => [
       "Adldap\Laravel\Validation\Rules\DenyTrashed",
     ],
     "scopes" => [],
     "identifiers" => [
       "ldap" => [
         "locate_users_by" => "samaccountname",
         "bind_users_by" => "distinguishedname",
       ],
       "database" => [
         "guid_column" => "objectguid",
         "username_column" => "username",
       ],
       "windows" => [
         "locate_users_by" => "samaccountname",
         "server_key" => "AUTH_USER",
       ],
     ],
     "passwords" => [
       "sync" => true,
       "column" => "password",
     ],
     "login_fallback" => false,
     "sync_attributes" => [
       "App\Handlers\LdapAttributeHandler",
     ],
     "logging" => [
       "enabled" => true,
       "events" => [
        ...
       ],
     ],
   ]

Own LDAP User model:

namespace App\Ldap\Models;

use Adldap\Models\User as Model;
use Illuminate\Foundation\Auth\Access\Authorizable;

class User extends Model
{

}

Own LDAP Schema:

namespace App\Ldap\Schemas;

use Adldap\Schemas\ActiveDirectory;
use App\Ldap\Models\User;

class LdapSchema extends ActiveDirectory
{
    public function userModel()
    {
        return User::class;
    }
}
Giddeon commented 4 years ago

Hello, had anyone solved this issue? I have the same problem and found no solution to it?

@stevebauman this issue is closed with some solution?