Download the PHP package enriko/laravel-attribute-restrictor without Composer
On this page you can find all versions of the php package enriko/laravel-attribute-restrictor. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download enriko/laravel-attribute-restrictor
More information about enriko/laravel-attribute-restrictor
Files in enriko/laravel-attribute-restrictor
Package laravel-attribute-restrictor
Short Description For Laravel. Easy way to restrict access to specified attributes in a Model
License GPL-3.0-or-later
Informations about the package laravel-attribute-restrictor
LaravelAttributeRestrictor
This package offers an easy way to hide specific attributes from a model to specific situations. You can hide important data to specific users by using a trait with a couple of methods. Every other place that uses the model's data, will be restricted
How to use
Install
Install the package from packagist using composer:
(Recomended) Publish the config file:
Configure
Add the RestrictedAttributes Trait to your model:
Choose the attributes you wish to restrict with the getRestrictedAttributes method:
Define the restrictions conditions by:
Specific definition using getAttributeRestrictions
If the attribute's condition is false, the attribute will be replaced:
Specific definition using array
Same as the previous options, but expects an array returning method:
Globally restriction
Defines a single restriction, if it's false, all attributes defined on getRestrictedAttributes will be restricted
Define the substitute text
If the attribute is restricted, you can return something else in it's place. If you published the config file, you can modify it on:
root\config\enriko\attribute-restriction.php
But, you can also set individual messages for the model using the getReplacedText method:
Get the data
Finally, when you get the model's data, it will validate the restrictions. If your user pass, it'll see the info, unchanged. If not, it'll be replaced by the substitute restriction text.
Examples
Imagine a User model with the following data:
id | name | account_number |
---|---|---|
1 | Foo | 12345 |
2 | Bar | 55555 |
3 | Baz | 00220 |
Normally, if you use $User->get(), you will get:
Now, let's say we configure the account_number as restricted. If the user can't access this information, $User->get() will return:
Same thing for using $User->only(['name', 'account_number']) and $User->account_number.
Get original data
Sometimes, you might want to deal with the data, in this case, you don't want it to be substituted by some generic text.
Fundamentally, the restriction works as another cast, so you can ignore with by using getRawOriginal: