rpillz / laravel-feature-access
Laravel应用的特性访问
Requires
- php: ^8.0 || ^8.1
- illuminate/contracts: ^8.37 || ^9.0
- spatie/laravel-package-tools: ^1.13.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- orchestra/testbench: ^7.9
- pestphp/pest: ^1.20
- spatie/ray: ^1.28
This package is auto-updated.
Last update: 2024-09-10 00:12:38 UTC
README
向您的Laravel应用添加计划(例如:基本、标准、专业)和特性限制(例如:可以制作3个页面,可以上传视频)。
计划和相应特性在配置文件中硬编码,但可以通过特定用户的数据库条目来覆盖这些默认值。(即:您的特殊朋友想要所有功能,但像,免费。)
特性访问可以通过特质分配给任何模型(例如:用户、团队),该特质添加了用于您的应用逻辑的属性,例如 $user->canViewFeature('pictures-of-my-dog')
安装
您可以通过composer安装此包
composer require rpillz/laravel-feature-access
发布配置文件。这是定义您的特性和层级的地方。
php artisan vendor:publish --tag=feature-access-config
发布和运行迁移。这将为您模型附加的访问信息创建一个 'features' 表。
php artisan vendor:publish --tag=feature-access-migrations php artisan migrate
基本用法
1. 在配置中定义特性(和层级)
首先在 feature-access.php 配置文件中定义您的应用特性。
这是配置文件中的一个特性示例
return [ 'sample-feature' => [ // begin with the feature key name. This is used to request permissions. 'name' => 'Test Feature', // Human readable name 'read' => true, // Can read/view items in this feature 'update' => false, // cannot edit/change/update items 'create' => false, // cannot create new items 'destroy' => false, // cannot destroy/delete items 'limit' => 3, // limit on number of items allowed 'levels' => [ // Override the base feature permissions with levels or packages (eg: basic, pro, plus) 'pro' => [ // level/package key 'name' => 'Extra Stuff!', // human readable name 'update' => true, // pro level can edit/change/update items 'create' => true, // pro level can create items 'limit' => 5, // limit is increased // other permissions will default to base feature definition (above) ], ] ], ];
此特性的基本级别将是所有用户 和访客 使用的权限,除非他们明确授予了升级的访问级别或覆盖。
2. 将特质添加到模型中
在大多数情况下,您会将此特质添加到您的 User 模型中,这将允许该用户访问(或无法访问)特性。
use RPillz\FeatureAccess\Traits\HasFeatureAccess; class User extends Authenticatable { use HasFeatureAccess; ...
3. 在您的应用逻辑中添加权限检查
在一个blade模板中的示例,添加一个按钮以创建新项目,前提是当前用户被允许在 sample-feature 特性上 创建。
@if(Auth::user()->canCreateFeature('sample-feature')) <button>Add New Sample Item</button> @endif
4. 授予用户更高的访问权限
您可以使用 setFeatureAccess() 方法为您的用户(或任何模型)设置权限访问。
$user->setFeatureAccess('sample-feature', 'basic'); // give this user 'basic' level access to 'sample-feature' (which does not exist) $user->setFeatureAccess('sample-feature', 'pro'); // give this user 'pro' level access to 'sample-feature' $user->setFeatureAccess('sample-feature', 'pro', [ 'update' => false ]); // give this user 'pro' level access to 'sample-feature', but override the default setting to allow edits just for this user.
在上面的第一个示例中,用户被授予 basic 级别的 sample-feature 访问权限。然而,在特性配置文件中没有定义 basic 级别。他们对 sample-feature 的权限将默认为基本设置。如果没有为用户明确设置级别,则将使用这些相同的默认设置。
更多用法选项
使用团队模型
您的应用可能使用团队(类似于Jetstream),在这种情况下,您可能希望使用 Team 模型使用此特质,并通过团队访问用户权限。
将 HasFeatureAccess 特质添加到您的团队模型中。现在,该团队上的任何用户都将通过团队访问相同的特性。
Auth::user()->currentTeam->canReadFeature('maple-syrup');
此特质可以应用于任何模型,或者如果想要能够单独将权限应用于用户和团队,则可以应用于多个模型。(在这种情况下没有内置的权限继承,只有一个是或另一个。)
使用其他模型
您可以使用 HasFeatureAccess 特质向任何模型授予特性访问权限。在大多数情况下,该模型将与认证用户以某种方式相关联。但是,它可以适应其他用途
- 在租户或域名模型上,这将根据通过哪个域名访问您的应用来授予功能。
- 在特定的页面或文章上,以更改布局中显示的特性。
您可以在同一个应用中为多个模型使用此功能,因此您可以按照自己的意愿进行调整!
检查模型上权限的方法
$user->canUseFeature('feature-name', 'permission-requested'); // alias functions $user->canCreateFeature('sample-feature'); // permission-requested = create $user->canReadFeature('sample-feature'); // permission-requested = read $user->canViewFeature('sample-feature'); // permission-requested = read $user->canUpdateFeature('sample-feature'); // permission-requested = update $user->canEditFeature('sample-feature'); // permission-requested = update $user->canDestroyFeature('sample-feature'); // permission-requested = destroy $user->canDeleteFeature('sample-feature'); // permission-requested = destroy $user->withinFeatureLimit('sample-feature', $user->items->count()); // compares against feature limit $user->withinFeatureLimit('sample-feature', $user->items->count(), 1); // is there room to add 1 more item? // or get all the permission data for your model $user->getFeatureData('sample-feature'); // return array
使用外观
您可以使用外观方法来检查权限。然而,这仅适用于用户和/或团队模型。优点是,无论访客是否登录,这些方法都会正常工作,并为访客返回基础访问权限。
FeatureAccess::userCan(string $feature_name, string $permission); // check for permission to 'create', 'read', 'update', or 'destroy' // shortcut aliases FeatureAccess::userCanCreate('sample-feature'); // returns boolean true/false FeatureAccess::userCanRead('sample-feature'); FeatureAccess::userCanView('sample-feature'); // alias of 'read' permission FeatureAccess::userCanUpdate('sample-feature'); FeatureAccess::userCanEdit('sample-feature'); // alias of 'update' permission FeatureAccess::userCanDestroy('sample-feature'); FeatureAccess::userCanDelete('sample-feature'); // alias of 'destroy' permission // it also works for Teams! (a la Jetstream) FeatureAccess::teamCan(string $feature_name, string $permission); FeatureAccess::teamCanCreate('sample-feature'); // and all the other aliases as per above
如果您需要获取当前权限访问的完整数组,可以使用这个
FeatureAccess::userFeature('sample-feature'); // returns array FeatureAccess::teamFeature('sample-feature'); // returns array
与订阅(来自Cashier、SPark等)集成
而不是创建/更新重复的数据库行来更改用户的特征访问级别,有一个功能可以动态检查用户的活跃订阅级别,并据此获取特征权限。
此功能必须在feature-access.php配置文件中激活。
// Check for active subscription to grant access 'subscriptions' => true,
然后您需要在您的用户模型(或团队模型,或使用HasFeatureAccess特质的任何模型)上添加一个函数,该函数将返回与在您的功能配置中定义的适当级别相对应的名称。
public function getFeatureSubscriptionLevel(string $feature_name = null): ?string { // this logic needs to be customized according to your subscription set-up if ($subscription = $this->subscriptions->active()->first()){ return $subscription->name; // ie: "pro" } return null; }
设置完成后,在返回用户的当前功能访问时将查阅订阅。
$free_user->canCreateFeature('sample-feature'); //false $pro_user->canCreateFeature('sample-feature'); //true
按照优先级顺序,用户的特征级别首先由数据库中保存的内容(例如通过setFeatureAccesss())确定,然后检查订阅级别(如果已激活),最后默认到配置文件。这意味着您可以根据具体情况为VIP用户提供超出付费订阅的访问权限。技术上,您也可以降低他们的权限,怪物!)
以下是与Laravel Spark订阅相关的函数示例。
public function getFeatureSubscriptionLevel(string $feature_name = null): ?string { // If you're using Laravel Spark with this model as a Billable. // Note: The plan name from Spark is likely capitalized. Make sure it matches your feature-access config level names exactly. return $this->sparkPlan()->name ?: null; }
请注意,此函数还接受一个可选的$feature_name参数,这使得在需要的情况下可以更细粒度地进行订阅和逻辑操作。例如:如果访问页面的订阅与访问博客的订阅不同。
超级管理员权限
如果当前模型的属性与数组中的任何内容匹配,所有权限测试都将返回true。他们可以做任何事情。
return [ // grant all access to models which match... 'super_admin_property' => 'email', // this property... 'super_admin_access' => [ // to any of these. 'admin@example.com', ], ]
测试
composer test
变更日志
有关最近更改的更多信息,请参阅变更日志。
贡献
有关详细信息,请参阅贡献指南。
安全漏洞
有关如何报告安全漏洞,请参阅我们的安全策略。
鸣谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。