如何在laravel 5.6中为具有不同表的管理员重置密码?

时间:2019-01-22 03:10:50

标签: laravel laravel-5.6

我已经安装了新的laravel,并复制了所有登录,注册和密码视图以及用于管理员登录的控制器,并创建了名为“ admins”的表。当我尝试为laravel默认用户重置密码时,它可以工作,但对于“管理员”用户,它不会更新密码。重置默认用户密码后,密码重置表也为空。

// providers in config/auth.php   

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
    'admins' => [
        'driver' => 'eloquent',
        'model' => App\Dashboard\Admin::class,
    ],
],

// passwords reset in config/auth.php 

'passwords' => [
    'users' => [
        'provider' => 'users',
        'table' => 'password_resets',
        'expire' => 60,
    ],
    'admins' => [
        'provider' => 'admins',
        'table' => 'admin_password_resets',
        'expire' => 60,
    ],
],

//Also added broker and guard in ResetPasswordController.php


public function broker()
{
    return Password::broker('admins');
}

protected function guard()
{
    return Auth::guard('admin');
}

我只想更新管理员密码,并在重置密码后填写users_reset_table。我该如何实现?

4 个答案:

答案 0 :(得分:2)

步骤1:更新auth.php // config / auth.php中的提供程序

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
        'hash' => false,
    ],

    'admin' => [
        'driver' => 'session',
        'provider' => 'admins',
    ]
],


'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],

    'admins' => [
        'driver' => 'eloquent',
        'model' => App\models\Admin::class,
    ]
],
'passwords' => [
    'users' => [
        'provider' => 'users',
        'table' => 'password_resets',
        'expire' => 60,
        'throttle' => 60,
    ],
    'admins' => [
        'provider' => 'admins',
        'table' => 'password_resets',
        'expire' => 60,
        'throttle' => 60,
    ],
],

设置2: 在ForgotPasswordController.php中添加两个功能

/**
 * Get the broker to be used during password reset.
 *
 * @return PasswordBroker
 */
protected function broker()
{
    return Password::broker('admins');
}

/**
 * Display the form to request a password reset link.
 *
 * @return \Illuminate\View\View
 */
public function showLinkRequestForm()
{
    return view('auth.passwords.email')->with('user_type', request()->user_type);
}

设置3: 在ResetPasswordController.php中添加两个功能

/**
 * Get the broker to be used during password reset.
 *
 * @return PasswordBroker
 */
protected function broker()
{
    return Password::broker('admins');
}


/**
 * Display the password reset view for the given token.
 *
 * If no token is present, display the link request form.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  string|null  $token
 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
 */
public function showResetForm(Request $request, $token = null)
{
    return view('auth.passwords.reset')->with(
        ['token' => $token, 'email' => $request->email, 'user_type' => $request->user_type]
    );
}

设置4: //在刀片模板上发送用户类型 //admin-show.blade.php,我要在其中重设密码。

<button class="btn btn-primary btn-sm" onclick="event.preventDefault();
        document.getElementById('change-password-form').submit();">
 <span class="fa-passwd-reset">
  <i class="fa fa-lock"></i>
 </span> </i> Change Password
</button>

<form id="change-password-form" method="POST"
      action="{{ route('password.email', ['user_type' => 'admin']) }}">
      @csrf
      <input type="hidden" name="email" value="{{ $admin->email }}">

Setp 5: //现在在views / auth / passwords /内打开email.blade.php和reset.blade.php,并添加一个隐藏的输入字段,像这样

 <input type="hidden" name="user_type" value="{{ $user_type }}" required>

来源:https://medium.com/backenders-club/password-brokers-reset-passwords-on-multiple-tables-in-laravel-73068542925c

答案 1 :(得分:0)

您是否已在App \ Dashboard \ Admin模型中添加了表?

protected $table = 'admins';

答案 2 :(得分:0)

据我所知,您需要覆盖driver.findElement

中的某些方法

通过文件的外观,我感觉这两种方法应该被覆盖。

    public void static setComplaintDate(WebDriver driver) {
        driver.findElement(By.cssSelector("[data-selector=date-received-complaint]")).sendKeys(LocalDate.now().toString());
    }

答案 3 :(得分:0)

特别是重置管理员表的密码

Laravel包含Auth\ForgotPasswordControllerAuth\ResetPasswordController类,这些类包含通过电子邮件发送密码重置链接和重置用户密码所必需的逻辑。可以使用laravel/ui Composer软件包来生成执行密码重置所需的所有路由:

运行以下命令:

php artisan make:notification ResetPasswordNotification

composer require laravel/ui

php artisan ui vue --auth

auth复制resources\views文件夹并命名为admin

步骤1:自定义config / auth.php

<?php

return [

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],

    

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'jwt',
            'provider' => 'users',
            'hash' => false,
        ],

    // Admin guards

        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],

        'admin-api' => [
            'driver' => 'token',
            'provider' => 'admins',
        ],
    ],


    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],

        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class,
        ],
    ],


    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],

        'admins' => [
            'provider' => 'admins',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],
    ],


    'password_timeout' => 10800,

];

设置2: App\Http\Controllers\Admin文件夹中的ForgotPasswordController.php

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
use Illuminate\Support\Facades\Password;

class ForgotPasswordController extends Controller
{
 
    use SendsPasswordResetEmails;

    public function showLinkRequestForm()
    {
        return view('admin.passwords.email');
    }
    public function __construct()
    {
        $this->middleware('guest:admin');
    }
    public function broker()
    {
        return Password::broker('admins');
    }
}

并且 App\Http\Controllers\Admin文件夹中的ResetPasswordController.php

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Password;

class ResetPasswordController extends Controller
{
    use ResetsPasswords;

    protected $redirectTo = 'admin/dashboard';

  
    public function showResetForm(Request $request, $token = null)
    {
        return view('admin.passwords.reset')->with(
            ['token' => $token, 'email' => $request->email]
        );
    }

    public function __construct()
    {
        $this->middleware('guest:admin');
    }

    public function broker()
    {
        return Password::broker('admins');
    }
}

设置3: App文件夹中的Admin.php模型

重置电子邮件自定义

您可以轻松地修改用于将密码重置链接发送给用户的通知类。要开始使用,请在用户模型上覆盖sendPasswordResetNotification方法。在这种方法中,您可以使用您选择的任何通知类别发送通知。密码重置$token是该方法接收的第一个参数:

<?php

namespace App;

use App\Notifications\ResetPasswordNotification;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class Admin extends Authenticatable
{
    use Notifiable;

    protected $fillable = [
        'name', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
    public function sendPasswordResetNotification($token)
    {
        $this->notify(new ResetPasswordNotification($token));
    }
}

设置4: routs文件夹中的web.php路由文件

Route::get('admin-login','Admin\LoginController@showLoginForm')->name('admin.login');
    Route::post('admin-password/email','Admin\ForgotPasswordController@sendResetLinkEmail')->name('admin.password.email');
    Route::get('admin-password/reset','Admin\ForgotPasswordController@showLinkRequestForm')->name('admin.password.request');
    Route::post('admin-password/reset','Admin\ResetPasswordController@reset')->name('admin.password.update');
    Route::get('admin-password/reset/{token}','Admin\ResetPasswordController@showResetForm')->name('admin.password.reset');

Setp 5:resources\views\admin\passwords文件夹中的email.blade.php文件

<form method="POST" action="{{ route('admin.password.email') }}">

中的resources\views\admin\passwords文件夹中的reset.blade.php文件

<form method="POST" action="{{ route('admin.password.update') }}">

resources\views\admin文件夹中的login.blade.php文件

<form method="POST" action="{{ route('admin.login') }}">

Setp 6:app\Notifications文件夹中的ResetPasswordNotification.php文件 <?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Lang;

class ResetPasswordNotification extends Notification
{
    use Queueable;
    public $token;
    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($token)
    {
        $this->token = $token;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->subject(Lang::get('Reset Password Notification'))
            ->line(Lang::get('You are receiving this email because we received a password reset request for your account.'))
            ->action(Lang::get('Reset Password'), url(config('app.url').route('admin.password.reset', ['token' => $this->token, 'email' => $notifiable->getEmailForPasswordReset()], false)))
            ->line(Lang::get('This password reset link will expire in :count minutes.', ['count' => config('auth.passwords.'.config('auth.defaults.passwords').'.expire')]))
            ->line(Lang::get('If you did not request a password reset, no further action is required.'));
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}