This repository was archived by the owner on Apr 7, 2021. It is now read-only.
File tree 3 files changed +50
-5
lines changed
3 files changed +50
-5
lines changed Original file line number Diff line number Diff line change @@ -125,7 +125,19 @@ protected function shouldEncrypt($key): bool
125
125
{
126
126
$ encrypt = DatabaseEncryption::isEnabled () && isset ($ this ->encrypted ) && is_array ($ this ->encrypted ) ? $ this ->encrypted : [];
127
127
128
- return in_array ($ key , $ encrypt );
128
+ return in_array ($ key , $ encrypt , true );
129
+ }
130
+
131
+ /**
132
+ * Determine whether a model is ready for encryption.
133
+ *
134
+ * @return bool
135
+ */
136
+ protected function isEncryptable (): bool
137
+ {
138
+ $ exists = property_exists ($ this , 'exists ' );
139
+
140
+ return $ exists === false || ($ exists === true && $ this ->exists === true );
129
141
}
130
142
131
143
/**
@@ -318,6 +330,6 @@ protected function getArrayableAttributes()
318
330
*/
319
331
public function getAttributes ()
320
332
{
321
- return $ this ->exists ? $ this ->doDecryptAttributes (parent ::getAttributes ()) : parent ::getAttributes ();
333
+ return $ this ->isEncryptable () ? $ this ->doDecryptAttributes (parent ::getAttributes ()) : parent ::getAttributes ();
322
334
}
323
335
}
Original file line number Diff line number Diff line change @@ -110,4 +110,26 @@ public function testGetArrayableAttributes()
110
110
$ this ->assertEquals ($ strings ['shouldnt_be_encrypted ' ], $ attributes ['shouldnt_be_encrypted ' ]);
111
111
$ this ->assertEquals ($ strings ['should_be_encrypted ' ], $ attributes ['should_be_encrypted ' ]);
112
112
}
113
+
114
+ public function testIsEncryptableExists ()
115
+ {
116
+ $ strings = $ this ->randomValues ();
117
+ $ model = DatabaseModel::create ($ strings );
118
+
119
+ $ this ->assertTrue ($ model ->exists );
120
+ $ this ->assertTrue (self ::callProtectedMethod ($ model , 'isEncryptable ' ));
121
+ }
122
+
123
+ public function testIsEncryptableNew ()
124
+ {
125
+ $ strings = $ this ->randomValues ();
126
+ $ model = new DatabaseModel ();
127
+
128
+ foreach ($ strings as $ key => $ value ) {
129
+ $ model ->$ key = $ value ;
130
+ }
131
+
132
+ $ this ->assertFalse ($ model ->exists );
133
+ $ this ->assertFalse (self ::callProtectedMethod ($ model , 'isEncryptable ' ));
134
+ }
113
135
}
Original file line number Diff line number Diff line change @@ -40,8 +40,19 @@ public function testEncryptNormalString()
40
40
public function testEncryptStringWithPlus ()
41
41
{
42
42
$ this ->doTest ([
43
- 'dont_encrypt ' => '12345+ ' . rand (111111 , 999999 ) . '@gmail.com ' ,
44
- 'encrypt_me ' => 'abcde+ ' . str_random () . '@gmail.com ' ,
45
- ]);
43
+ 'dont_encrypt ' => '12345+ ' . rand (111111 , 999999 ) . '@gmail.com ' ,
44
+ 'encrypt_me ' => 'abcde+ ' . str_random () . '@gmail.com ' ,
45
+ ]);
46
+ }
47
+
48
+ public function testIsEncryptable ()
49
+ {
50
+ $ attributes = [
51
+ 'dont_encrypt ' => '12345+ ' . rand (111111 , 999999 ) . '@gmail.com ' ,
52
+ 'encrypt_me ' => 'abcde+ ' . str_random () . '@gmail.com ' ,
53
+ ];
54
+ $ model = new DummyModel ($ attributes );
55
+
56
+ $ this ->assertTrue (self ::callProtectedMethod ($ model , 'isEncryptable ' ));
46
57
}
47
58
}
You can’t perform that action at this time.
0 commit comments