1. Go to this page and download the library: Download ayd/authorize 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/ */
use Ayd\ApiResponseBase\Meta;
use Ayd\Authorize\ResourceAbilitiesResolver;
$meta = Meta::abilities(ResourceAbilitiesResolver::fromBooleanMap([
'create' => $user->can('product.create'),
'delete' => $user->can('product.delete'),
])->resolve());
use Ayd\Authorize\ResourceAbilitiesResolver;
use Ayd\Authorize\Concerns\HasResourceAbilities;
use Ayd\Authorize\Contracts\HasResourceAbilities as HasResourceAbilitiesContract;
class Product extends Model implements HasResourceAbilitiesContract
{
use HasResourceAbilities;
public function resolveAbilities(mixed $user = null): array|ResourceAbilitiesResolver
{
return $this->abilityResolver()
->allow('detail')
->fields('update', [
'title',
'quantity' => $this->isLocked()
? ['allowed' => false, 'reason' => 'Quantity locked']
: true,
])
->action('delete', $user?->can('delete', $this) ?? false);
}
}
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Ayd\Authorize\Laravel\Concerns\IncludesResourceAbilities;
class ProductResource extends JsonResource
{
use IncludesResourceAbilities;
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'title' => $this->title,
'abilities' => $this->resourceAbilities($request),
];
}
}
use Ayd\ApiResponseBase\Contracts\AbilitiesResolver;
$this->app->bind(AbilitiesResolver::class, ProductPageAbilitiesResolver::class);
use Ayd\Authorize\ResourceAbilities;
$abilities = ResourceAbilities::resolve($product, $user);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.