Laravel Enloquent - 加密/解密呼叫数据 [英] Laravel Eloquent - Encrypting/Decrypt Data on call
问题描述
我可以使用 Crypt
加密/解密我的数据。我想加密我的数据库中的一些信息(例如名称,电子邮件,电话号码等)。
I can use Crypt
to encrypt/decrypt my data. I want to encrypt some information in my db (such as the name, email, phone number to name a few).
假设我想要一切加密,我希望能够在后台执行此操作,通过覆盖创建
和 save
函数可以执行此操作:
Assuming that I want EVERYTHING to be encrypted, I want to be able to do this in the background by itself, which I can perform by overwriting the create
and save
functions:
// For instance, the save() function could become
public function save(array $options = array())
{
foreach ($this->attributes as $key => $value)
{
if (isset($value)) $this->attributes[$key] = Crypt::encrypt($value);
}
return parent::save($options);
}
现在,我希望以相同的方式执行解密,所以当我说 User :: find($ id)
,返回的 $ user
已经被解密。还有其他功能,如 firstOrFail()
get()
first()
和all都可以正常工作。
Now, I want the decryption to be performed the same way, so that when I say User::find($id)
, the returned $user
is already decrypted. Also other functions such as firstOrFail()
get()
first()
and all to work as well.
当我使用关系时,我也希望扩展这个功能(所以 User :: with ('someOtherTable') - > find($ id)
也可以使用)。
I also would like this functionality to be extended when I use relationships (so User::with('someOtherTable')->find($id)
also work).
这可能吗?如果这不可能,我正在考虑创建一个帮助函数 decyrpt()
Would this be possible? If this is not possible, I am thinking of creating a helper function decyrpt()
function decrypt($array)
{
if (!is_array($array)) return Crypt::decrypt($array);
$result = [];
foreach($array as $key => $value) $result[$key] = decrypt($value);
return $result;
}
然后先将所有结果传递给我们,然后开始使用它们,但是如果Laravel会提供这个,或者如果有一个Laravel Way这样做,会更好。
And pass all my results through this first, and then start using them, but it would be nicer if Laravel would provide this, or if there was a "Laravel Way" of doing this.
推荐答案
真正有意义的是加密一切。例如,您不想加密主键;这甚至没有意义。同样,你可能不想加密日期字段;您将失去对它们执行任何类型的SQL查询的能力。
It doesn't really make sense to encrypt everything. For example, you never want to encrypt the primary key; that doesn't even make sense. Likewise you probably don't want to encrypt the date fields; you'll lose the ability to perform any sort of SQL query on them.
考虑到这一点,您可以尝试以下方式:
With that in mind, you can try something like this:
class BaseModel extends Eloquent {
protected $encrypt = [];
public function setAttribute($key, $value)
{
if (in_array($key, $this->encrypt))
{
$value = Crypt::encrypt($value);
}
return parent::setAttribute($key, $value);
}
public function getAttribute($key)
{
if (in_array($key, $this->encrypt))
{
return Crypt::decrypt($this->attributes[$key]);
}
return parent::getAttribute($key);
}
public function attributesToArray()
{
$attributes = parent::attributesToArray();
foreach ($attributes as $key => $value)
{
if (in_array($key, $this->encrypt))
{
$attributes[$key] = Crypt::decrypt($value);
}
}
return $attributes;
}
}
一个,并将 $ encrypt
属性设置为您为特定模型加密的任何列。
then have all you models extend this one, and set the $encrypt
property to whatever columns you want encrypted for that particular model.
PS 如果您想使用Eloquent的访问器功能,您将不得不再玩一点。
P.S. If you want to use Eloquent's accessor functionality, you'll have to play with this a bit more.
这篇关于Laravel Enloquent - 加密/解密呼叫数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!