首页 > 解决方案 > 在 Laravel 中,无法重置加密的用户密码

问题描述

我想加密用户表中的电子邮件地址以保护个人信息。我试过这种方式

应用 Encryptable.php

<?php
namespace App;
use Crypt;
trait Encryptable{
    public function getAttribute($key){
        $value = parent::getAttribute($key);
        if (in_array($key, $this->encryptable)) {$value = Crypt::decrypt($value);return $value;}
        return $value;
    }
    public function setAttribute($key, $value){
        if (in_array($key, $this->encryptable)) {$value = Crypt::encrypt($value);}
        return parent::setAttribute($key, $value);
    }
}

应用\用户.php

<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail as MustVerifyEmailContract;
use Illuminate\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use App\Encryptable;
class User extends Authenticatable implements MustVerifyEmailContract{
    use MustVerifyEmail, Notifiable;
    use Encryptable;
    protected $fillable = ['name', 'email', 'password',];
    protected $hidden = ['password', 'remember_token',];
    protected $casts = [email_verified_at' => 'datetime',];
    public $encryptable = [email',];
}

应用程序/encryptable.php

<?php
namespace App;
use crypt;
trait Encryptable {
    public function getAttribute($key){
        $value = parent::getAttribute($key);
        if (in_array($key, $this->encryptable)) {$value = Crypt::decrypt($value);return $value;}
        return $value;
    }
    public function setAttribute($key, $value){
        if (in_array($key, $this->encryptable)) {$value = Crypt::encrypt($value);}
        return parent::setAttribute($key, $value);
    }
}

我可以加密电子邮件地址。但我无法登录和重置密码。用户可以在同一个电子邮件地址中创建多个帐户。这是一个非常糟糕的错误。

帮我!!

不同的帐户发送相同的问题,因为我无法登录。我试图重置密码,但失败了。对不起。

标签: phplaravelencryption

解决方案


推荐阅读