This repository was archived by the owner on Apr 7, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +50
-5
lines changed Expand file tree Collapse file tree 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
125125 {
126126 $ encrypt = DatabaseEncryption::isEnabled () && isset ($ this ->encrypted ) && is_array ($ this ->encrypted ) ? $ this ->encrypted : [];
127127
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 );
129141 }
130142
131143 /**
@@ -318,6 +330,6 @@ protected function getArrayableAttributes()
318330 */
319331 public function getAttributes ()
320332 {
321- return $ this ->exists ? $ this ->doDecryptAttributes (parent ::getAttributes ()) : parent ::getAttributes ();
333+ return $ this ->isEncryptable () ? $ this ->doDecryptAttributes (parent ::getAttributes ()) : parent ::getAttributes ();
322334 }
323335}
Original file line number Diff line number Diff line change @@ -110,4 +110,26 @@ public function testGetArrayableAttributes()
110110 $ this ->assertEquals ($ strings ['shouldnt_be_encrypted ' ], $ attributes ['shouldnt_be_encrypted ' ]);
111111 $ this ->assertEquals ($ strings ['should_be_encrypted ' ], $ attributes ['should_be_encrypted ' ]);
112112 }
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+ }
113135}
Original file line number Diff line number Diff line change @@ -40,8 +40,19 @@ public function testEncryptNormalString()
4040 public function testEncryptStringWithPlus ()
4141 {
4242 $ 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 ' ));
4657 }
4758}
You can’t perform that action at this time.
0 commit comments