在使用忘记密码发送密码重置电子邮件之前,检查是否已激活用户

时间:2016-12-21 18:36:52

标签: laravel laravel-5.3

我正在使用@Requestmapping(value="/createperson",method=method.post) public @Responsebody String createperson(@Requestbody person,@Requestbody company) { //Some code to save } 创建一个小应用。我在Laravel的默认Laravel 5.3上应用了用户激活(通过电子邮件确认)。但如果帐户/用户未通过验证电子邮件地址激活,我找不到停止发送密码重置链接的方法。目前,如果用户创建帐户并且未验证他/她可以使用密码重置链接登录的电子邮件地址。

这就是我在用户表中的内容

Auth

更新 我尝试通过更新以下功能来实现。但它不起作用

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name')->nullable();;
        $table->string('username')->unique();
        $table->string('email')->unique();
        $table->string('company')->nullable();;
        $table->string('password');
        $table->boolean('activated')->default(false);
        $table->rememberToken();
        $table->timestamps();
    });

    Schema::create('user_activations', function (Blueprint $table) {
        $table->integer('user_id')->unsigned();
        $table->string('token')->index();
        $table->timestamp('created_at');
    });
}

3 个答案:

答案 0 :(得分:4)

我找到了解决方案。以防万一有人在寻找相同的解决方案。这是我重写的功能

public function sendResetLinkEmail(Request $request)
{
    $this->validate($request, ['email' => 'required|email']);
    $user_check = User::where('email', $request->email)->first();

    if (!$user_check->activated) {
        return back()->with('status', 'Your account is not activated. Please activate it first.');
    } else {
        $response = $this->broker()->sendResetLink(
            $request->only('email')
        );

        if ($response === Password::RESET_LINK_SENT) {
            return back()->with('status', trans($response));
        }

        return back()->withErrors(
            ['email' => trans($response)]
        );
    }
} 

答案 1 :(得分:0)

另一种直接的方法是创建一个新的验证规则,以检查用户帐户是否已激活,然后将该规则添加到validateEmail内部的ForgotPasswordController方法中。只要确保您在每次停用用户时都删除密码令牌即可。

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use App\User;

class ActiveUser implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        $user = User::whereEmail($value)->first();
        if($user) {
            if($user->active) {
                return true;
            }
            return false;
        }
        return true;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'Your account is deactivated.';
    }
}

在ForgotPasswordController中

/**
 * Validate the email for the given request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return void
 */
protected function validateEmail(Request $request)
{
    $this->validate($request, ['email' => [ 'required', 'email', new ActiveUser ]]);
}

这是在用户停用时删除令牌的方法。

$user = \App\user::find(1);
\Illuminate\Support\Facades\Password::broker()->deleteToken($user);

答案 2 :(得分:0)

另一种解决方案是覆盖用户模型中的sendPasswordResetNotification

/**
 * OVERWRITE ORIGINAL
 * @param string $token
 */
public function sendPasswordResetNotification($token) {
  if(!$this->active){
    session()->flash('error', 'Your account is disabled.');
    return back();
  }

  $this->notify(new \Illuminate\Auth\Notifications\ResetPassword($token));
}

如果未激活该用户,则不会再收到另一封电子邮件。相反,他将返回到登录页面,并在会话中显示错误消息。要显示它,您需要在刀片文件中添加以下内容:

@if ($errors->any())
@foreach ($errors->all() as $message)
  <div class="alert alert-danger-plain">
    <i class="icon-exclamation"></i> {{ $message }}
  </div>
@endforeach
@endif