-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added UsersWithPermissionInfoToContentItemMapper
- Loading branch information
Showing
1 changed file
with
122 additions
and
0 deletions.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
src/lib/User/Mapper/UsersWithPermissionInfoToContentItemMapper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
|
||
namespace Ibexa\AdminUi\User\Mapper; | ||
|
||
use Ibexa\Contracts\Core\Repository\PermissionResolver; | ||
use Ibexa\Contracts\Core\Repository\UserService; | ||
use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo; | ||
use Ibexa\Contracts\Core\Repository\Values\Content\Search\SearchResult; | ||
use Ibexa\Contracts\Core\Repository\Values\User\User; | ||
use Ibexa\Core\Repository\Values\User\UserReference; | ||
|
||
/** | ||
* @phpstan-type TUserData array{ | ||
* access: array{ | ||
* name: string, | ||
* email: string, | ||
* }|array{}, | ||
* no_access: array{ | ||
* name: string, | ||
* email: string, | ||
* }|array{}, | ||
* } | ||
*/ | ||
final class UsersWithPermissionInfoToContentItemMapper | ||
{ | ||
private PermissionResolver $permissionResolver; | ||
|
||
private UserService $userService; | ||
|
||
public function __construct( | ||
UserService $userService, | ||
PermissionResolver $permissionResolver | ||
) { | ||
$this->userService = $userService; | ||
$this->permissionResolver = $permissionResolver; | ||
} | ||
|
||
/** | ||
* @param array<\Ibexa\Contracts\Core\Repository\Values\ValueObject> $targets | ||
* | ||
* @phpstan-return TUserData | ||
* | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException | ||
*/ | ||
public function mapSearchResults( | ||
SearchResult $searchResult, | ||
ContentInfo $contentInfo, | ||
string $module, | ||
string $function, | ||
array $targets = [] | ||
): array { | ||
$currentUserReference = $this->permissionResolver->getCurrentUserReference(); | ||
|
||
$results = $this->groupByPermissions($searchResult, $contentInfo, $module, $function, $targets); | ||
|
||
$this->permissionResolver->setCurrentUserReference($currentUserReference); | ||
|
||
return $results; | ||
} | ||
|
||
/** | ||
* @param array<\Ibexa\Contracts\Core\Repository\Values\ValueObject> $targets | ||
* | ||
* @phpstan-return TUserData | ||
* | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException | ||
*/ | ||
private function groupByPermissions( | ||
SearchResult $searchResult, | ||
ContentInfo $contentInfo, | ||
string $module, | ||
string $function, | ||
array $targets = [] | ||
): array { | ||
$results = [ | ||
'access' => [], | ||
'no_access' => [], | ||
]; | ||
|
||
foreach ($searchResult as $result) { | ||
/** @var \Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo $userContentInfo */ | ||
$userContentInfo = $result->valueObject; | ||
|
||
$user = $this->userService->loadUser($userContentInfo->getId()); | ||
$userReference = new UserReference($user->getUserId()); | ||
$userData = $this->getUserData($user); | ||
|
||
$this->permissionResolver->setCurrentUserReference($userReference); | ||
|
||
if ($this->permissionResolver->canUser($module, $function, $contentInfo, $targets)) { | ||
$results['access'][] = $userData; | ||
} else { | ||
$results['no_access'][] = $userData; | ||
} | ||
} | ||
|
||
return $results; | ||
} | ||
|
||
/** | ||
* @return array{ | ||
* name: string, | ||
* email: string, | ||
* } | ||
*/ | ||
private function getUserData(User $user): array | ||
{ | ||
return [ | ||
'name' => $user->getName() ?? $user->getLogin(), | ||
'email' => $user->email, | ||
]; | ||
} | ||
} |