PHP code example of rpillz / laravel-feature-access
1. Go to this page and download the library: Download rpillz/laravel-feature-access library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rpillz / laravel-feature-access example snippets
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)
],
]
],
];
use RPillz\FeatureAccess\Traits\HasFeatureAccess;
class User extends Authenticatable
{
use HasFeatureAccess;
...
@if(Auth::user()->canCreateFeature('sample-feature'))
<button>Add New Sample Item</button>
@endif
$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.
$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
// Check for active subscription to grant access
'subscriptions' => true,
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;
}
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;
}
return [
// grant all access to models which match...
'super_admin_property' => 'email', // this property...
'super_admin_access' => [ // to any of these.
'[email protected]',
],
]