MyProfileDataProvider.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace App\DataProvider\Access;
  3. use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
  4. use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
  5. use App\Entity\Access\Access;
  6. use App\DTO\Access\MyProfile;
  7. use App\Entity\Organization\Organization;
  8. use App\Service\Utils\Module;
  9. use Symfony\Component\Security\Core\Role\Role;
  10. use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
  11. use Symfony\Component\Security\Core\Security;
  12. final class MyProfileDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
  13. {
  14. /** @var Security */
  15. private $security;
  16. /** @var RoleHierarchyInterface */
  17. private $roleHierarchy;
  18. /** @var Module */
  19. private $module;
  20. public function __construct(
  21. Security $security,
  22. RoleHierarchyInterface $roleHierarchy,
  23. Module $module
  24. )
  25. {
  26. $this->security = $security;
  27. $this->roleHierarchy = $roleHierarchy;
  28. $this->module = $module;
  29. }
  30. public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
  31. {
  32. return MyProfile::class === $resourceClass;
  33. }
  34. public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): ?MyProfile
  35. {
  36. /** @var Access $access */
  37. $access = $this->security->getUser();
  38. $myProfile = $this->setProfileFromAccess($access);
  39. $myProfile->setRoles($this->roleHierarchy->getReachableRoleNames($this->security->getToken()->getRoleNames()));
  40. $myProfile->setModules($this->module->getOrganizationModules($access->getOrganization()));
  41. return $myProfile;
  42. }
  43. /**
  44. * @param Access $access
  45. * @return MyProfile
  46. */
  47. public function setProfileFromAccess(Access $access): MyProfile
  48. {
  49. $myProfile = new MyProfile();
  50. $myProfile->setId($access->getId());
  51. $myProfile->setName($access->getPerson()->getName());
  52. $myProfile->setGivenName($access->getPerson()->getGivenName());
  53. return $myProfile;
  54. }
  55. }