enriko/laravel-attribute-restrictor
Composer 安装命令:
composer require enriko/laravel-attribute-restrictor
包简介
For Laravel. Easy way to restrict access to specified attributes in a Model
README 文档
README
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:
composer require enriko/laravel-attribute-restrictor
(Recomended) Publish the config file:
php artisan vendor:publish --tag=config
Configure
Add the RestrictedAttributes Trait to your model:
use Enriko\LaravelAttributeRestrictor\Traits\RestrictedAttributes; class User extends Authenticatable { use RestrictedAttributes; // ... }
Choose the attributes you wish to restrict with the getRestrictedAttributes method:
public static function getRestrictedAttributes(): array { return [ 'name', 'email' ]; }
Define the restrictions conditions by:
Specific definition using getAttributeRestrictions
If the attribute's condition is false, the attribute will be replaced:
public static function getAttributeRestrictions($attr): callable|bool { return match ($attr) { 'name' => fn() => Auth::user()->can('see_names'), // varies by user 'email' => false, // always restricts default => true // doesn't restrict }; }
Specific definition using array
Same as the previous options, but expects an array returning method:
public static function getAttributeRestrictionsArray(): array { return [ 'name' => fn() => Auth::user()->can('see_names'), // varies by user 'email' => false, // always restricts ]; }
Globally restriction
Defines a single restriction, if it's false, all attributes defined on getRestrictedAttributes will be restricted
public static function getGlobalRestriction() : callable|bool { return Auth::user()->can('see_sensitive_data'); }
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:
private function getReplacedText() { return 'You can't see user information'; }
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:
{
id: 1,
name: 'Foo',
account_number: '12345'
},
{
id: 2,
name: 'Bar',
account_number: '55555'
}
{
id: 3,
name: 'Baz',
account_number: '00220'
}
Now, let's say we configure the account_number as restricted. If the user can't access this information, $User->get() will return:
{
id: 1,
name: 'Foo',
account_number: 'Restricted'
},
{
id: 2,
name: 'Bar',
account_number: 'Restricted'
}
{
id: 3,
name: 'Baz',
account_number: 'Restricted'
}
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:
// $User = 'Foo' $User->getRawOriginal('account_number'); // will return 12345, no matter what
enriko/laravel-attribute-restrictor 适用场景与选型建议
enriko/laravel-attribute-restrictor 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 12 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「security」 「laravel」 「permission」 「roles」 「permissions」 「casting」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 enriko/laravel-attribute-restrictor 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 enriko/laravel-attribute-restrictor 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 enriko/laravel-attribute-restrictor 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provide a way to secure accesses to all routes of an symfony application.
This package provides a flexible way to add Role-based Permissions to Laravel 6.x
It's a barebone security class written on PHP
This package provides a flexible way to add Role-based Permissions to Laravel
Contao CMS integrity check for some files
A PHP security linter to detect insecure functions like var_dump, print_r, and other dangerous functions in your codebase
统计信息
- 总下载量: 12
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2024-12-02