-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUserController.php
More file actions
190 lines (175 loc) · 8.2 KB
/
Copy pathUserController.php
File metadata and controls
190 lines (175 loc) · 8.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?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\ContentForms\Controller;
use Ibexa\Bundle\Core\Controller;
use Ibexa\ContentForms\Data\Mapper\UserCreateMapper;
use Ibexa\ContentForms\Data\Mapper\UserUpdateMapper;
use Ibexa\ContentForms\Form\ActionDispatcher\ActionDispatcherInterface;
use Ibexa\ContentForms\Form\Type\User\UserCreateType;
use Ibexa\ContentForms\Form\Type\User\UserUpdateType;
use Ibexa\ContentForms\User\View\UserCreateView;
use Ibexa\ContentForms\User\View\UserUpdateView;
use Ibexa\Contracts\ContentForms\Content\Form\Provider\GroupedContentFormFieldsProviderInterface;
use Ibexa\Contracts\Core\Repository\ContentService;
use Ibexa\Contracts\Core\Repository\ContentTypeService;
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\UserService;
use Ibexa\Core\Base\Exceptions\UnauthorizedException as CoreUnauthorizedException;
use Ibexa\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
final class UserController extends Controller
{
public function __construct(
private readonly ContentTypeService $contentTypeService,
private readonly UserService $userService,
private readonly LocationService $locationService,
private readonly LanguageService $languageService,
private readonly ActionDispatcherInterface $userActionDispatcher,
private readonly PermissionResolver $permissionResolver,
private readonly UserLanguagePreferenceProviderInterface $userLanguagePreferenceProvider,
private readonly GroupedContentFormFieldsProviderInterface $groupedContentFormFieldsProvider,
private readonly ContentService $contentService
) {
}
/**
* @throws \Ibexa\Core\Base\Exceptions\InvalidArgumentType
* @throws \Ibexa\Core\Base\Exceptions\InvalidArgumentException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
* @throws \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
* @throws \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
* @throws \Symfony\Component\OptionsResolver\Exception\NoSuchOptionException
* @throws \Symfony\Component\OptionsResolver\Exception\MissingOptionsException
* @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
* @throws \Symfony\Component\OptionsResolver\Exception\AccessException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
*/
public function createAction(
string $contentTypeIdentifier,
string $language,
int $parentLocationId,
Request $request
): Response|UserCreateView {
$contentType = $this->contentTypeService->loadContentTypeByIdentifier(
$contentTypeIdentifier,
$this->userLanguagePreferenceProvider->getPreferredLanguages()
);
$location = $this->locationService->loadLocation($parentLocationId);
$language = $this->languageService->loadLanguage($language);
$languageCode = $language->getLanguageCode();
$parentGroup = $this->userService->loadUserGroup($location->getContentId());
$data = (new UserCreateMapper())->mapToFormData($contentType, [$parentGroup], [
'mainLanguageCode' => $languageCode,
]);
$data->sectionId = $location->getContentInfo()->getSectionId();
$form = $this->createForm(UserCreateType::class, $data, [
'languageCode' => $languageCode,
'mainLanguageCode' => $languageCode,
'struct' => $data,
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid() && null !== $form->getClickedButton()) {
$this->userActionDispatcher->dispatchFormAction($form, $data, $form->getClickedButton()->getName());
if ($response = $this->userActionDispatcher->getResponse()) {
return $response;
}
}
return new UserCreateView(
null,
[
'form' => $form->createView(),
'language' => $language,
'parent_location' => $location,
'content_type' => $contentType,
'parent_group' => $parentGroup,
'grouped_fields' => $this->groupedContentFormFieldsProvider->getGroupedFields(
$form->get('fieldsData')->all()
),
]
);
}
/**
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
* @throws \Ibexa\Core\Base\Exceptions\InvalidArgumentType
* @throws \Ibexa\Core\Base\Exceptions\UnauthorizedException
*/
public function editAction(
int $contentId,
int $versionNo,
string $language,
Request $request
): Response|UserUpdateView {
$user = $this->userService->loadUser($contentId);
if (!$this->permissionResolver->canUser('content', 'edit', $user)) {
throw new CoreUnauthorizedException('content', 'edit', ['userId' => $contentId]);
}
$contentType = $this->contentTypeService->loadContentType(
$user->getContentInfo()->getContentType()->id,
$this->userLanguagePreferenceProvider->getPreferredLanguages()
);
$userUpdate = (new UserUpdateMapper())->mapToFormData($user, $contentType, [
'languageCode' => $language,
]);
try {
// assume main location if no location was provided
$location = $this->locationService->loadLocation(
(int)$user->getVersionInfo()->getContentInfo()->getMainLocationId()
);
} catch (UnauthorizedException $e) {
// if no access to the main location assume content has multiple locations and first of them can be used
$availableLocations = $this->locationService->loadLocations(
$user->getVersionInfo()->getContentInfo()
);
$location = array_shift($availableLocations);
}
$form = $this->createForm(
UserUpdateType::class,
$userUpdate,
[
'location' => $location,
'content' => $this->contentService->loadContent($contentId),
'languageCode' => $language,
'mainLanguageCode' => $user->getContentInfo()->getMainLanguageCode(),
'struct' => $userUpdate,
],
);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid() && null !== $form->getClickedButton()) {
$this->userActionDispatcher->dispatchFormAction($form, $userUpdate, $form->getClickedButton()->getName());
if ($response = $this->userActionDispatcher->getResponse()) {
return $response;
}
}
$parentLocation = null;
try {
$parentLocation = $this->locationService->loadLocation($location->parentLocationId);
} catch (UnauthorizedException) {
//do nothing
}
return new UserUpdateView(
null,
[
'form' => $form->createView(),
'language_code' => $language,
'language' => $this->languageService->loadLanguage($language),
'content_type' => $contentType,
'user' => $user,
'location' => $location,
'parent_location' => $parentLocation,
'grouped_fields' => $this->groupedContentFormFieldsProvider->getGroupedFields(
$form->get('fieldsData')->all()
),
]
);
}
}