vendor/ezsystems/ezplatform-kernel/eZ/Publish/Core/Repository/Mapper/RoleDomainMapper.php line 32

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. namespace eZ\Publish\Core\Repository\Mapper;
  7. use eZ\Publish\Core\Repository\Permission\LimitationService;
  8. use eZ\Publish\Core\Repository\Values\User\Policy;
  9. use eZ\Publish\Core\Repository\Values\User\PolicyDraft;
  10. use eZ\Publish\Core\Repository\Values\User\Role;
  11. use eZ\Publish\API\Repository\Values\User\Role as APIRole;
  12. use eZ\Publish\Core\Repository\Values\User\RoleDraft;
  13. use eZ\Publish\API\Repository\Values\User\RoleCopyStruct as APIRoleCopyStruct;
  14. use eZ\Publish\API\Repository\Values\User\RoleCreateStruct as APIRoleCreateStruct;
  15. use eZ\Publish\Core\Repository\Values\User\UserRoleAssignment;
  16. use eZ\Publish\Core\Repository\Values\User\UserGroupRoleAssignment;
  17. use eZ\Publish\API\Repository\Values\User\User;
  18. use eZ\Publish\API\Repository\Values\User\UserGroup;
  19. use eZ\Publish\SPI\Persistence\User\Policy as SPIPolicy;
  20. use eZ\Publish\SPI\Persistence\User\RoleAssignment as SPIRoleAssignment;
  21. use eZ\Publish\SPI\Persistence\User\Role as SPIRole;
  22. use eZ\Publish\SPI\Persistence\User\RoleCopyStruct as SPIRoleCopyStruct;
  23. use eZ\Publish\SPI\Persistence\User\RoleCreateStruct as SPIRoleCreateStruct;
  24. /**
  25.  * Internal service to map Role objects between API and SPI values.
  26.  *
  27.  * @internal Meant for internal use by Repository.
  28.  */
  29. class RoleDomainMapper
  30. {
  31.     /** @var \eZ\Publish\Core\Repository\Permission\LimitationService */
  32.     protected $limitationService;
  33.     /**
  34.      * @param \eZ\Publish\Core\Repository\Permission\LimitationService $limitationService
  35.      */
  36.     public function __construct(LimitationService $limitationService)
  37.     {
  38.         $this->limitationService $limitationService;
  39.     }
  40.     /**
  41.      * Maps provided SPI Role value object to API Role value object.
  42.      *
  43.      * @param \eZ\Publish\SPI\Persistence\User\Role $role
  44.      *
  45.      * @return \eZ\Publish\API\Repository\Values\User\Role
  46.      */
  47.     public function buildDomainRoleObject(SPIRole $role)
  48.     {
  49.         $rolePolicies = [];
  50.         foreach ($role->policies as $spiPolicy) {
  51.             $rolePolicies[] = $this->buildDomainPolicyObject($spiPolicy);
  52.         }
  53.         return new Role(
  54.             [
  55.                 'id' => $role->id,
  56.                 'identifier' => $role->identifier,
  57.                 'status' => $role->status,
  58.                 'policies' => $rolePolicies,
  59.             ]
  60.         );
  61.     }
  62.     /**
  63.      * Builds a RoleDraft domain object from value object returned by persistence
  64.      * Decorates Role.
  65.      *
  66.      * @param \eZ\Publish\SPI\Persistence\User\Role $spiRole
  67.      *
  68.      * @return \eZ\Publish\API\Repository\Values\User\RoleDraft
  69.      */
  70.     public function buildDomainRoleDraftObject(SPIRole $spiRole)
  71.     {
  72.         return new RoleDraft(
  73.             [
  74.                 'innerRole' => $this->buildDomainRoleObject($spiRole),
  75.             ]
  76.         );
  77.     }
  78.     /**
  79.      * Maps provided SPI Policy value object to API Policy value object.
  80.      *
  81.      * @param \eZ\Publish\SPI\Persistence\User\Policy $spiPolicy
  82.      *
  83.      * @return \eZ\Publish\API\Repository\Values\User\Policy|\eZ\Publish\API\Repository\Values\User\PolicyDraft
  84.      */
  85.     public function buildDomainPolicyObject(SPIPolicy $spiPolicy)
  86.     {
  87.         $policyLimitations = [];
  88.         if ($spiPolicy->module !== '*' && $spiPolicy->function !== '*' && $spiPolicy->limitations !== '*') {
  89.             foreach ($spiPolicy->limitations as $identifier => $values) {
  90.                 $policyLimitations[] = $this->limitationService->getLimitationType($identifier)->buildValue($values);
  91.             }
  92.         }
  93.         $policy = new Policy(
  94.             [
  95.                 'id' => $spiPolicy->id,
  96.                 'roleId' => $spiPolicy->roleId,
  97.                 'module' => $spiPolicy->module,
  98.                 'function' => $spiPolicy->function,
  99.                 'limitations' => $policyLimitations,
  100.             ]
  101.         );
  102.         // Original ID is set on SPI policy, which means that it's a draft.
  103.         if ($spiPolicy->originalId) {
  104.             $policy = new PolicyDraft(['innerPolicy' => $policy'originalId' => $spiPolicy->originalId]);
  105.         }
  106.         return $policy;
  107.     }
  108.     /**
  109.      * Builds the API UserRoleAssignment object from provided SPI RoleAssignment object.
  110.      *
  111.      * @param \eZ\Publish\SPI\Persistence\User\RoleAssignment $spiRoleAssignment
  112.      * @param \eZ\Publish\API\Repository\Values\User\User $user
  113.      * @param \eZ\Publish\API\Repository\Values\User\Role $role
  114.      *
  115.      * @return \eZ\Publish\API\Repository\Values\User\UserRoleAssignment
  116.      */
  117.     public function buildDomainUserRoleAssignmentObject(SPIRoleAssignment $spiRoleAssignmentUser $userAPIRole $role)
  118.     {
  119.         $limitation null;
  120.         if (!empty($spiRoleAssignment->limitationIdentifier)) {
  121.             $limitation $this
  122.                 ->limitationService
  123.                 ->getLimitationType($spiRoleAssignment->limitationIdentifier)
  124.                 ->buildValue($spiRoleAssignment->values);
  125.         }
  126.         return new UserRoleAssignment(
  127.             [
  128.                 'id' => $spiRoleAssignment->id,
  129.                 'limitation' => $limitation,
  130.                 'role' => $role,
  131.                 'user' => $user,
  132.             ]
  133.         );
  134.     }
  135.     /**
  136.      * Builds the API UserGroupRoleAssignment object from provided SPI RoleAssignment object.
  137.      *
  138.      * @param \eZ\Publish\SPI\Persistence\User\RoleAssignment $spiRoleAssignment
  139.      * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup
  140.      * @param \eZ\Publish\API\Repository\Values\User\Role $role
  141.      *
  142.      * @return \eZ\Publish\API\Repository\Values\User\UserGroupRoleAssignment
  143.      */
  144.     public function buildDomainUserGroupRoleAssignmentObject(SPIRoleAssignment $spiRoleAssignmentUserGroup $userGroupAPIRole $role)
  145.     {
  146.         $limitation null;
  147.         if (!empty($spiRoleAssignment->limitationIdentifier)) {
  148.             $limitation $this
  149.                 ->limitationService
  150.                 ->getLimitationType($spiRoleAssignment->limitationIdentifier)
  151.                 ->buildValue($spiRoleAssignment->values);
  152.         }
  153.         return new UserGroupRoleAssignment(
  154.             [
  155.                 'id' => $spiRoleAssignment->id,
  156.                 'limitation' => $limitation,
  157.                 'role' => $role,
  158.                 'userGroup' => $userGroup,
  159.             ]
  160.         );
  161.     }
  162.     /**
  163.      * Creates SPI Role create struct from provided API role create struct.
  164.      */
  165.     public function buildPersistenceRoleCreateStruct(APIRoleCreateStruct $roleCreateStruct): SPIRoleCreateStruct
  166.     {
  167.         $policiesToCreate $this->fillRoleStructWithPolicies($roleCreateStruct);
  168.         return new SPIRoleCreateStruct(
  169.             [
  170.                 'identifier' => $roleCreateStruct->identifier,
  171.                 'policies' => $policiesToCreate,
  172.             ]
  173.         );
  174.     }
  175.     /**
  176.      * Creates SPI Role copy struct from provided API role copy struct.
  177.      */
  178.     public function buildPersistenceRoleCopyStruct(APIRoleCopyStruct $roleCopyStructint $clonedIdint $status): SPIRoleCopyStruct
  179.     {
  180.         $policiesToCopy $this->fillRoleStructWithPolicies($roleCopyStruct);
  181.         return new SPIRoleCopyStruct(
  182.             [
  183.                 'clonedId' => $clonedId,
  184.                 'newIdentifier' => $roleCopyStruct->newIdentifier,
  185.                 'status' => $status,
  186.                 'policies' => $policiesToCopy,
  187.             ]
  188.         );
  189.     }
  190.     protected function fillRoleStructWithPolicies(APIRoleCreateStruct $struct): array
  191.     {
  192.         $policies = [];
  193.         foreach ($struct->getPolicies() as $policyStruct) {
  194.             $policies[] = $this->buildPersistencePolicyObject(
  195.                 $policyStruct->module,
  196.                 $policyStruct->function,
  197.                 $policyStruct->getLimitations()
  198.             );
  199.         }
  200.         return $policies;
  201.     }
  202.     /**
  203.      * Creates SPI Policy value object from provided module, function and limitations.
  204.      *
  205.      * @param string $module
  206.      * @param string $function
  207.      * @param \eZ\Publish\API\Repository\Values\User\Limitation[] $limitations
  208.      *
  209.      * @return \eZ\Publish\SPI\Persistence\User\Policy
  210.      */
  211.     public function buildPersistencePolicyObject($module$function, array $limitations)
  212.     {
  213.         $limitationsToCreate '*';
  214.         if ($module !== '*' && $function !== '*' && !empty($limitations)) {
  215.             $limitationsToCreate = [];
  216.             foreach ($limitations as $limitation) {
  217.                 $limitationsToCreate[$limitation->getIdentifier()] = $limitation->limitationValues;
  218.             }
  219.         }
  220.         return new SPIPolicy(
  221.             [
  222.                 'module' => $module,
  223.                 'function' => $function,
  224.                 'limitations' => $limitationsToCreate,
  225.             ]
  226.         );
  227.     }
  228. }