Skip to content
This repository has been archived by the owner on Jul 24, 2023. It is now read-only.

Model binding is always null #692

Closed
benjivm opened this issue Mar 19, 2019 · 11 comments
Closed

Model binding is always null #692

benjivm opened this issue Mar 19, 2019 · 11 comments

Comments

@benjivm
Copy link

benjivm commented Mar 19, 2019

  • Laravel Version: 5.8.4
  • Adldap2-Laravel Version: 6.0
  • PHP Version: 7.3.2
  • LDAP Type: ActiveDirectory

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
Copy link
Member

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

@benjivm
Copy link
Author

benjivm commented Mar 19, 2019

Dang you're fast, thanks @stevebauman

stevebauman added a commit that referenced this issue Mar 19, 2019
@stevebauman
Copy link
Member

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
Copy link
Author

benjivm commented Mar 20, 2019

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
Copy link
Member

stevebauman commented Mar 21, 2019

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
Copy link
Author

benjivm commented Mar 21, 2019

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
Copy link
Member

No problem @benjivm! 😄

@viniciusraupp
Copy link

viniciusraupp commented May 14, 2019

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
Copy link
Member

stevebauman commented May 14, 2019

Hi @viniciusraupp,

Are you logging in with the ldap auth driver?

EDIT: Also:

  • Do you have auto_connect set to true
  • And, does this imported user have an objectguid in their respective database column?

@viniciusraupp
Copy link

viniciusraupp commented May 14, 2019

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
Copy link

Giddeon commented Dec 12, 2019

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?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants