-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
172 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Migrating swisnl/laravel-encrypted-data | ||
|
||
## To Laravel Encrypted Casting | ||
The main difference between this package and [Laravel Encrypted Casting](https://laravel.com/docs/10.x/eloquent-mutators#encrypted-casting) is that this package serializes the data before encrypting it, while Laravel Encrypted Casting encrypts the data directly. This means that the data is not compatible between the two packages. In order to migrate from this package to Laravel Encrypted Casting, you will need to decrypt the data and then re-encrypt it using Laravel Encrypted Casting. Here is a step-by-step guide on how to do this: | ||
|
||
[//]: # (TODO: What to do when you need serialized data?) | ||
|
||
1. Set up Encrypted Casting, but keep extending `EncryptedModel` from this package: | ||
```diff | ||
- protected $encrypted = [ | ||
- 'secret', | ||
- ]; | ||
+ protected $casts = [ | ||
+ 'secret' => 'encrypted', | ||
+ ]; | ||
``` | ||
2. Set up our custom model encrypter in your `AppServiceProvider`: | ||
```php | ||
public function boot(): void | ||
{ | ||
$modelEncrypter = new \Swis\Laravel\Encrypted\ModelEncrypter(); | ||
YourEncryptedModel::encryptUsing($modelEncrypter); | ||
// ... all your other models that extend \Swis\Laravel\Encrypted\EncryptedModel | ||
} | ||
``` | ||
3. Run our re-encryption command: | ||
```bash | ||
php artisan encrypted-data:re-encrypt:models app/Models --quietly --no-touch --with-trashed | ||
``` | ||
N.B. Modify options as needed! | ||
4. Remove the `Swis\Laravel\Encrypted\EncryptedModel` from your models and replace it with `Illuminate\Database\Eloquent\Model`: | ||
```diff | ||
- use Swis\Laravel\Encrypted\EncryptedModel | ||
+ use Illuminate\Database\Eloquent\Model | ||
|
||
- class YourEncryptedModel extends EncryptedModel | ||
+ class YourEncryptedModel extends Model | ||
``` | ||
5. Remove our custom model encrypter from your `AppServiceProvider` (step 2). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Swis\Laravel\Encrypted\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class ReEncryptModels extends Command | ||
{ | ||
protected $signature = 'encrypted-data:re-encrypt:models [directory] {--quietly} {--no-touch} {--with-trashed}'; | ||
|
||
protected $description = 'Re-encrypt models'; | ||
|
||
public function handle(): int | ||
{ | ||
collect($models) | ||
->filter(fn (string $model) => is_a($model, Model::class, true)) | ||
->each( | ||
/** | ||
* @param class-string<\Illuminate\Database\Eloquent\Model> $modelClass | ||
*/ | ||
function (string $modelClass) { | ||
// with trashed? | ||
$modelClass::query()->eachById(function (Model $model) { | ||
// quietly? | ||
// no-touch? | ||
return $model->save(); | ||
}); | ||
} | ||
); | ||
|
||
return 0; | ||
} | ||
|
||
/** | ||
* Get the root namespace for the class. | ||
* | ||
* @return string | ||
*/ | ||
protected function rootNamespace() | ||
{ | ||
return $this->laravel->getNamespace(); | ||
} | ||
|
||
/** | ||
* Get the default namespace for the class. | ||
* | ||
* @param string $rootNamespace | ||
* | ||
* @return string | ||
*/ | ||
protected function getDefaultNamespace($rootNamespace) | ||
{ | ||
return is_dir(app_path('Models')) ? $rootNamespace.'\\Models' : $rootNamespace; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Swis\Laravel\Encrypted; | ||
|
||
use Illuminate\Contracts\Encryption\Encrypter; | ||
|
||
class ModelEncrypter implements Encrypter | ||
{ | ||
private ?Encrypter $encrypter; | ||
|
||
public function __construct() | ||
{ | ||
$this->encrypter = app('encrypted-data.encrypter'); | ||
} | ||
|
||
public function encrypt($value, $serialize = true) | ||
{ | ||
return $this->encrypter->encrypt($value, $serialize); | ||
} | ||
|
||
public function decrypt($payload, $unserialize = true) | ||
{ | ||
$decrypted = $this->encrypter->decrypt($payload, false); | ||
|
||
$unserialized = @unserialize($decrypted); | ||
if ($unserialized === false && $decrypted !== 'b:0;') { | ||
return $decrypted; | ||
} | ||
|
||
return $unserialized; | ||
} | ||
|
||
public function getKey() | ||
{ | ||
return $this->encrypter->getKey(); | ||
} | ||
|
||
public function getAllKeys() | ||
{ | ||
return $this->encrypter->getAllKeys(); | ||
} | ||
|
||
public function getPreviousKeys() | ||
{ | ||
return $this->encrypter->getPreviousKeys(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters