44
55use Wazza \DbEncrypt \Http \Controllers \BaseController ;
66use Wazza \DbEncrypt \Models \EncryptedAttributes ;
7+ use Wazza \DbEncrypt \Helper \Encryptor ;
78use Illuminate \Support \Facades \App ;
89use Illuminate \Database \Eloquent \Model ;
910use Illuminate \Contracts \Container \BindingResolutionException ;
2223
2324class DnEncryptController extends BaseController
2425{
26+ /**
27+ * The model to sync
28+ *
29+ * @var \Illuminate\Database\Eloquent\Model
30+ */
31+ private $ model ;
32+
33+ /**
34+ * Property to define the Model properties that will be encrypted.
35+ * Structure:
36+ * $encryptedProperties = [
37+ * 'social_security_number',
38+ * 'business_address',
39+ * 'business_city',
40+ * ];
41+ *
42+ * @var array
43+ */
44+ private $ encryptedProperties = [];
45+
2546 /**
2647 * Create a new CrmController instance and define the log identifier (blank will create a new one)
2748 *
@@ -36,6 +57,136 @@ public function __construct(?string $logIdentifier = null)
3657 // parent constructor
3758 parent ::__construct ($ logIdentifier );
3859
39- // ...
60+ // clear the properties
61+ $ this ->model = null ;
62+ $ this ->encryptedProperties = [];
63+ }
64+
65+ /**
66+ * Set the model to process.
67+ * Any property that is defined in the model will be encrypted in the `encrypted_attributes` table
68+ *
69+ * @param \Illuminate\Database\Eloquent\Model|null $model
70+ * @return $this
71+ */
72+ public function setModel (?Model $ model = null )
73+ {
74+ // -- set the model
75+ $ this ->model = $ model ;
76+
77+ // -- log the model set (and set the append to model type)
78+ $ this ->logger ->setLogIdentifier ('[ ' . get_class ($ this ->model ) . '] ' , true );
79+ $ this ->logger ->infoLow ('DB Encrypt Model set. Class: ` ' . get_class ($ this ->model ) . '`, Table: ` ' . $ this ->model ->getTable () . '`. ' );
80+
81+ // -- set the properties in the model that will be encrypted
82+ $ this ->setEncryptedProperties ($ model ->encryptedProperties ?? []);
83+
84+ // done
85+ return $ this ;
86+ }
87+
88+ /**
89+ * Set the property mapping for the CRM provider
90+ *
91+ * @param array $propertyMapping
92+ * @return $this
93+ */
94+ public function setEncryptedProperties (array $ propertyMapping = []): self
95+ {
96+ // set the property mappings
97+ $ this ->encryptedProperties = $ propertyMapping ;
98+ $ this ->logger ->infoLow ('DB Encrypt Mapping: ` ' . json_encode ($ this ->encryptedProperties ) . '` ' );
99+ return $ this ;
100+ }
101+
102+ /**
103+ * Get the encrypted properties defined in the model.
104+ *
105+ * @return array
106+ */
107+ public function getEncryptedProperties (): array
108+ {
109+ // return the encrypted properties
110+ return $ this ->encryptedProperties ;
111+ }
112+
113+ /**
114+ * Get the model that is set.
115+ *
116+ * @return \Illuminate\Database\Eloquent\Model|null
117+ */
118+ public function getModel (): ?Model
119+ {
120+ // return the model
121+ return $ this ->model ;
122+ }
123+
124+ /**
125+ * Check if the model is defined.
126+ *
127+ * @return bool
128+ */
129+ public function isModelDefined (): bool
130+ {
131+ // check if the model is defined
132+ return $ this ->model instanceof Model;
133+ }
134+
135+ /**
136+ * Encrypt all of the model defined ($this->encryptedProperties) properties.
137+ * If $property is provided, only that property will be encrypted.
138+ *
139+ * @param mixed $property The property to encrypt. If null, all properties will be encrypted.
140+ * @return void
141+ * @throws Exception
142+ */
143+ public function encrypt ($ property = null )
144+ {
145+ // check if the model is set
146+ if (!$ this ->isModelDefined ()) {
147+ throw new Exception ('Model is not set. Please set the model using the `setModel` method. ' );
148+ }
149+
150+ // check if the property is set
151+ if ($ property !== null && !in_array ($ property , $ this ->encryptedProperties )) {
152+ throw new Exception ('Property ` ' . $ property . '` is not defined in the encrypted properties. ' );
153+ }
154+
155+ // encrypt the properties
156+ $ this ->logger ->infoLow ('Encrypting properties for model: ' . get_class ($ this ->model ) . ', property: ' . ($ property ?? 'all ' ));
157+
158+ // loop through the encrypted properties
159+ foreach ($ this ->encryptedProperties as $ prop ) {
160+ // making sure the provided property to encrypt (if any) is defined in the model encrypted properties list
161+ if ($ property === null || $ prop === $ property ) {
162+ // check if the property exists in the model
163+ if (property_exists ($ this ->model , $ prop )) {
164+ // encrypt the property
165+ $ value = $ this ->model ->{$ prop };
166+ $ encryptedValue = Encryptor::encrypt ($ value );
167+
168+ // save the encrypted value in the EncryptedAttributes model
169+ EncryptedAttributes::updateOrCreate (
170+ [
171+ 'object_type ' => get_class ($ this ->model ),
172+ 'object_id ' => $ this ->model ->getKey (),
173+ 'attribute ' => $ prop ,
174+ ],
175+ [
176+ 'hash_index ' => Encryptor::hash ($ value ),
177+ 'encrypted_value ' => $ encryptedValue ,
178+ ]
179+ );
180+
181+ // log the encryption
182+ $ this ->logger ->infoLow ('Encrypted property: ' . $ prop . ', value: ' . $ value );
183+ } else {
184+ throw new Exception ('Property ` ' . $ prop . '` does not exist in the model. ' );
185+ }
186+ }
187+ }
188+
189+ // log the encryption
190+ $ this ->logger ->infoLow ('Encryption completed for model: ' . get_class ($ this ->model ) . ', properties: ' . json_encode ($ this ->encryptedProperties ));
40191 }
41192}
0 commit comments