|
1 |
| -laravel-redact-model |
| 1 | +# Laravel Redacted Model |
| 2 | + |
| 3 | +Laravel Redacted Model makes it easier to hide or modify fields on a modal based on given conditions. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Laravel Redacted Model can be installed using composer. Run the following command in your project. |
| 8 | + |
| 9 | +```bash |
| 10 | +composer require langleyfoxall/laravel-redacted-modal |
| 11 | +``` |
| 12 | + |
| 13 | +If you have never used the Composer dependency manager before, head to the [Composer website](https://getcomposer.org/) for more information on how to get started. |
| 14 | + |
| 15 | +# Usage |
| 16 | + |
| 17 | +To redact fields simply extend `RedactedModel` in your model and set the `redacted` variable to an array of the fields you want to protect. By default when accesed these fields will return `[Hidden Data]`. |
| 18 | + |
| 19 | +```php |
| 20 | +class SensetiveModel extends RedactedModel |
| 21 | +{ |
| 22 | + protected $redacted = ['name']; |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +### Conditionally redacting data |
| 27 | + |
| 28 | +To conditionally redact fields override `shouldRedactField` on your model. The name of the field will be passed into this method. This will return true by default until you override it. |
| 29 | + |
| 30 | +_Note: Only fields specified in `$redacted` will be redacted regardless of what's returned from this method._ |
| 31 | + |
| 32 | +```php |
| 33 | +class SensetiveModel extends RedactedModel |
| 34 | +{ |
| 35 | + protected $redacted = ['name']; |
| 36 | + |
| 37 | + public function shouldRedactField($key) |
| 38 | + { |
| 39 | + return !\Auth::user()->canSeeSensetiveFields(); |
| 40 | + } |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | + |
| 45 | +### Changing the default redacted string |
| 46 | + |
| 47 | +To change the message returned you can set the `redactedString` on your model. This will then be returned instead of `[Hidden Data]`. |
| 48 | + |
| 49 | +```php |
| 50 | +class SensetiveModel extends RedactedModel |
| 51 | +{ |
| 52 | + protected $redacted = ['name']; |
| 53 | + |
| 54 | + protected $redactedString = '[Top Secret]'; |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +### Hiding fields instead of redacting them |
| 59 | + |
| 60 | +If you want to completely omit the field instead of redacting it you can set the `redact` variable on your model to false. |
| 61 | + |
| 62 | +```php |
| 63 | +class SensetiveModel extends RedactedModel |
| 64 | +{ |
| 65 | + protected $redacted = ['name']; |
| 66 | + |
| 67 | + protected $redact = false; |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +By default the array key of fields that return `null` and are in the redacted fields list will too be omitted in case the field name is sensetive. To disable this set `$redactKeys` to false on your model. |
| 72 | + |
| 73 | +```php |
| 74 | +class SensetiveModel extends RedactedModel |
| 75 | +{ |
| 76 | + protected $redacted = ['name']; |
| 77 | + |
| 78 | + protected $redactKeys = false; |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +### Redacted value accessors |
| 83 | + |
| 84 | +Redacted value accessors are defined the same way as [Laravel Accessors](https://laravel.com/docs/5.7/eloquent-mutators#accessors-and-mutators) but ending in `RedactedValue` instead of `Accessor`. |
| 85 | + |
| 86 | +The original value is passed into the method, this allows you to abstract the value instead of omitting or redacting it. |
| 87 | + |
| 88 | +```php |
| 89 | +class SensetiveModel extends RedactedModel |
| 90 | +{ |
| 91 | + protected $redacted = ['name']; |
| 92 | + |
| 93 | + public function getNameRedactedValue($value) |
| 94 | + { |
| 95 | + return subStr($value, 0).'***'.subStr($value, -1); |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
0 commit comments