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