简体   繁体   English

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

[英]Laravel 5 forgotten password email template condition

I am using the Laravel 5 built in user stuff with Entrust for user roles and permissions. 我正在使用带有Entrust的内置用户资料中的Laravel 5获取用户角色和权限。 I have two roles set up which are administrators and users. 我设置了两个角色,分别是管理员和用户。 Basically what I want to do is have two different forgotten password email templates - one for users and one for administrators. 基本上,我想做的是有两个不同的忘记密码的电子邮件模板-一个用于用户,一个用于管理员。 So when a user enters their email address to get the reset link emailed to them I need to check what sort of user they are first and then send them the right template. 因此,当用户输入电子邮件地址以将重置链接通过电子邮件发送给他们时,我需要先检查他们是哪种类型的用户,然后再向他们发送正确的模板。 I don't want to have to do any sort of hacky stuff in the standard email template their must be a way to do this in the controller or something surely? 我不想在标准电子邮件模板中做任何骇人听闻的事情,它们一定是在控制器中执行此操作的一种方式或肯定的方式? Anyone know how I would do it? 有人知道我会怎么做吗?

You can probably prompt them to enter their email and when they submit you can grab it in the controller: 您可能会提示他们输入他们的电子邮件,当他们提交时,您可以在控制器中获取它:

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');
}

Or better yet, just pass the user type to an email service that handles the emailing: 或者更好的是,只需将用户类型传递给处理电子邮件的电子邮件服务:

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');
}

If you are using Laravel 5's built in User Management: 如果您使用的是Laravel 5内置的用户管理:

To override the default template used you would need to manually set the $emailView in the PasswordBroker.php by writing a new class that extends PasswordBroker . 要覆盖使用的默认模板,您需要通过编写扩展了PasswordBroker的新类来在PasswordBroker.php手动设置$emailView

For example, comment out 'Illuminate\\Auth\\Passwords\\PasswordResetServiceProvider' in config/app.php 例如,在config / app.php中注释掉'Illuminate\\Auth\\Passwords\\PasswordResetServiceProvider'

Then create an extension class: 然后创建一个扩展类:

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?
    }
}

Then you would need to bind your new MyPasswordBroker class to AppServiceProvider at app/Providers/AppServiceProvider.php in the register method (below found online): 然后,你就需要绑定你的新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);
});

Definitely moderately advanced stuff, if you can handle it - great. 如果您能处理的话,绝对是中等高级的东西-太好了。 Otherwise I would possibly look into using an authentication package with built in features you need. 否则,我可能会考虑使用带有所需内置功能的身份验证包。

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

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