| @@ 529-576 (lines=48) @@ | ||
| 526 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessLimited |
|
| 527 | * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
|
| 528 | */ |
|
| 529 | public function testCanUserWithTargetNo() |
|
| 530 | { |
|
| 531 | $repository = $this->getRepository(); |
|
| 532 | ||
| 533 | $homeLocationId = $this->generateId('location', 2); |
|
| 534 | ||
| 535 | /* BEGIN: Use Case */ |
|
| 536 | // $homeLocationId contains the ID of the "Home" frontpage location |
|
| 537 | ||
| 538 | $user = $this->createUserVersion1(); |
|
| 539 | ||
| 540 | $permissionResolver = $repository->getPermissionResolver(); |
|
| 541 | ||
| 542 | // Set created user as current user reference |
|
| 543 | $permissionResolver->setCurrentUserReference($user); |
|
| 544 | ||
| 545 | $contentTypeService = $repository->getContentTypeService(); |
|
| 546 | ||
| 547 | $contentType = $contentTypeService->loadContentTypeByIdentifier('forum'); |
|
| 548 | ||
| 549 | $contentService = $repository->getContentService(); |
|
| 550 | ||
| 551 | $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US'); |
|
| 552 | $contentCreateStruct->setField('name', 'My awesome forum'); |
|
| 553 | $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789'; |
|
| 554 | $contentCreateStruct->alwaysAvailable = true; |
|
| 555 | ||
| 556 | $locationService = $repository->getLocationService(); |
|
| 557 | $locationCreateStruct = $locationService->newLocationCreateStruct($homeLocationId); |
|
| 558 | ||
| 559 | // This call will return false because user with Editor role has permission to |
|
| 560 | // create "forum" type content only under "folder" type content. |
|
| 561 | $canUser = $permissionResolver->canUser( |
|
| 562 | 'content', |
|
| 563 | 'create', |
|
| 564 | $contentCreateStruct, |
|
| 565 | [$locationCreateStruct] |
|
| 566 | ); |
|
| 567 | ||
| 568 | // Performing an action without necessary permissions will fail with "UnauthorizedException" |
|
| 569 | if (!$canUser) { |
|
| 570 | $contentDraft = $contentService->createContent( |
|
| 571 | $contentCreateStruct, |
|
| 572 | array($locationCreateStruct) |
|
| 573 | ); |
|
| 574 | } |
|
| 575 | /* END: Use Case */ |
|
| 576 | } |
|
| 577 | ||
| 578 | /** |
|
| 579 | * Test for the canUser() method. |
|
| @@ 820-865 (lines=46) @@ | ||
| 817 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited |
|
| 818 | * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
|
| 819 | */ |
|
| 820 | public function testCanUserWithTargetNo() |
|
| 821 | { |
|
| 822 | $repository = $this->getRepository(); |
|
| 823 | ||
| 824 | $homeLocationId = $this->generateId('location', 2); |
|
| 825 | ||
| 826 | /* BEGIN: Use Case */ |
|
| 827 | // $homeLocationId contains the ID of the "Home" frontpage location |
|
| 828 | ||
| 829 | $user = $this->createUserVersion1(); |
|
| 830 | ||
| 831 | // Set created user as current user |
|
| 832 | $repository->setCurrentUser($user); |
|
| 833 | ||
| 834 | $contentTypeService = $repository->getContentTypeService(); |
|
| 835 | ||
| 836 | $contentType = $contentTypeService->loadContentTypeByIdentifier('forum'); |
|
| 837 | ||
| 838 | $contentService = $repository->getContentService(); |
|
| 839 | ||
| 840 | $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US'); |
|
| 841 | $contentCreateStruct->setField('name', 'My awesome forum'); |
|
| 842 | $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789'; |
|
| 843 | $contentCreateStruct->alwaysAvailable = true; |
|
| 844 | ||
| 845 | $locationService = $repository->getLocationService(); |
|
| 846 | $locationCreateStruct = $locationService->newLocationCreateStruct($homeLocationId); |
|
| 847 | ||
| 848 | // This call will return false because user with Editor role has permission to |
|
| 849 | // create "forum" type content only under "folder" type content. |
|
| 850 | $canUser = $repository->canUser( |
|
| 851 | 'content', |
|
| 852 | 'create', |
|
| 853 | $contentCreateStruct, |
|
| 854 | $locationCreateStruct |
|
| 855 | ); |
|
| 856 | ||
| 857 | // Performing an action without necessary permissions will fail with "UnauthorizedException" |
|
| 858 | if (!$canUser) { |
|
| 859 | $contentDraft = $contentService->createContent( |
|
| 860 | $contentCreateStruct, |
|
| 861 | array($locationCreateStruct) |
|
| 862 | ); |
|
| 863 | } |
|
| 864 | /* END: Use Case */ |
|
| 865 | } |
|
| 866 | ||
| 867 | /** |
|
| 868 | * Test for the canUser() method. |
|