Skip to content

Commit

Permalink
fixup! IBX-6645: Added user profile page
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwojs committed Dec 9, 2023
1 parent f10073f commit 15655f3
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 34 deletions.
137 changes: 137 additions & 0 deletions src/bundle/Controller/User/ProfileEditController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Bundle\AdminUi\Controller\User;

use Ibexa\AdminUi\Specification\UserProfile\IsProfileAvailable;
use Ibexa\AdminUi\UserProfile\UserProfileConfigurationInterface;
use Ibexa\ContentForms\Data\Mapper\UserUpdateMapper;
use Ibexa\ContentForms\Form\ActionDispatcher\ActionDispatcherInterface;
use Ibexa\ContentForms\Form\Type\User\UserUpdateType;
use Ibexa\ContentForms\User\View\UserUpdateView;
use Ibexa\Contracts\AdminUi\Controller\Controller;
use Ibexa\Contracts\ContentForms\Content\Form\Provider\GroupedContentFormFieldsProviderInterface;
use Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException;
use Ibexa\Contracts\Core\Repository\LanguageService;
use Ibexa\Contracts\Core\Repository\LocationService;
use Ibexa\Contracts\Core\Repository\PermissionResolver;
use Ibexa\Contracts\Core\Repository\Repository;
use Ibexa\Contracts\Core\Repository\UserService;
use Ibexa\Contracts\Core\Repository\Values\Content\Field;
use Ibexa\Contracts\Core\Repository\Values\User\User;
use Ibexa\Core\Base\Exceptions\UnauthorizedException as CoreUnauthorizedException;
use Symfony\Component\HttpFoundation\Request;

final class ProfileEditController extends Controller
{
private Repository $repository;

private UserService $userService;

private LocationService $locationService;

private UserProfileConfigurationInterface $configuration;

private PermissionResolver $permissionResolver;

private LanguageService $languageService;

private ActionDispatcherInterface $userActionDispatcher;

private GroupedContentFormFieldsProviderInterface $groupedContentFormFieldsProvider;

public function __construct(
Repository $repository,
UserService $userService,
LocationService $locationService,
UserProfileConfigurationInterface $configuration,
PermissionResolver $permissionResolver,
LanguageService $languageService,
ActionDispatcherInterface $userActionDispatcher,
GroupedContentFormFieldsProviderInterface $groupedContentFormFieldsProvider
) {
$this->repository = $repository;
$this->userService = $userService;
$this->locationService = $locationService;
$this->configuration = $configuration;
$this->permissionResolver = $permissionResolver;
$this->languageService = $languageService;
$this->userActionDispatcher = $userActionDispatcher;
$this->groupedContentFormFieldsProvider = $groupedContentFormFieldsProvider;
}

/**
* @return \Ibexa\ContentForms\User\View\UserUpdateView|\Symfony\Component\HttpFoundation\Response
*/
public function editAction(Request $request, int $userId, string $languageCode = 'eng-GB')
{
$user = $this->userService->loadUser($this->permissionResolver->getCurrentUserReference()->getUserId());
if (!$this->isUserProfileAvailable($user)) {
throw $this->createNotFoundException();
}

if (!$this->permissionResolver->canUser('user', 'selfedit', $user)) {
throw new CoreUnauthorizedException('user', 'selfedit', ['userId' => $user->getUserId()]);
}

$userUpdate = (new UserUpdateMapper())->mapToFormData($user, $user->getContentType(), [
'languageCode' => $languageCode,
'filter' => static fn (Field $field): bool => $field->fieldTypeIdentifier !== 'ezuser',
]);

$form = $this->createForm(
UserUpdateType::class,
$userUpdate,
[
'languageCode' => $languageCode,
'mainLanguageCode' => $user->contentInfo->mainLanguageCode,
]
);

$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid() && null !== $form->getClickedButton()) {

Check failure on line 97 in src/bundle/Controller/User/ProfileEditController.php

View workflow job for this annotation

GitHub Actions / Tests (7.4)

Call to an undefined method Symfony\Component\Form\FormInterface::getClickedButton().

Check failure on line 97 in src/bundle/Controller/User/ProfileEditController.php

View workflow job for this annotation

GitHub Actions / Tests (8.0)

Call to an undefined method Symfony\Component\Form\FormInterface::getClickedButton().

Check failure on line 97 in src/bundle/Controller/User/ProfileEditController.php

View workflow job for this annotation

GitHub Actions / Tests (8.1)

Call to an undefined method Symfony\Component\Form\FormInterface::getClickedButton().
$this->userActionDispatcher->dispatchFormAction($form, $userUpdate, $form->getClickedButton()->getName());

Check failure on line 98 in src/bundle/Controller/User/ProfileEditController.php

View workflow job for this annotation

GitHub Actions / Tests (7.4)

Call to an undefined method Symfony\Component\Form\FormInterface::getClickedButton().

Check failure on line 98 in src/bundle/Controller/User/ProfileEditController.php

View workflow job for this annotation

GitHub Actions / Tests (8.0)

Call to an undefined method Symfony\Component\Form\FormInterface::getClickedButton().

Check failure on line 98 in src/bundle/Controller/User/ProfileEditController.php

View workflow job for this annotation

GitHub Actions / Tests (8.1)

Call to an undefined method Symfony\Component\Form\FormInterface::getClickedButton().
if ($response = $this->userActionDispatcher->getResponse()) {
return $response;
}
}

$location = $this->repository->sudo(function () use ($user) {
return $this->locationService->loadLocation(
(int)$user->versionInfo->contentInfo->mainLocationId
);
});

$parentLocation = null;
try {
$parentLocation = $this->locationService->loadLocation($location->parentLocationId);
} catch (UnauthorizedException $e) {
}

return new UserUpdateView(
null,
[
'form' => $form->createView(),
'language_code' => $languageCode,
'language' => $this->languageService->loadLanguage($languageCode),
'content_type' => $user->getContentType(),
'user' => $user,
'location' => $location,
'parent_location' => $parentLocation,
'grouped_fields' => $this->groupedContentFormFieldsProvider->getGroupedFields(
$form->get('fieldsData')->all()
),
]
);
}

private function isUserProfileAvailable(User $user): bool
{
return (new IsProfileAvailable($this->configuration))->isSatisfiedBy($user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,78 +8,76 @@

namespace Ibexa\Bundle\AdminUi\Controller\User;

use Ibexa\AdminUi\Specification\UserProfile\IsProfileAvailable;
use Ibexa\AdminUi\UserProfile\UserProfileConfigurationInterface;
use Ibexa\Contracts\AdminUi\Controller\Controller;
use Ibexa\Contracts\Core\Repository\PermissionResolver;
use Ibexa\Contracts\Core\Repository\Repository;
use Ibexa\Contracts\Core\Repository\RoleService;
use Ibexa\Contracts\Core\Repository\UserService;
use Ibexa\Contracts\Core\Repository\Values\User\Role;
use Ibexa\Contracts\Core\Repository\Values\User\RoleAssignment;
use Ibexa\Contracts\Core\Repository\Values\User\User;
use Symfony\Component\HttpFoundation\Response;

final class ProfileController extends Controller
final class ProfileViewController extends Controller
{
private Repository $repository;

private UserService $userService;

private RoleService $roleService;

private UserProfileConfigurationInterface $configuration;

private PermissionResolver $permissionResolver;

Check failure on line 33 in src/bundle/Controller/User/ProfileViewController.php

View workflow job for this annotation

GitHub Actions / Tests (7.4)

Property Ibexa\Bundle\AdminUi\Controller\User\ProfileViewController::$permissionResolver is never read, only written.

Check failure on line 33 in src/bundle/Controller/User/ProfileViewController.php

View workflow job for this annotation

GitHub Actions / Tests (8.0)

Property Ibexa\Bundle\AdminUi\Controller\User\ProfileViewController::$permissionResolver is never read, only written.

Check failure on line 33 in src/bundle/Controller/User/ProfileViewController.php

View workflow job for this annotation

GitHub Actions / Tests (8.1)

Property Ibexa\Bundle\AdminUi\Controller\User\ProfileViewController::$permissionResolver is never read, only written.

public function __construct(
Repository $repository,
UserService $userService,
RoleService $roleService,
PermissionResolver $permissionResolver,
UserProfileConfigurationInterface $configuration
) {
$this->repository = $repository;
$this->userService = $userService;
$this->roleService = $roleService;
$this->permissionResolver = $permissionResolver;
$this->configuration = $configuration;
}

public function viewAction(int $userId): Response
{
$user = $this->userService->loadUser($userId);
if (!$this->isUserProfileEnabled($user)) {
if (!$this->isUserProfileAvailable($user)) {
throw $this->createNotFoundException();
}

$roles = $this->roleService->getRoleAssignmentsForUser($user, true);

return $this->render(
'@ibexadesign/account/profile/view.html.twig',
[
'user' => $user,
'roles' => $roles,
'roles' => $this->getUserRoles($user),
'field_groups' => $this->configuration->getFieldGroups(),
]
);
}

public function editAction(int $userId): Response
/**
* @return \Ibexa\Contracts\Core\Repository\Values\User\Role[]
*/
private function getUserRoles(User $user): iterable
{
$user = $this->userService->loadUser($userId);
if (!$this->isUserProfileEnabled($user)) {
throw $this->createNotFoundException();
}
/** @var \Ibexa\Contracts\Core\Repository\Values\User\RoleAssignment[] $roles */
$roles = $this->repository->sudo(function () use ($user): iterable {
return $this->roleService->getRoleAssignmentsForUser($user, true);
});

// TODO: Add language selection ?
return $this->redirectToRoute(
'ibexa.user.update',
[
'contentId' => $user->getUserId(),
'language' => $user->contentInfo->mainLanguageCode,
'versionNo' => $user->versionInfo->versionNo,
]
);
return array_map(static fn (RoleAssignment $assignment): Role => $assignment->getRole(), $roles);
}

private function isUserProfileEnabled(User $user): bool
private function isUserProfileAvailable(User $user): bool
{
if ($this->configuration->isEnabled()) {
return in_array(
$user->getContentType()->identifier,
$this->configuration->getContentTypes(),
true
);
}

return false;
return (new IsProfileAvailable($this->configuration))->isSatisfiedBy($user);
}
}
4 changes: 2 additions & 2 deletions src/bundle/Resources/config/routing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -678,14 +678,14 @@ ibexa.user.invite.to_group:

ibexa.user.profile.view:
path: /user/profile/{userId}/view
controller: Ibexa\Bundle\AdminUi\Controller\User\ProfileController::viewAction
controller: Ibexa\Bundle\AdminUi\Controller\User\ProfileViewController::viewAction
methods: ['GET']
options:
expose: true

ibexa.user.profile.edit:
path: /user/profile/{userId}/edit
controller: Ibexa\Bundle\AdminUi\Controller\User\ProfileController::editAction
controller: Ibexa\Bundle\AdminUi\Controller\User\ProfileEditController::editAction
methods: ['GET', 'POST']

#
Expand Down
8 changes: 7 additions & 1 deletion src/bundle/Resources/config/services/controllers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,13 @@ services:
tags:
- controller.service_arguments

Ibexa\Bundle\AdminUi\Controller\User\ProfileController:
Ibexa\Bundle\AdminUi\Controller\User\ProfileViewController:
parent: Ibexa\Contracts\AdminUi\Controller\Controller
autowire: true
tags:
- controller.service_arguments

Ibexa\Bundle\AdminUi\Controller\User\ProfileEditController:
parent: Ibexa\Contracts\AdminUi\Controller\Controller
autowire: true
tags:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
{% for role_assigment in roles %}
<div class="ibexa-tag">
<div class="ibexa-tag__content">
{{ role_assigment.role.identifier }}
{{ role_assigment.identifier }}
</div>
</div>
{% endfor %}
Expand All @@ -87,7 +87,7 @@
</div>

<div class="ibexa-container container">
{{ ibexa_render_component_group('user-profile-blocks') }}
{# {{ ibexa_render_component_group('user-profile-blocks') }}#}
</div>
</main>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Bundle\AdminUi\DependencyInjection\Configuration\Parser;

use PHPUnit\Framework\TestCase;

final class UserProfileTest extends TestCase
{
}

0 comments on commit 15655f3

Please sign in to comment.