1. Go to this page and download the library: Download doefom/restrict 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/ */
doefom / restrict example snippets
// v0.4.x:
use Doefom\Restrict\Facades\Restrict;
use Statamic\Contracts\Auth\User;
use Statamic\Contracts\Entries\Entry;
class AppServiceProvider extends ServiceProvider
{
public function register(): void
{
// ...
}
public function boot(): void
{
Restrict::setRestriction(function (User $user, Entry $entry) {
// Can view own entries only
return $entry->authors()->contains($user->id());
});
}
}
namespace App\Policies;
use Statamic\Policies\EntryPolicy;
class MyEntryPolicy extends EntryPolicy
{
public function view($user, $entry)
{
// ...
}
}
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
$this->app->bind(
\Statamic\Policies\EntryPolicy::class,
\App\Policies\MyEntryPolicy::class
);
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
// ...
}
}
use Statamic\Policies\EntryPolicy;
class MyEntryPolicy extends EntryPolicy
{
public function view($user, $entry)
{
$default = parent::view($user, $entry);
if ($entry->blueprint()->hasField('author')) {
return $default && $entry->authors()->contains($user->id());
}
return $default;
}
}
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
// ...
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
// Add one general permission to Statamic
Permission::extend(function () {
Permission::register('view entries of other companies')
->label('View entries of other companies');
});
}
}
use Statamic\Policies\EntryPolicy;
class MyEntryPolicy extends EntryPolicy
{
public function view($user, $entry)
{
$default = parent::view($user, $entry);
if ($user->hasPermission('view entries of other companies')) {
// Can view all entries
return $default;
}
// Can view entries of the same company only
return $default && $user->get('company') === $entry->get('company');
}
}
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
// ...
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
// Add one permission per collection
Permission::extend(function () {
Permission::get('view {collection} entries')->addChild(
Permission::make("view {collection} entries of other companies")
->label("View entries of other companies")
);
});
}
}
use Statamic\Policies\EntryPolicy;
class MyEntryPolicy extends EntryPolicy
{
public function view($user, $entry)
{
$default = parent::view($user, $entry);
if ($user->hasPermission("view {$entry->collectionHandle()} entries of other companies")) {
// Can view all entries in the entry's collection
return $default;
}
// Can view entries of the same company only for this collection
return $default && $user->get('company') === $entry->get('company');
}
}
class ServiceProvider extends AddonServiceProvider
{
public function bootAddon(): void
{
if ($this->app->runningInConsole()) {
return;
}
// ...
}
}
// ----------------------------------------------------------------
// Rebinding the Entry Policy in your AppServiceProvider
// ----------------------------------------------------------------
$this->app->bind(
\Statamic\Policies\EntryPolicy::class,
\App\Policies\MyEntryPolicy::class
);
// ----------------------------------------------------------------
// Rebinding the Entry Query Builder as done
// by the Restrict addon in its ServiceProvider.
// ----------------------------------------------------------------
$this->app->bind(\Statamic\Stache\Query\EntryQueryBuilder::class, function ($app) {
return new \Doefom\Restrict\Stache\Query\EntryQueryBuilder($app['stache']->store('entries'));
});
shell
php artisan make:policy MyEntryPolicy
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.