Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 在用户列表页面的“操作”列中,添加“更改密码”链接及其工作过程_Laravel_Backpack For Laravel_Laravel Backpack - Fatal编程技术网

Laravel 在用户列表页面的“操作”列中,添加“更改密码”链接及其工作过程

Laravel 在用户列表页面的“操作”列中,添加“更改密码”链接及其工作过程,laravel,backpack-for-laravel,laravel-backpack,Laravel,Backpack For Laravel,Laravel Backpack,我正在使用laravel双肩包4.0。想要在“操作”列的“编辑”等用户列表中添加一个带有页面和所有验证功能的密码更改链接。想要另一个页面,在该页面中,当软件包的内置用户crud允许您更新密码以及所有其他用户字段时,您只能更新密码,这似乎有点奇怪。也就是说,假设您有自己的理由(并且我正确理解了用法),一种方法是使用建议的CRUD控制器,然后为用户模型制作第二个CRUD控制器,该控制器只支持“更新”操作,并且只允许编辑密码 注 这是未经测试的,因此可能有一些小问题需要解决,但方法是可靠的 安装并配置

我正在使用laravel双肩包4.0。想要在“操作”列的“编辑”等用户列表中添加一个带有页面和所有验证功能的密码更改链接。

想要另一个页面,在该页面中,当软件包的内置用户crud允许您更新密码以及所有其他用户字段时,您只能更新密码,这似乎有点奇怪。也就是说,假设您有自己的理由(并且我正确理解了用法),一种方法是使用建议的CRUD控制器,然后为用户模型制作第二个CRUD控制器,该控制器只支持“更新”操作,并且只允许编辑密码

注 这是未经测试的,因此可能有一些小问题需要解决,但方法是可靠的

安装并配置。然后,为用户创建第二个控制器,但进行编辑,以便仅允许“更新”操作,并且仅可编辑密码和密码确认字段。我们将使名称和电子邮件只读,以便您可以查看其收件人,但无法编辑这些字段。如果需要,可以隐藏这些字段,也可以删除它们,但是如果删除它们,请注意,需要创建自定义请求类并更新规则,以便在提交时不需要这些字段

<?php

namespace App\Http\Controllers;

use Backpack\CRUD\app\Http\Controllers\CrudController;
use Backpack\CRUD\app\Http\Requests\CrudRequest;
use EduardoArandaH\UserManager\app\Http\Requests\UserStoreCrudRequest as StoreRequest;
use EduardoArandaH\UserManager\app\Http\Requests\UserUpdateCrudRequest as UpdateRequest;

class UserPasswordCrudController extends CrudController
{
    use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation { update as traitUpdate; }

    public function setup()
    {
        $this->crud->setModel(config('backpack.permissionmanager.models.user'));
        $this->crud->setEntityNameStrings('User Password', 'User Passwords');
        $this->crud->setRoute(backpack_url('userPasswords'));
        $this->crud->denyAccess('create');
        $this->crud->denyAccess('list');
        $this->crud->denyAccess('delete');
        $this->crud->denyAccess('reorder');
        $this->crud->denyAccess('revisions');
    }

    public function setupUpdateOperation()
    {
        $this->addUserFields();
        $this->crud->setValidation(UpdateRequest::class);
    }

    /**
     * Update the specified resource in the database.
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update()
    {
        $this->crud->setRequest($this->crud->validateRequest());
        $this->crud->setRequest($this->handlePasswordInput($this->crud->getRequest()));
        $this->crud->unsetValidation(); // validation has already been run
        return $this->traitUpdate();
    }

    /**
     * Handle password input fields.
     */
    protected function handlePasswordInput($request)
    {
        // Remove fields not present on the user.
        $request->request->remove('password_confirmation');
        $request->request->remove('roles_show');
        $request->request->remove('permissions_show');

        // Encrypt password if specified.
        if ($request->input('password')) {
            $request->request->set('password', Hash::make($request->input('password')));
        } else {
            $request->request->remove('password');
        }

        return $request;
    }

    protected function addUserFields()
    {
        $this->crud->addFields([
            [
                'name'  => 'name',
                'label' => trans('backpack::permissionmanager.name'),
                'type'  => 'text',
                'attributes' => ['readonly' => 'readonly'],
            ],
            [
                'name'  => 'email',
                'label' => trans('backpack::permissionmanager.email'),
                'type'  => 'email',
                'attributes' => ['readonly' => 'readonly'],
            ],
            [
                'name'  => 'password',
                'label' => trans('backpack::permissionmanager.password'),
                'type'  => 'password',
            ],
            [
                'name'  => 'password_confirmation',
                'label' => trans('backpack::permissionmanager.password_confirmation'),
                'type'  => 'password',
            ],
        ]);
    }
}
最后,在您的普通用户crud控制器(或您想要按钮的任何控制器)中,在控制器的setupListOperation方法中将按钮添加到行堆栈中:

public function setupListOperation()
{
    $this->crud->addButtonFromView('line', 'update_password', 'view', 'end');
    
    // ... normal setup code
}

嗨@HMInt。我不确定我是否完全理解这个用例,也许你可以更详细地解释一下?您好,@WesleySmith在我的项目中有一个用户管理模块,作为管理员,我想更改列表页面中所有用户的密码。我该怎么做?嗯,你是想一次更改他们所有的密码,还是单独更改?如果单独使用,您是否见过这个插件,它添加了内置密码更改功能的用户CRUD?是的。在插件中,密码字段出现在编辑链接上,这是我不想要的方式。我想要的是一个修改密码链接后编辑链接,这将打开一个页面,只有2个字段的密码和确认密码,点击提交后,它将显示通知密码已更改,就是这样。
@if ($crud->hasAccess('update'))
    <!-- Single edit button -->
    <a href="{{ url(config('backpack.base.route_prefix').'/userPasswords/'.$entry->getKey().'/edit') }}" class="btn btn-sm btn-link"><i class="la la-edit"></i>Edit Password</a>
@endif
public function setupListOperation()
{
    $this->crud->addButtonFromView('line', 'update_password', 'view', 'end');
    
    // ... normal setup code
}