| @@ 794-839 (lines=46) @@ | ||
| 791 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited |
|
| 792 | * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
|
| 793 | */ |
|
| 794 | public function testCanUserWithTargetNo() |
|
| 795 | { |
|
| 796 | $repository = $this->getRepository(); |
|
| 797 | ||
| 798 | $homeLocationId = $this->generateId('location', 2); |
|
| 799 | ||
| 800 | /* BEGIN: Use Case */ |
|
| 801 | // $homeLocationId contains the ID of the "Home" frontpage location |
|
| 802 | ||
| 803 | $user = $this->createUserVersion1(); |
|
| 804 | ||
| 805 | // Set created user as current user |
|
| 806 | $repository->setCurrentUser($user); |
|
| 807 | ||
| 808 | $contentTypeService = $repository->getContentTypeService(); |
|
| 809 | ||
| 810 | $contentType = $contentTypeService->loadContentTypeByIdentifier('forum'); |
|
| 811 | ||
| 812 | $contentService = $repository->getContentService(); |
|
| 813 | ||
| 814 | $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US'); |
|
| 815 | $contentCreateStruct->setField('name', 'My awesome forum'); |
|
| 816 | $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789'; |
|
| 817 | $contentCreateStruct->alwaysAvailable = true; |
|
| 818 | ||
| 819 | $locationService = $repository->getLocationService(); |
|
| 820 | $locationCreateStruct = $locationService->newLocationCreateStruct($homeLocationId); |
|
| 821 | ||
| 822 | // This call will return false because user with Editor role has permission to |
|
| 823 | // create "forum" type content only under "folder" type content. |
|
| 824 | $canUser = $repository->canUser( |
|
| 825 | 'content', |
|
| 826 | 'create', |
|
| 827 | $contentCreateStruct, |
|
| 828 | $locationCreateStruct |
|
| 829 | ); |
|
| 830 | ||
| 831 | // Performing an action without necessary permissions will fail with "UnauthorizedException" |
|
| 832 | if (!$canUser) { |
|
| 833 | $contentDraft = $contentService->createContent( |
|
| 834 | $contentCreateStruct, |
|
| 835 | array($locationCreateStruct) |
|
| 836 | ); |
|
| 837 | } |
|
| 838 | /* END: Use Case */ |
|
| 839 | } |
|
| 840 | ||
| 841 | /** |
|
| 842 | * Test for the canUser() method. |
|
| @@ 530-577 (lines=48) @@ | ||
| 527 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessLimited |
|
| 528 | * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
|
| 529 | */ |
|
| 530 | public function testCanUserWithTargetNo() |
|
| 531 | { |
|
| 532 | $repository = $this->getRepository(); |
|
| 533 | ||
| 534 | $homeLocationId = $this->generateId('location', 2); |
|
| 535 | ||
| 536 | /* BEGIN: Use Case */ |
|
| 537 | // $homeLocationId contains the ID of the "Home" frontpage location |
|
| 538 | ||
| 539 | $user = $this->createUserVersion1(); |
|
| 540 | ||
| 541 | $permissionResolver = $repository->getPermissionResolver(); |
|
| 542 | ||
| 543 | // Set created user as current user reference |
|
| 544 | $permissionResolver->setCurrentUserReference($user); |
|
| 545 | ||
| 546 | $contentTypeService = $repository->getContentTypeService(); |
|
| 547 | ||
| 548 | $contentType = $contentTypeService->loadContentTypeByIdentifier('forum'); |
|
| 549 | ||
| 550 | $contentService = $repository->getContentService(); |
|
| 551 | ||
| 552 | $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US'); |
|
| 553 | $contentCreateStruct->setField('name', 'My awesome forum'); |
|
| 554 | $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789'; |
|
| 555 | $contentCreateStruct->alwaysAvailable = true; |
|
| 556 | ||
| 557 | $locationService = $repository->getLocationService(); |
|
| 558 | $locationCreateStruct = $locationService->newLocationCreateStruct($homeLocationId); |
|
| 559 | ||
| 560 | // This call will return false because user with Editor role has permission to |
|
| 561 | // create "forum" type content only under "folder" type content. |
|
| 562 | $canUser = $permissionResolver->canUser( |
|
| 563 | 'content', |
|
| 564 | 'create', |
|
| 565 | $contentCreateStruct, |
|
| 566 | [$locationCreateStruct] |
|
| 567 | ); |
|
| 568 | ||
| 569 | // Performing an action without necessary permissions will fail with "UnauthorizedException" |
|
| 570 | if (!$canUser) { |
|
| 571 | $contentDraft = $contentService->createContent( |
|
| 572 | $contentCreateStruct, |
|
| 573 | array($locationCreateStruct) |
|
| 574 | ); |
|
| 575 | } |
|
| 576 | /* END: Use Case */ |
|
| 577 | } |
|
| 578 | ||
| 579 | /** |
|
| 580 | * Test for the canUser() method. |
|