<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
whitedigital-eu / entity-resource-mapper-bundle example snippets
namespace App\Service;
use App\Dto\CustumerDto;
use App\Entity\Customer;
use WhiteDigital\EntityResourceMapper\Mapper\ClassMapperConfiguratorInterface;
use WhiteDigital\EntityResourceMapper\Mapper\ClassMapper;
class ClassMapperConfigurator implements ClassMapperConfiguratorInterface
{
public function __invoke(ClassMapper $classMapper)
{
$classMapper->registerMapping(CustomerResource::class, Customer::class);
// with Callback - must return true for mapping to be active
$classMapper->registerMapping(PublicHtmlResource::class, Html::class, callback: static fn (array $context) => !self::isAdmin($context));
$classMapper->registerMapping(AdminHtmlResource::class, Html::class, callback: static fn (array $context) => self::isAdmin($context));
}
/**
* IsAdmin or else IsPublic.
*/
private static function isAdmin(array $context): bool
{
return array_key_exists('request_uri', $context) && str_starts_with($context['request_uri'], '/api/admin');
}
}
use App\Dto\CustumerDto;
use Doctrine\ORM\Mapping as ORM;
use WhiteDigital\EntityResourceMapper\Attribute\Mapping;
#[ORM\Entity]
#[Mapping(CustumerDto::class)]
class Customer ...
use WhiteDigital\EntityResourceMapper\Attribute\Mapping;
use App\Entity\Customer;
#[Mapping(Customer::class)]
class CustumerDto ...
// src/Service/Configurator/AuthorizationServiceConfigurator.php
use WhiteDigital\EntityResourceMapper\Resource\BaseResource;
use WhiteDigital\EntityResourceMapper\Security\AuthorizationServiceConfiguratorInterface;
final class AuthorizationServiceConfigurator implements AuthorizationServiceConfiguratorInterface
{
public function __invoke(AuthorizationService $service): void
{
$service->setAuthorizationOverride(static fn (BaseEntity|BaseResource|null $object = null) => 'cli' === strtolower(PHP_SAPI) && 'test' !== $_ENV['APP_ENV']);
$service->setResources([
ActivityResource::class => [
AuthorizationService::ALL => ['ROLE_SUPER_ADMIN' => GrantType::ALL, 'ROLE_KAM' => GrantType::ALL],
AuthorizationService::COL_GET => [, 'ROLE_JUNIOR_KAM' => GrantType::OWN],
AuthorizationService::ITEM_GET => [, 'ROLE_JUNIOR_KAM' => GrantType::GROUP],
AuthorizationService::COL_POST => [],
AuthorizationService::ITEM_PATCH => [],
AuthorizationService::ITEM_DELETE => [],
]]);
//either mainResource or roles key must be set
$service->setMenuStructure(
[
['name' => 'ACTIVITIES',
'mainResource' => ActivityResource::class,
],
['name' => 'REPORTS',
'roles' => ['ROLE_SUPER_ADMIN', 'ROLE_KAM'],
],
]);
}
}
$this->authorizationService->limitGetCollection($resourceClass, $queryBuilder); // This will affect queryBuilder object
$this->authorizationService->authorizeSingleObject($entity, AuthorizationService::ITEM_GET); // This will throw AccessDeniedException if not authorized
$this->authorizationService->authorizeSingleObject($data, AuthorizationService::ITEM_PATCH); // This will throw AccessDeniedException if not authorized
// or
$this->authorizationService->authorizeSingleObject($data, AuthorizationService::COL_POST; // This will throw AccessDeniedException if not authorized
$this->authorizationService->authorizeSingleObject($data, AuthorizationService::ITEM_DELETE); // This will throw AccessDeniedException if not authorized
#[AuthorizeResource(accessResolvers: [
new AccessResolverConfiguration(className: OwnerPropertyAccessResolver::class, config: ['ownerPropertyPath' => 'supervisor']),
])]
#[Groups('deal_read')]
#[ApiProperty(attributes: ["openapi_context" => ["description" => "If Authorization GrantType::OWN or GROUP is calculated, resource can be restricted."]])]
public bool $isRestricted = false;
declare(strict_types = 1);
use App\Constants\Enum\Roles;
use Symfony\Config\EntityResourceMapperConfig;
return static function (EntityResourceMapperConfig $config): void {
$config
->rolesEnum(Roles::class);
};
declare(strict_types = 1);
namespace App\Constants\Enum;
enum Roles: string
{
case ROLE_USER = 'ROLE_USER';
case ROLE_ADMIN = 'ROLE_ADMIN'
}
// src/Service/Configurator/AuthorizationServiceConfigurator.php
use WhiteDigital\EntityResourceMapper\Security\AuthorizationServiceConfiguratorInterface;
use WhiteDigital\EntityResourceMapper\Security\Enum\GrantType;
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
final class AuthorizationServiceConfigurator implements AuthorizationServiceConfiguratorInterface
{
public function __invoke(AuthorizationService $service): void
{
$service->setResources([
ActivityResource::class => [
AuthorizationService::ALL => ['ROLE_SUPER_ADMIN' => GrantType::ALL, 'ROLE_KAM' => GrantType::ALL],
AuthorizationService::COL_GET => ['ROLE_JUNIOR_KAM' => GrantType::LIMITED],
AuthorizationService::ITEM_GET => [AuthenticatedVoter::PUBLIC_ACCESS => GrantType::ALL],
AuthorizationService::COL_POST => [],
AuthorizationService::ITEM_PATCH => [],
AuthorizationService::ITEM_DELETE => [],
]]);
}
}
use WhiteDigital\EntityResourceMapper\MenuBuilder\Interface\MenuBuilderServiceConfiguratorInterface;
use WhiteDigital\EntityResourceMapper\MenuBuilder\Services\MenuBuilderService;
final class MenuBuilderServiceConfigurator implements MenuBuilderServiceConfiguratorInterface
{
public function __invoke(MenuBuilderService $service): void
{
//either mainResource or roles key must be set
$service->setMenuStructure(
[
[
'name' => 'ACTIVITIES',
'mainResource' => ActivityResource::class,
],
[
'name' => 'REPORTS',
'roles' => ['ROLE_SUPER_ADMIN', 'ROLE_KAM'],
],
]);
}
}
use WhiteDigital\EntityResourceMapper\MenuBuilder\Services\MenuBuilderService;
class SomeClass
{
public function someFunction(MenuBuilderService $service): void
{
$data = $service->getMenuForCurrentUser();
}
}
namespace App\ApiResource;
use WhiteDigital\EntityResourceMapper\Attribute\ExtendedApiResource;
#[ExtendedApiResource(routePrefix: '/vendor')]
class AuditResource extends WhiteDigital\Audit\ApiResource\AuditResource
{
}
use Symfony\Config\EntityResourceMapperConfig;
return static function (EntityResourceMapperConfig $config): void {
$namespaces = $config
->namespaces();
$namespaces
->apiResource('ApiResource')
->classMapConfigurator('Service\\Configurator') # ->space('_');
};
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use WhiteDigital\EntityResourceMapper\Filters\ResourceDateFilter;
use WhiteDigital\EntityResourceMapper\Resource\BaseResource;
#[
ApiResource (
shortName: 'User'
),
ApiFilter(ResourceDateFilter::class, properties: ['createdAt', 'updatedAt', ]),
]
class UserResource extends BaseResource
{
public ?DateTimeImmutable $createdAt = null;
public ?DateTimeImmutable $updatedAt = null;
public ?UserResource $parent = null;
}
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use WhiteDigital\EntityResourceMapper\Filters\ResourceDateFilter;
use WhiteDigital\EntityResourceMapper\Resource\BaseResource;
#[
ApiResource (
shortName: 'User'
),
ApiFilter(ResourceDateFilter::class, properties: ['createdAt', 'updatedAt', 'parent.createdAt', 'parent.updatedAt']),
]
class UserResource extends BaseResource
{
public ?DateTimeImmutable $createdAt = null;
public ?DateTimeImmutable $updatedAt = null;
public ?UserResource $parent = null;
}
use ApiPlatform\Metadata\ApiResource;
#[ApiResource]
class ClassifierResource
{
public mixed $id = null;
public ?string $value = null;
public ?array $data = [];
public ?ClassifierType $type = null;
}
enum ClassifierType: string
{
case ONE = 'ONE';
case TWO = 'TWO';
}
use ApiPlatform\Metadata\ApiResource;
use WhiteDigital\EntityResourceMapper\Validator\Constraints as WDAssert;
#[ApiResource]
class TestResource
{
#[WDAssert\CorrectClassifierType(ClassifierType::ONE)]
public ?ClassifierResource $one = null;
}
use ApiPlatform\Metadata\ApiResource;
use WhiteDigital\EntityResourceMapper\Validator\Constraints as WDAssert;
#[ApiResource]
#[WDAssert\ClassifierRequiredDataIsSet(ClassifierType::ONE, ['test1'])]
class ClassifierResource
{
public mixed $id = null;
public ?string $value = null;
public ?array $data = [];
public ?ClassifierType $type = null;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.