diff --git a/navigation.php b/navigation.php index 70e7bba..d0a505f 100644 --- a/navigation.php +++ b/navigation.php @@ -144,7 +144,7 @@ return [ 'children' => [ 'The two applications' => 'the-two-applications', 'Tenants' => 'tenants', - 'Encrypt Attributes' => 'encrypt-attributes', + 'Tenant attribute encryption' => 'tenant-attribute-encryption', 'Domains' => 'domains', 'Event system' => 'event-system', 'Routes' => 'routes', diff --git a/source/docs/v3/encrypt-attributes.blade.md b/source/docs/v3/encrypt-attributes.blade.md deleted file mode 100644 index 5bd1873..0000000 --- a/source/docs/v3/encrypt-attributes.blade.md +++ /dev/null @@ -1,91 +0,0 @@ ---- -title: Encrypt Attributes -extends: _layouts.documentation -section: content ---- - -# Encrypt Tenant Attributes {#encrypt} - -We can encrypt the tenant attributes if needed. In the example below, We will encrypt the database username and password. - -# Encrypt Tenant Username And Password {#encrypt-username-password} - -When resolving the tenant's database configuration, this package uses the values in the `data` column stored as `tenancy_db_username` and `tenancy_db_password` to create the values used in the DB config as `username` and `password`, -respectively. So the Tenancy package looks in the [Virtual Column](https://github.com/archtechx/virtualcolumn) for `tenancy_db_username` and `tenancy_db_password`. - -The package provides a place in the migrations for your own custom columns and a method to tell the model that it should look for the data in an actual database column instead of the virtual column. - -This means, we can do this: - -```php -string('id')->primary(); - - // Your custom columns - $table->string('tenancy_db_username', 512); - $table->string('tenancy_db_password', 512); - - $table->timestamps(); - $table->json('data')->nullable(); - }); - } -} -``` - -To create the columns in the database. Then let the model know about them And combine this with [Laravel encrypted casts](https://laravel.com/docs/9.x/eloquent-mutators#encrypted-casting) -(or, better yet, a custom cast) to encrypt and decrypt these when needed: - -```php - 'encrypted', - 'tenancy_db_password' => 'encrypted', - ]; - - /** - * Define custom columns for this model (that shouldn't be accessed via 'data' property). - * - * @return array - */ - public static function getCustomColumns(): array - { - return [ - 'id', - 'tenancy_db_username', - 'tenancy_db_password', - ]; - } -} -``` \ No newline at end of file diff --git a/source/docs/v3/tenant-attribute-encryption.blade.md b/source/docs/v3/tenant-attribute-encryption.blade.md new file mode 100644 index 0000000..f0488f7 --- /dev/null +++ b/source/docs/v3/tenant-attribute-encryption.blade.md @@ -0,0 +1,63 @@ +--- +title: Tenant attribute encryption +extends: _layouts.documentation +section: content +--- + +# Tenant attribute encryption {#encrypt} + +To encrypt the attributes, you must store them in [custom columns](https://tenancyforlaravel.com/docs/v3/tenants/#custom-columns). For example, we'll encrypt the tenant's database credentials (which are stored using the [Virtual Column](https://github.com/archtechx/virtualcolumn) as `tenancy_db_username` and `tenancy_db_password` by default) in a few steps. + +Add custom columns to the tenants migration (we recommend making the string size at least 512 characters, so it's big enough to contain the encrypted data): + +```php +string('id')->primary(); + + // Your custom columns + $table->string('tenancy_db_username', 512); + $table->string('tenancy_db_password', 512); + + $table->timestamps(); + $table->json('data')->nullable(); + }); + } +} +``` + +Define the custom columns in the Tenant model: + +```php +public static function getCustomColumns(): array +{ + return [ + 'id', + 'tenancy_db_username', + 'tenancy_db_password', + ]; +} +``` + +Then cast the attributes (using [Laravel's encrypted casts](https://laravel.com/docs/9.x/eloquent-mutators#encrypted-casting), or your custom casts) to encrypt and decrypt the attributes when needed: + +```php +protected $casts = [ + 'tenancy_db_username' => 'encrypted', + 'tenancy_db_password' => 'encrypted', +]; +```