| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?php
- namespace App\DataProvider\Access;
- use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
- use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
- use App\Entity\Access\Access;
- use App\DTO\Access\MyProfile;
- use App\Entity\Organization\Organization;
- use App\Service\Utils\Module;
- use Symfony\Component\Security\Core\Role\Role;
- use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
- use Symfony\Component\Security\Core\Security;
- final class MyProfileDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
- {
- /** @var Security */
- private $security;
- /** @var RoleHierarchyInterface */
- private $roleHierarchy;
- /** @var Module */
- private $module;
- public function __construct(
- Security $security,
- RoleHierarchyInterface $roleHierarchy,
- Module $module
- )
- {
- $this->security = $security;
- $this->roleHierarchy = $roleHierarchy;
- $this->module = $module;
- }
- public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
- {
- return MyProfile::class === $resourceClass;
- }
- public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): ?MyProfile
- {
- /** @var Access $access */
- $access = $this->security->getUser();
- $myProfile = $this->setProfileFromAccess($access);
- $myProfile->setRoles($this->roleHierarchy->getReachableRoleNames($this->security->getToken()->getRoleNames()));
- $myProfile->setModules($this->module->getOrganizationModules($access->getOrganization()));
- return $myProfile;
- }
- /**
- * @param Access $access
- * @return MyProfile
- */
- public function setProfileFromAccess(Access $access): MyProfile
- {
- $myProfile = new MyProfile();
- $myProfile->setId($access->getId());
- $myProfile->setName($access->getPerson()->getName());
- $myProfile->setGivenName($access->getPerson()->getGivenName());
- return $myProfile;
- }
- }
|