Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[User Management] Add Dependencies to User. #556

Merged
merged 4 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions config/users.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ services:
Pimcore\Bundle\StudioBackendBundle\User\Service\UserInformationServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\User\Service\UserInformationService

Pimcore\Bundle\StudioBackendBundle\User\Service\ObjectDependenciesServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\User\Service\ObjectDependenciesService


Pimcore\Bundle\StudioBackendBundle\User\Service\MailServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\User\Service\MailService
arguments: [ '@Pimcore\Bundle\StudioBackendBundle\Setting\Provider\SystemSettingsProvider' ]
Expand All @@ -66,6 +70,9 @@ services:
Pimcore\Bundle\StudioBackendBundle\User\Hydrator\SimpleUserHydratorInterface:
class: Pimcore\Bundle\StudioBackendBundle\User\Hydrator\SimpleUserHydrator

Pimcore\Bundle\StudioBackendBundle\User\Hydrator\DependencyHydratorInterface:
class: Pimcore\Bundle\StudioBackendBundle\User\Hydrator\DependencyHydrator


#
# Repositories
Expand Down
35 changes: 35 additions & 0 deletions src/User/Hydrator/DependencyHydrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\User\Hydrator;

use Pimcore\Bundle\StudioBackendBundle\User\Schema\Dependency;
use Pimcore\Model\DataObject\Concrete;

/**
* @internal
*/
final class DependencyHydrator implements DependencyHydratorInterface
{
public function hydrate(Concrete $concrete): Dependency
{
return new Dependency(
id: $concrete->getId(),
path: $concrete->getRealFullPath(),
subtype: $concrete->getClass()->getName(),
);
}
}
28 changes: 28 additions & 0 deletions src/User/Hydrator/DependencyHydratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\User\Hydrator;

use Pimcore\Bundle\StudioBackendBundle\User\Schema\Dependency;
use Pimcore\Model\DataObject\Concrete;

/**
* @internal
*/
interface DependencyHydratorInterface
{
public function hydrate(Concrete $concrete): Dependency;
}
3 changes: 3 additions & 0 deletions src/User/Hydrator/UserHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Pimcore\Bundle\StaticResolverBundle\Lib\Tools\AdminResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\User\Schema\KeyBinding;
use Pimcore\Bundle\StudioBackendBundle\User\Schema\User as UserSchema;
use Pimcore\Bundle\StudioBackendBundle\User\Service\ObjectDependenciesServiceInterface;
use Pimcore\Model\User;
use Pimcore\Model\UserInterface;
use Psr\Log\LoggerInterface;
Expand All @@ -35,6 +36,7 @@ public function __construct(
private ToolResolverInterface $toolResolver,
private AdminResolverInterface $adminToolResolver,
private WorkspaceHydratorInterface $workspaceHydrator,
private ObjectDependenciesServiceInterface $objectDependenciesService
) {
}

Expand Down Expand Up @@ -68,6 +70,7 @@ classes: $user->getClasses(),
assetWorkspaces: $this->workspaceHydrator->hydrateAssetWorkspace($user),
dataObjectWorkspaces: $this->workspaceHydrator->hydrateDataObjectWorkspace($user),
documentWorkspaces: $this->workspaceHydrator->hydrateDocumentWorkspace($user),
objectDependencies: $this->objectDependenciesService->getDependenciesForUser($user),
);
}

Expand Down
58 changes: 58 additions & 0 deletions src/User/Schema/Dependency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\User\Schema;

use OpenApi\Attributes\Property;
use OpenApi\Attributes\Schema;

/**
* @internal
*/
#[Schema(
schema: 'UserDependency',
title: 'Dependency to an Object',
description: 'Dependency to an Object',
required: ['id', 'path', 'subtype'],
type: 'object'
)]
final readonly class Dependency
{
public function __construct(
#[Property(description: 'ID of the object', type: 'integer', example: 42)]
private int $id,
#[Property(description: 'Path to the object', type: 'string', example: '/path/to/object')]
private string $path,
#[Property(description: 'Subtype of the object', type: 'string', example: 'Car')]
private string $subtype,
) {
}

public function getId(): int
{
return $this->id;
}

public function getPath(): string
{
return $this->path;
}

public function getSubtype(): string
{
return $this->subtype;
}
}
51 changes: 51 additions & 0 deletions src/User/Schema/ObjectDependencies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\User\Schema;

use OpenApi\Attributes\Items;
use OpenApi\Attributes\Property;
use OpenApi\Attributes\Schema;

/**
* @internal
*/
#[Schema(
title: 'User Object Dependencies',
description: 'User Object Dependencies',
required: ['hasHidden', 'dependencies'],
type: 'object',
)]
final readonly class ObjectDependencies
{
public function __construct(
#[Property(description: 'Dependencies to objects', type: 'array', items: new Items(ref: Dependency::class))]
private array $dependencies,
#[Property(description: 'If is has hidden dependencies', type: 'boolean', example: true)]
private bool $hasHidden
) {
}

public function getDependencies(): array
{
return $this->dependencies;
}

public function isHasHidden(): bool
{
return $this->hasHidden;
}
}
9 changes: 8 additions & 1 deletion src/User/Schema/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
'id', 'active', 'admin', 'classes', 'closeWarning', 'allowDirtyClose', 'contentLanguages', 'hasImage',
'keyBindings', 'language', 'memorizeTabs', 'parentId', 'permissions', 'roles',
'twoFactorAuthenticationEnabled', 'websiteTranslationLanguagesEdit', 'websiteTranslationLanguagesView',
'welcomeScreen', 'assetWorkspaces', 'dataObjectWorkspaces', 'documentWorkspaces',
'welcomeScreen', 'assetWorkspaces', 'dataObjectWorkspaces', 'documentWorkspaces', 'objectDependencies',
],
type: 'object'
)]
Expand Down Expand Up @@ -94,6 +94,8 @@ public function __construct(
private readonly array $dataObjectWorkspaces,
#[Property(description: 'Document Workspace', type: 'array', items: new Items(ref: UserWorkspace::class))]
private readonly array $documentWorkspaces,
#[Property(ref: ObjectDependencies::class, description: 'Object Dependencies', type: 'object')]
private readonly ObjectDependencies $objectDependencies,
) {
}

Expand Down Expand Up @@ -238,4 +240,9 @@ public function getDocumentWorkspaces(): array
{
return $this->documentWorkspaces;
}

public function getObjectDependencies(): ObjectDependencies
{
return $this->objectDependencies;
}
}
54 changes: 54 additions & 0 deletions src/User/Service/ObjectDependenciesService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\User\Service;

use Pimcore\Bundle\StaticResolverBundle\Models\DataObject\DataObjectServiceResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\User\Hydrator\DependencyHydratorInterface;
use Pimcore\Bundle\StudioBackendBundle\User\Schema\ObjectDependencies;
use Pimcore\Model\UserInterface;

/**
* @internal
*/
final readonly class ObjectDependenciesService implements ObjectDependenciesServiceInterface
{
public function __construct(
private DataObjectServiceResolverInterface $dataObjectServiceResolver,
private DependencyHydratorInterface $dependencyHydrator
) {
}

public function getDependenciesForUser(UserInterface $user): ObjectDependencies
{
$dependencies = [];
$hasHidden = false;

$objects = $this->dataObjectServiceResolver->getObjectsReferencingUser($user->getId());

foreach ($objects as $object) {
if ($object->isAllowed('list')) {
$dependencies[] = $this->dependencyHydrator->hydrate($object);

continue;
}

$hasHidden = true;
}

return new ObjectDependencies($dependencies, $hasHidden);
}
}
28 changes: 28 additions & 0 deletions src/User/Service/ObjectDependenciesServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\User\Service;

use Pimcore\Bundle\StudioBackendBundle\User\Schema\ObjectDependencies;
use Pimcore\Model\UserInterface;

/**
* @internal
*/
interface ObjectDependenciesServiceInterface
{
public function getDependenciesForUser(UserInterface $user): ObjectDependencies;
}
52 changes: 52 additions & 0 deletions tests/Unit/User/Service/ObjectDependenciesServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Tests\Unit\User\Service;

use Codeception\Test\Unit;
use Pimcore\Bundle\StaticResolverBundle\Models\DataObject\DataObjectServiceResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\User\Hydrator\DependencyHydratorInterface;
use Pimcore\Bundle\StudioBackendBundle\User\Service\ObjectDependenciesService;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\UserInterface;

/**
* @internal
*/
final class ObjectDependenciesServiceTest extends Unit
{
public function testIfHiddenIsSet(): void
{
$demoObject = $this->makeEmpty(Concrete::class, [
'isAllowed' => false,
]);

$dataObjectServiceResolver = $this->makeEmpty(DataObjectServiceResolverInterface::class, [
'getObjectsReferencingUser' => [$demoObject],
]);
$dependencyHydrator = $this->makeEmpty(DependencyHydratorInterface::class);

$objectDependenciesService = new ObjectDependenciesService($dataObjectServiceResolver, $dependencyHydrator);

$user = $this->makeEmpty(UserInterface::class, [
'getId' => 1,
]);

$objectDependencies = $objectDependenciesService->getDependenciesForUser($user);

$this->assertTrue($objectDependencies->isHasHidden());
}
}
Loading