重置密码不令牌Laravel 4.2(Reset password without token in

2019-10-22 08:56发布

我是新来的Laravel 4。

想知道如果我可以重置用户的密码,登录以管理员身份的时候。 在这种情况下,我并不需要一个令牌允许更改密码,当用户收到一封电子邮件,改变她的密码。 我'在ReminderController类postReset方法激励自己:

/**
 * Handle a POST request to reset a user's password.
 *
 * @return Response
 */
public function postReset()
{
    $credentials = Input::only(
        'email', 'password', 'password_confirmation', 'token'
    );

    $response = Password::reset($credentials, function ($user, $password) {
        $user->password = Hash::make($password);

        $user->save();

        Auth::login($user);
    });

    switch ($response) {
        case Password::INVALID_TOKEN:
            return Redirect::to('/login')->with('error', Lang::get($response));
        case Password::INVALID_PASSWORD:
        case Password::INVALID_USER:
            return Redirect::back()->with('error', Lang::get($response));

        case Password::PASSWORD_RESET:
            return Redirect::to('/')->with('message', Lang::get($response));
    }
}

但这种方法处理token string$credetials调用时可变Password::reset 。 贝娄是更新用户数据的方法。

public function update($colaborador)
{
      $credentials = Input::only(
        'nome_completo', 'email', 'password', 'password_confirmation', 'token'
    );
    $emailGestor = Input::get('email-gestor');
    $enviarEmail = Input::get('enviar-email');
    $user        = $colaborador->user;

    if (User::where('email', $email)->where('id', '!=', $user->id)->count() > 0) {
        $mensagem = 'O endereço de e-mail ' . $email . ' já está sendo utilizado.';
    } else  {
        $response = Password::reset($credentials, function ($credentials, $user, $password, $enviarEmail) {
            $user->nome_completo = $credentials['nome_completo'];
            $user->email = $credentials['email'];
            $user->password = Hash::make($password);

            $user->save();
            $mensagem = 'Colaborador alterado.';

            if ($enviarEmail == 1) {
                PrimeiroAcesso::remind(['email' => $email], function ($msg) {
                    $msg->subject('Assessment – Mapeamento de Competências Funcionais Natura');
                });
                $mensagem .= ' E-mail de primeiro acesso enviado.';
            }
        });

        switch ($response) {
            case Password::INVALID_TOKEN:
                $mensagem = 'Token inválido.'; break;
            case Password::INVALID_PASSWORD:
                $mensagem = 'Senha inválida.'; break;
            case Password::INVALID_USER:
                $mensagem = 'Nome de usuário inválido'; break;
            default: break;
        }
    }

    if ($emailGestor == '' && $colaborador->gestor) {
        $colaborador->gestor()->dissociate();
        $colaborador->save();
        $mensagem .= ' Gestor removido.';
    } else {
        $gestor = User::with('colaborador')->where('email', $emailGestor)->first();

        if ($gestor) {
            $colaborador->gestor()->associate($gestor->colaborador);
            $colaborador->save();
            $mensagem .= ' Gestor alterado para ' . $emailGestor . '.';
        }
    }

    return Redirect::route('admin.colaborador.index')->with('flash_message', $mensagem);
}

$credentials = Input::only(
            'nome_completo', 'email', 'password', 'password_confirmation', 'token'
        );

我得到token从视图的形式。

Answer 1:

reset这是在找到方法Illuminate\Auth\Reminders\PasswordBroker需要额外的token参数是凭证阵列的一部分,因为它需要用来删除相应的条目password_reminders表,如果复位成功。 因此,没有一个匹配的token该表中的项,您将无法使用该方法,因为你会得到一个INVALID_TOKEN响应。

话虽这么说,有2个选项的位置:

  1. 使用之前,您创建一个新的令牌Password::reset
  2. 手动更新指定用户的密码

我个人只使用第二个,因为它更容易和它跳过保存令牌到数据库的额外的步骤,只是将其删除后,密码重置,所有在同一请求。

东西,因为这应该做的那样简单(当然你也可以扩展,以满足您的需求indiviual):

// Get the request parameters
list($name, $email, $password, $passwordConfirmation) = Input::only('nome_completo', 'email', 'password', 'password_confirmation');

// Search for a user matching the email address
$user = User::where('email', $email)->first();

// Go ahead if a user matching that email was found
if ( ! is_null($user))
{
    // Check if the password and password confirmation match
    // NOTE: you can do additional validations here if needed
    if ($password == $passwordConfirmation)
    {
        $user->nome_completo = $name;
        $user->password = Hash::make($password);
        $user->save();
    }
}


文章来源: Reset password without token in Laravel 4.2