如何使用Adldap2-laravel软件包更改AD用户密码?

时间:2019-10-30 12:15:33

标签: php laravel active-directory openldap adldap

我想在AD中更改用户密码,因为AD中没有密码属性。

当前正在运行带有Adldap2-laravel软件包的laravel框架,以管理ADLDAP操作。

这是我的ldap_auth.php

<?php

return [
    'connection' => env('LDAP_CONNECTION', 'default'),
    'provider' => Adldap\Laravel\Auth\DatabaseUserProvider::class,
    'model' => App\User::class,
    'rules' => [
        Adldap\Laravel\Validation\Rules\DenyTrashed::class,
    ],
    'scopes' => [
        Adldap\Laravel\Scopes\UidScope::class
    ],
    'identifiers' => [
        'ldap' => [
            'locate_users_by' => 'uid',
            'bind_users_by' => 'dn',
        ],
        'database' => [
            'guid_column' => 'objectguid',
            'username_column' => 'username',
        ],
        'windows' => [
            'locate_users_by' => 'samaccountname',
            'server_key' => 'AUTH_USER',
        ],
    ],
    'passwords' => [
        'sync' => env('LDAP_PASSWORD_SYNC', false),
        'column' => 'password',
    ],
    'login_fallback' => env('LDAP_LOGIN_FALLBACK', false),
    'sync_attributes' => [
        'username' => 'uid',
        'password' => 'userPassword',
        'name' => 'cn',
        'role' => 'l',
        'category' => 'businessCategory',
        'telephone_number' => 'telephoneNumber',
        'email' => 'mail'

    ],
    'logging' => [

        'enabled' => env('LDAP_LOGGING', true),

        'events' => [

            \Adldap\Laravel\Events\Importing::class                 => \Adldap\Laravel\Listeners\LogImport::class,
            \Adldap\Laravel\Events\Synchronized::class              => \Adldap\Laravel\Listeners\LogSynchronized::class,
            \Adldap\Laravel\Events\Synchronizing::class             => \Adldap\Laravel\Listeners\LogSynchronizing::class,
            \Adldap\Laravel\Events\Authenticated::class             => \Adldap\Laravel\Listeners\LogAuthenticated::class,
            \Adldap\Laravel\Events\Authenticating::class            => \Adldap\Laravel\Listeners\LogAuthentication::class,
            \Adldap\Laravel\Events\AuthenticationFailed::class      => \Adldap\Laravel\Listeners\LogAuthenticationFailure::class,
            \Adldap\Laravel\Events\AuthenticationRejected::class    => \Adldap\Laravel\Listeners\LogAuthenticationRejection::class,
            \Adldap\Laravel\Events\AuthenticationSuccessful::class  => \Adldap\Laravel\Listeners\LogAuthenticationSuccess::class,
            \Adldap\Laravel\Events\DiscoveredWithCredentials::class => \Adldap\Laravel\Listeners\LogDiscovery::class,
            \Adldap\Laravel\Events\AuthenticatedWithWindows::class  => \Adldap\Laravel\Listeners\LogWindowsAuth::class,
            \Adldap\Laravel\Events\AuthenticatedModelTrashed::class => \Adldap\Laravel\Listeners\LogTrashedModel::class,

        ],
    ],

];

这是我的LdapController.php,其中包含用于重置密码的功能

public function resetPassword(Request $req)
    {
        $req->validate([
            'userid' => 'required',
            'password' => 'required|min:6|confirmed'
        ]);
        $userLdap = Adldap::search()->where('uid', $req->userid)->firstOrFail();
        $newPassword = "{SHA}" . base64_encode(pack("H*", sha1($req->password)));
        $res = $userLdap->update([
            'userpassword' => $newPassword
        ]);

        //Force change AD Password
        // $adPassword = str_replace("\n", "", shell_exec("echo -n '\"" . $req->password . "\"' | recode latin1..utf-16le/base64"));
        // $provider = Adldap\Models\User::connect('ad');
        // $dn = $provider->search()->where('cn', $req->userid)->get();
        // $res = $dn->setPassword($adPassword);

        if ($res) {
            return back()->withSuccess('<strong>Success!</strong> Your password has been changed');
        } else {
            return back()->withErrors('<strong>Failed!</strong> Your password was unable to changed');
        }
    }

不幸的是,$ res = $ dn-> setPassword($ adPassword);返回错误“方法Adldap \ Query \ Collection :: setPassword不存在。”

1 个答案:

答案 0 :(得分:0)

我在Google搜索“ Adldap2-laravel更改密码”时发现了一个示例here

$user = Adldap::users()->find('jdoe');

if ($user instanceof Adldap\Models\User) {
    $oldPassword = 'password123';

    $newPassword = 'correcthorsebatterystaple';

    $user->changePassword($oldPassword, $newPassword);
}

如果您想重设密码,似乎应该可以:

$user->setPassword("correcthorsebatterystaple");
$user->save();

如果您想知道下面发生了什么,或者不使用Adldap2-laravel怎么做:

该属性为unicodePwd。您可以“更改”密码,也可以“重置”密码。

更改密码需要知道旧密码。这就是用户自己做的事情。

重置密码需要对该帐户具有“重置密码”权限,该权限通常授予管理帐户。

documentation for unicodePwd告诉您如何执行这两个操作。对于“更改”,您将在同一请求中发送带有旧密码的删除指令和带有新密码的添加指令。

要进行重置,您将发送一条替换指令。

在两种情况下,都必须以特定格式发送密码。

“ ldap_modify_batch” shows an example of how to change a password的PHP文档。

ldap_mod_replacethere is a comment的文档页面上,该页面显示了如何重设密码。