LdapRecord: [Bug] password not encoded if using custom model

Environment:

  • LDAP Server Type: ActiveDirectory
  • PHP Version:8.0

Describe the bug:

For some reason $user->unicodepwd is not automatically encoded IF I’m using custom model.

use LdapRecord\Models\Model;

class User extends Model
{
    /**
     * The object classes of the LDAP model.
     *
     * @var array
     */
    public static $objectClasses = [
        'top',
        'person',
        'organizationalperson',
        'user',
    ];

    protected $casts = [
        'accountexpires' => 'datetime:windows-int',
    ];
}
 $user = (new User());
$user->unicodepwd = $request->input('password');
dd($user->getModifications());
array:3 [
    "attrib" => "unicodepwd"
    "modtype" => 1
    "values" => array:1 [
      0 => "testpassword"
    ]

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (9 by maintainers)

Most upvoted comments

Hi @vatsake,

I don’t believe you can set a password during creation of an ActiveDirectory account – it must be set after the account exists. Can you confirm if setting the password using your own model actually creates the user with the password you’ve provided?

Also, can you try re-extending the base User model and attempt setting the password after creation?

I.e.

$user = new User();

// ...

$user->save();

$user->update([
    'unicodepwd' => $request->input('password'),
    'userAccountControl' => 512,
    'pwdlastset' => 0,
]); 

Hi @vatsake,

If you create your own custom User Active Directory model, you should either extend the built-in ActiveDirectory\User model, or implement the interface and traits that currently exist on that model, namely the HasPassword trait:

https://github.com/DirectoryTree/LdapRecord/blob/8ad10c26a992b46a265ff84b2d1845003c910a6e/src/Models/ActiveDirectory/User.php#L14-L32

Custom User Model:

use LdapRecord\Models\Model;
use LdapRecord\Models\Concerns\HasPassword;
use LdapRecord\Models\Types\ActiveDirectory;

class User extends Model implements ActiveDirectory
{
    use HasPassword;

    protected $passwordHashMethod = 'encode';

    protected $passwordAttribute = 'unicodepwd';

    // ...
}

I’d recommend extending the default User model if you can, as it provides a lot of scaffolding out-of-the-box:

Extending Default User Model:

use LdapRecord\Models\ActiveDirectory\User as BaseModel;

class User extends BaseModel
{
    // ...
}

This will resolve your password encoding issue 👍