繁体   English   中英

Laravel 5忘记密码电子邮件模板条件

[英]Laravel 5 forgotten password email template condition

我正在使用带有Entrust的内置用户资料中的Laravel 5获取用户角色和权限。 我设置了两个角色,分别是管理员和用户。 基本上,我想做的是有两个不同的忘记密码的电子邮件模板-一个用于用户,一个用于管理员。 因此,当用户输入电子邮件地址以将重置链接通过电子邮件发送给他们时,我需要先检查他们是哪种类型的用户,然后再向他们发送正确的模板。 我不想在标准电子邮件模板中做任何骇人听闻的事情,它们一定是在控制器中执行此操作的一种方式或肯定的方式? 有人知道我会怎么做吗?

您可能会提示他们输入他们的电子邮件,当他们提交时,您可以在控制器中获取它:

public function forgot()
{
    $email = Input::get('email');
    $user = User::where('email', $email)->first();

    if($user->type == 'admin') {
        // logic to email admin with admin template
    } else {
        // logic to email user with user template 
    }

    // redirect to success page letting user know the email was sent
    return View::make('someview');
}

或者更好的是,只需将用户类型传递给处理电子邮件的电子邮件服务:

public function forgot()
{
   $email = Input::get('email');
   $user = User::where('email', $email)->first();

   $emailService->sendForgotForType($user->type);

   // redirect to success page letting user know the email was sent
   return View::make('someview');
}

如果您使用的是Laravel 5内置的用户管理:

要覆盖使用的默认模板,您需要通过编写扩展了PasswordBroker的新类来在PasswordBroker.php手动设置$emailView

例如,在config / app.php中注释掉'Illuminate\\Auth\\Passwords\\PasswordResetServiceProvider'

然后创建一个扩展类:

use Illuminate\Contracts\Auth\PasswordBroker;
use Illuminate\Contracts\Auth\CanResetPassword;

class MyPasswordBroker extends PasswordBroker {

    // override 
    public function emailResetLink(CanResetPasswordContract $user, $token, Closure $callback = null)
    {
        // Override Logic to email reset link function perhaps using the example above?
    }
}

然后,你就需要绑定你的新MyPasswordBrokerAppServiceProviderapp/Providers/AppServiceProvider.php寄存器方法(下面在网上找到):

$this->app->bind('App\Model\PasswordBroker', function($app) {
    $key = $app['config']['app.key'];
    $userToken = new \App\Model\NewUserToken; 
    $tokens = new \App\Repository\NewTokenRepository($key,$userToken);
    $user = new \App\Model\NewUser;
    $view = $app['config']['auth.password.email'];
    return new \App\Model\PasswordBroker($tokens, $users, $app['mailer'], $view);
});

如果您能处理的话,绝对是中等高级的东西-太好了。 否则,我可能会考虑使用带有所需内置功能的身份验证包。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM