2017-04-06 107 views
0

如何在两个不同的ResetPasswordNotifications之间切换?Laravel 5.4在2个不同的ResetPassword通知之间切换

我已经得到了通常的重置密码,但是当一个新用户由管理员创建(在一个单独的控制器中)时,他们也会得到一个重置密码“like”通知。它将具有不同的内容,但通过提供带有所生成和存储的令牌的URL来实现相同的目的。

我可以看到如何做到这一点的唯一方法是:

  1. 创建我自己的PasswordBroker继承Laravel的PasswordBroker
  2. 覆盖PasswordBroker :: sendResetLink采取额外的PARAM
  3. 注册新PasswordResetServiceProvider中的PasswordBroker
  4. 然后,当它被调用时,它将采用用户名param(原始)和一个额外的参数来切换通知,如

    Password :: broker() - > sendResetLink($ username,$ myNewToggleParam);

这是最简单的方法来做到这一点,并保持为用户创建和存储令牌的重置密码功能。

回答

0

通过散步到PasswordBroker,然后到PasswordResetServiceProvider找到它在服务容器中实例化的位置,然后我可以做到这一点,以便将备用密码重置通知发送到刚刚由仪表板中的管理员创建的用户。

为了简便起见,我只放少量的公共端点,倒是在的情况下完整解决方案失去方面没有它:

/** 
* Store a newly created user in storage, and send a notification to 
* indicate the account was created and requires a password reset. 
* 
* @param \Illuminate\Http\Request $request 
* @return \Illuminate\Http\JsonResponse 
*/ 
public function store(Request $request) 
{ 
    $this->authorize('store', User::class); 

    $this->validate($request, [...]); 

    $user = $this->create($request); 

    $this->sendResetLinkEmail($user); 

    return response()->json([ 
     'user' => $user, 
     'message' => trans('user.created'), 
    ]); 
} 

/** 
* Send a reset link to the new user. 
* 
* @param User $user 
*/ 
private function sendResetLinkEmail(User $user) 
{ 
    // Reach into the service container for the password broker 
    $passwordBroker = App::make('auth.password.broker'); 

    // Create a new token from the token repository, and send of the email 
    // notification to the new user to reset their password 
    $user->sendNewUserPasswordResetNotication(
     $passwordBroker->getRepository()->create($user) 
    ); 
} 

不要忘记为新的通知添加到用户模型。