diff --git a/src/ContextProvider/RoleIdentify.php b/src/ContextProvider/RoleIdentify.php index 82795497..2dc81dfc 100644 --- a/src/ContextProvider/RoleIdentify.php +++ b/src/ContextProvider/RoleIdentify.php @@ -6,7 +6,9 @@ */ namespace EzSystems\PlatformHttpCacheBundle\ContextProvider; +use eZ\Publish\API\Repository\PermissionResolver; use eZ\Publish\API\Repository\Repository; +use eZ\Publish\API\Repository\UserService; use FOS\HttpCache\UserContext\ContextProvider; use FOS\HttpCache\UserContext\UserContext; @@ -22,19 +24,31 @@ */ class RoleIdentify implements ContextProvider { - /** - * @var \eZ\Publish\Core\Repository\Repository - */ + /** @var \eZ\Publish\Core\Repository\Repository */ protected $repository; - public function __construct(Repository $repository) - { + /** @var \eZ\Publish\API\Repository\PermissionResolver */ + private $permissionResolver; + + /** @var \eZ\Publish\API\Repository\UserService */ + private $userService; + + public function __construct( + Repository $repository, + PermissionResolver $permissionResolver, + UserService $userService + ) { $this->repository = $repository; + $this->permissionResolver = $permissionResolver; + $this->userService = $userService; } public function updateUserContext(UserContext $context) { - $user = $this->repository->getCurrentUser(); + $user = $this->userService->loadUser( + $this->permissionResolver->getCurrentUserReference()->getUserId() + ); + /** @var \eZ\Publish\API\Repository\Values\User\RoleAssignment[] $roleAssignments */ $roleAssignments = $this->repository->sudo( function (Repository $repository) use ($user) { @@ -42,8 +56,8 @@ function (Repository $repository) use ($user) { } ); - $roleIds = array(); - $limitationValues = array(); + $roleIds = []; + $limitationValues = []; /** @var \eZ\Publish\API\Repository\Values\User\UserRoleAssignment $roleAssignment */ foreach ($roleAssignments as $roleAssignment) { $roleId = $roleAssignment->getRole()->id; @@ -52,7 +66,7 @@ function (Repository $repository) use ($user) { // If a limitation is present, store the limitation values by roleId if ($limitation !== null) { $limitationValuesKey = sprintf('%s-%s', $roleId, $limitation->getIdentifier()); - $limitationValues[$limitationValuesKey] = array(); + $limitationValues[$limitationValuesKey] = []; foreach ($limitation->limitationValues as $value) { $limitationValues[$limitationValuesKey][] = $value; } diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index 8fba2501..9a8606f2 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -61,7 +61,10 @@ services: ezplatform.http_cache.user_context_provider.role_identify: class: EzSystems\PlatformHttpCacheBundle\ContextProvider\RoleIdentify - arguments: ["@ezpublish.api.repository"] + arguments: + - '@ezpublish.api.repository' + - '@eZ\Publish\API\Repository\PermissionResolver' + - '@ezpublish.api.service.user' tags: - { name: fos_http_cache.user_context_provider } diff --git a/tests/ContextProvider/RoleIdentifyTest.php b/tests/ContextProvider/RoleIdentifyTest.php index 69ccd204..c40c2f22 100644 --- a/tests/ContextProvider/RoleIdentifyTest.php +++ b/tests/ContextProvider/RoleIdentifyTest.php @@ -7,14 +7,15 @@ namespace EzSystems\PlatformHttpCacheBundle\Tests\ContextProvider; use eZ\Publish\API\Repository\RoleService; +use eZ\Publish\API\Repository\UserService; use eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation; use eZ\Publish\API\Repository\Values\User\Role; use eZ\Publish\API\Repository\Values\User\User as APIUser; use eZ\Publish\API\Repository\Values\User\UserReference; -use eZ\Publish\Core\Repository\Repository; use eZ\Publish\Core\Repository\Helper\LimitationService; use eZ\Publish\Core\Repository\Helper\RoleDomainMapper; use eZ\Publish\Core\Repository\Permission\PermissionResolver; +use eZ\Publish\Core\Repository\Repository; use eZ\Publish\Core\Repository\Values\User\UserRoleAssignment; use eZ\Publish\SPI\Persistence\User\Handler as SPIUserHandler; use EzSystems\PlatformHttpCacheBundle\ContextProvider\RoleIdentify; @@ -27,22 +28,22 @@ class RoleIdentifyTest extends TestCase { /** - * @var \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\API\Repository\Repository + * @var \eZ\Publish\API\Repository\Repository|\PHPUnit_Framework_MockObject_MockObject */ private $repositoryMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\API\Repository\RoleService + * @var \eZ\Publish\API\Repository\RoleService|\PHPUnit_Framework_MockObject_MockObject */ private $roleServiceMock; - protected function setUp() + protected function setUp(): void { parent::setUp(); $this->repositoryMock = $this ->getMockBuilder(Repository::class) ->disableOriginalConstructor() - ->setMethods(array('getRoleService', 'getCurrentUser', 'getPermissionResolver')) + ->setMethods(['getRoleService', 'getPermissionResolver']) ->getMock(); $this->roleServiceMock = $this->createMock(RoleService::class); @@ -51,66 +52,73 @@ protected function setUp() ->expects($this->any()) ->method('getRoleService') ->willReturn($this->roleServiceMock); - $this->repositoryMock - ->expects($this->any()) - ->method('getPermissionResolver') - ->willReturn($this->getPermissionResolverMock()); } public function testSetIdentity() { $user = $this->createMock(APIUser::class); + $userReference = $this->createMock(UserReference::class); $userContext = new UserContext(); + $permissionResolver = $this->getPermissionResolverMock(); + $permissionResolver + ->method('getCurrentUserReference') + ->willReturn($userReference); + $this->repositoryMock - ->expects($this->once()) - ->method('getCurrentUser') + ->expects($this->any()) + ->method('getPermissionResolver') + ->willReturn($permissionResolver); + + $userService = $this->getUserServiceMock(); + $userService + ->method('loadUser') ->willReturn($user); $roleId1 = 123; $roleId2 = 456; $roleId3 = 789; $limitationForRole2 = $this->generateLimitationMock( - array( - 'limitationValues' => array('/1/2', '/1/2/43'), - ) + [ + 'limitationValues' => ['/1/2', '/1/2/43'], + ] ); $limitationForRole3 = $this->generateLimitationMock( - array( - 'limitationValues' => array('foo', 'bar'), - ) + [ + 'limitationValues' => ['foo', 'bar'], + ] ); - $returnedRoleAssignments = array( + $returnedRoleAssignments = [ $this->generateRoleAssignmentMock( - array( + [ 'role' => $this->generateRoleMock( - array( + [ 'id' => $roleId1, - ) + ] ), - ) + ] ), $this->generateRoleAssignmentMock( - array( + [ 'role' => $this->generateRoleMock( - array( + [ 'id' => $roleId2, - ) + ] ), 'limitation' => $limitationForRole2, - ) + ] ), $this->generateRoleAssignmentMock( - array( + [ 'role' => $this->generateRoleMock( - array( + [ 'id' => $roleId3, - ) + ] ), 'limitation' => $limitationForRole3, - ) + ] ), - ); + ]; $this->roleServiceMock ->expects($this->once()) @@ -118,45 +126,49 @@ public function testSetIdentity() ->with($user, true) ->willReturn($returnedRoleAssignments); - $this->assertSame(array(), $userContext->getParameters()); - $contextProvider = new RoleIdentify($this->repositoryMock); + $this->assertSame([], $userContext->getParameters()); + $contextProvider = new RoleIdentify( + $this->repositoryMock, + $permissionResolver, + $userService + ); $contextProvider->updateUserContext($userContext); $userContextParams = $userContext->getParameters(); $this->assertArrayHasKey('roleIdList', $userContextParams); - $this->assertSame(array($roleId1, $roleId2, $roleId3), $userContextParams['roleIdList']); + $this->assertSame([$roleId1, $roleId2, $roleId3], $userContextParams['roleIdList']); $this->assertArrayHasKey('roleLimitationList', $userContextParams); $limitationIdentifierForRole2 = \get_class($limitationForRole2); $limitationIdentifierForRole3 = \get_class($limitationForRole3); $this->assertSame( - array( - "$roleId2-$limitationIdentifierForRole2" => array('/1/2', '/1/2/43'), - "$roleId3-$limitationIdentifierForRole3" => array('foo', 'bar'), - ), + [ + "$roleId2-$limitationIdentifierForRole2" => ['/1/2', '/1/2/43'], + "$roleId3-$limitationIdentifierForRole3" => ['foo', 'bar'], + ], $userContextParams['roleLimitationList'] ); } - private function generateRoleAssignmentMock(array $properties = array()) + private function generateRoleAssignmentMock(array $properties = []) { return $this ->getMockBuilder(UserRoleAssignment::class) - ->setConstructorArgs(array($properties)) + ->setConstructorArgs([$properties]) ->getMockForAbstractClass(); } - private function generateRoleMock(array $properties = array()) + private function generateRoleMock(array $properties = []) { return $this ->getMockBuilder(Role::class) - ->setConstructorArgs(array($properties)) + ->setConstructorArgs([$properties]) ->getMockForAbstractClass(); } - private function generateLimitationMock(array $properties = array()) + private function generateLimitationMock(array $properties = []) { $limitationMock = $this ->getMockBuilder(RoleLimitation::class) - ->setConstructorArgs(array($properties)) + ->setConstructorArgs([$properties]) ->getMockForAbstractClass(); $limitationMock ->expects($this->any()) @@ -170,7 +182,7 @@ protected function getPermissionResolverMock() { return $this ->getMockBuilder(PermissionResolver::class) - ->setMethods(null) + ->setMethods(['getCurrentUserReference']) ->setConstructorArgs( [ $this->createMock(RoleDomainMapper::class), @@ -181,4 +193,11 @@ protected function getPermissionResolverMock() ) ->getMock(); } + + protected function getUserServiceMock() + { + return $this + ->getMockBuilder(UserService::class) + ->getMockForAbstractClass(); + } }