在Laravel 10中如何在不获取当前密码的情况下更改密码?

huangapple go评论51阅读模式
英文:

how to change password in laravel 10 with out get current password?

问题

I have a problem with editing user information.
我遇到一个编辑用户信息的问题。

I want the password column to remain unchanged when no input is entered in the password field. Currently, when, for example, I only change the email, the password field is sent empty, and that empty field is saved as a hashed value in the password column.
我希望当密码字段未填写时,密码列保持不变。目前,例如,当我只更改电子邮件时,密码字段会以空值发送,并且该空字段将保存为密码列中的哈希值。

Is there a way to solve this problem?
有解决这个问题的方法吗?

Case 1: If possible, I don't want the password change to be separate from the other information, and I want all of these to work correctly in one form tag.
情况1:如果可能的话,我不想将密码更改与其他信息分开,并且我希望所有这些在一个表单标签中正确工作。

Case 2: The password should not be changed by taking the current password because the admin does not have access to the user's password.
情况2:密码不应通过使用当前密码来更改,因为管理员无法访问用户的密码。

英文:

I have a problem with editing user information.
I want the password column to remain unchanged when no input is entered in the password field. Currently, when, for example, I only change the email, the password field is sent empty, and that empty field is saved as a hashed value in the password column.
Is there a way to solve this problem?
Case 1: If possible, I don't want the password change to be separate from the other information, and I want all of these to work correctly in one form tag.
Case 2: The password should not be changed by taking the current password because the admin does not have access to the user's password.

The codes are as follows:

Blade Template:

<form id="userFRM" action="{{ isset($user->id) ? route('user.update') : route('auth.register') }}" method="post">
    @csrf

    @if (session('error_message'))
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            <i class="bi bi-check2 me-1"></i> {{ session('error_message') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close">
            </button>
        </div>
    @endif
    @if (isset($user->id))
        <input id="user_id" name="user_id" type="hidden" value="{{ $user->id }}">
    @endif

    <div class="mb-2 row">
        <div class="form-floating mb-2 col-4">
            <input type="text" name="username" class="form-control" id="username"
                value="{{ isset($user->id) ? $user->username : old('username') }}" placeholder="Username" />
            <label for="username">{{ trans('admin.username') }}</label>
            <x-input-error :messages="$errors->get('username')" class="mt-2" />
        </div>
        <div class="form-floating mb-2 col-4">
            <input type="text" name="first_name" class="form-control" id="firs_tname"
                value="{{ isset($user->id) ? $user->first_name : old('first_name') }}" placeholder="First Name" />
            <label for="firs_tname">{{ trans('auth.first_name') }}</label>

        </div>
        <div class="form-floating mb-2 col-4">
            <input type="text" name="last_name" class="form-control" id="last_name"
                value="{{ isset($user->id) ? $user->last_name : old('last_name') }}" placeholder="Last Name" />
            <label for="last_name">{{ trans('auth.last_name') }}</label>

        </div>
    </div>
    <div class="mb-2 row">
        <div class="form-floating mb-2 col-4">

            <input type="email" name="email" class="form-control" id="email"
                value="{{ isset($user->id) ? $user->email : old('email') }}" placeholder="email" />
            <label for="email">{{ trans('auth.email') }}</label>

            <x-input-error :messages="$errors->get('email')" class="mt-2" />

        </div>

        <div class="form-floating mb-2 col-4">

            <input type="password" name="password" class="form-control" id="password" placeholder="password" />
            <label for="password">{{ trans('auth.password') }}</label>
            <x-input-error :messages="$errors->get('password')" class="mt-2" />

        </div>

        <div class="form-floating mb-2 col-4">
            <input type="password" name="password_confirmation" class="form-control" id="password_confirmation"
                placeholder="password_confirmation" />
            <label for="password_confirmation">{{ trans('auth.confirm_password') }}</label>

        </div>
    </div>
    <div class="mb-2 row">
        <div class="form-floating mb-2 col-4">

            <select name="role" class="form-select " id="role">
                @foreach ($roles as $role)
                    @if (isset($user->id))
                        <option value="{{ $role->id }}" {{ $role->id == $user->role_id ? 'selected' : '' }}>
                            {{ $role->title }}</option>
                    @else
                        <option value="{{ $role->id }}">{{ $role->title }}</option>
                    @endif
                @endforeach
            </select>
            <label for="role">{{ trans('auth.user_role') }}</label>

        </div>
        <div class="my-5 col-4">

            @if (isset($user->id))
                <input type="checkbox" name="is_admin" class="form-check-input" id="is_admin"
                    {{ $user->is_admin == true ? 'checked' : '' }} />
                <label for="is_admin" class="form-check-label">{{ trans('auth.is_admin') }}</label>
                <input type="checkbox" class="form-check-input ms-5" name="verify_email" id="verify_email"
                    {{ isset($user->email_verified_at) ? 'checked' : '' }} />
                <label for="verify_email" class="form-check-label">{{ trans('auth.verify_email') }}</label>
            @else
                <input type="checkbox" name="is_admin" class="form-check-input" id="is_admin" />
                <label for="is_admin" class="form-check-label">{{ trans('auth.is_admin') }}</label>
                <input type="checkbox" class="form-check-input ms-5" name="verify_email" id="verify_email" />
                <label for="verify_email" class="form-check-label">{{ trans('auth.verify_email') }}</label>
            @endif


        </div>
        <div class="my-5">


            @if (isset($user->id))

                @if (\App\Helper\ClaimsHelper::hasAccess(\App\Helper\ClaimsHelper::EDIT))
                    <x-primary-button>
                        {{ trans('users.edit') }}
                    </x-primary-button>
                @endif
            @else
                @if (\App\Helper\ClaimsHelper::hasAccess(\App\Helper\ClaimsHelper::ADD))
                    <x-primary-button>
                        {{ trans('auth.sign_up') }}
                    </x-primary-button>
                @endif

            @endif
        </div>
</form>

UI Form

在Laravel 10中如何在不获取当前密码的情况下更改密码?

Controller

public function update(UserUpdatRequest $request)
    {
        $data = $request->all();

        $updated = $this->userRepository->update($data['user_id'], [
            'username' => $data['username'],
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'is_admin' => ($request->is_admin == 'on') ? 1 : 0,
            'role_id' => $data['role'],
            'email_verified_at' => isset($request->verify_email) ? Carbon::now() : '',
        ]);
        
        if (!$updated) {
            return ControllerHelper::updateError();
        }
        return \redirect()->route('users.list');
    }

Requset Validation

 public function rules(): array
    {
        return [
            'user_id' => 'required|exists:'.User::class.',id',
            'username' => 'required|string| max:255|unique:users,username,'.\request()->user_id,
            'first_name' => 'required|string| min:3| max:255',
            'last_name' => 'required|string| min:3| max:255',
            'email' => 'required|string| email| max:255| unique:' . User::class . ',email,'.\request()->user_id,
            'password' => 'confirmed|max:32',
            'role' => 'required|integer|exists:' . Role::class . ',id',
        ];
    }

答案1

得分: 2

你需要有条件地构建你的更新载荷。

$updates = [
    'username' => $data['username'],
    'first_name' => $data['first_name'],
    'last_name' => $data['last_name'],
    'email' => $data['email'],
    'is_admin' => ($request->is_admin == 'on') ? 1 : 0,
    'role_id' => $data['role'],
    'email_verified_at' => isset($request->verify_email) ? Carbon::now() : '',
];

if ($request->input('password')) {
    $updates['password'] = Hash::make($request->input('password'));
}

$updated = $this->userRepository->update($data['user_id'], $updates);
英文:

You'll want to construct your update payload conditionally.

$updates = [
'username' => $data['username'],
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'is_admin' => ($request->is_admin == 'on') ? 1 : 0,
'role_id' => $data['role'],
'email_verified_at' => isset($request->verify_email) ? Carbon::now() : '',
];
if($request->input('password')) {
$updates['password'] = Hash::make($request->input('password'));
}
$updated = $this->userRepository->update($data['user_id'], $updates);

huangapple
  • 本文由 发表于 2023年5月10日 21:01:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218743.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定