2222use Ibexa \Core \Repository \Repository ;
2323use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
2424use Symfony \Component \HttpFoundation \RedirectResponse ;
25+ use Symfony \Component \HttpFoundation \RequestStack ;
2526use Symfony \Component \Routing \Generator \UrlGeneratorInterface ;
2627
2728final class UserProfileListener implements EventSubscriberInterface
@@ -38,20 +39,24 @@ final class UserProfileListener implements EventSubscriberInterface
3839
3940 private UserProfileConfigurationInterface $ configuration ;
4041
42+ private RequestStack $ requestStack ;
43+
4144 public function __construct (
4245 Repository $ repository ,
4346 PermissionResolver $ permissionResolver ,
4447 ContentService $ contentService ,
4548 UserService $ userService ,
4649 UrlGeneratorInterface $ urlGenerator ,
47- UserProfileConfigurationInterface $ configuration
50+ UserProfileConfigurationInterface $ configuration ,
51+ RequestStack $ requestStack
4852 ) {
4953 $ this ->repository = $ repository ;
5054 $ this ->permissionResolver = $ permissionResolver ;
5155 $ this ->contentService = $ contentService ;
5256 $ this ->userService = $ userService ;
5357 $ this ->urlGenerator = $ urlGenerator ;
5458 $ this ->configuration = $ configuration ;
59+ $ this ->requestStack = $ requestStack ;
5560 }
5661
5762 public static function getSubscribedEvents (): array
@@ -67,15 +72,11 @@ public function onUserUpdate(FormActionEvent $event): void
6772 $ form = $ event ->getForm ();
6873 $ data = $ event ->getData ();
6974
70- if (!($ data instanceof UserUpdateData) || !$ this ->isUserProfileUpdate ($ data )) {
75+ if (!($ data instanceof UserUpdateData) || !$ this ->isSupported ($ data )) {
7176 return ;
7277 }
7378
7479 $ user = $ data ->user ;
75- if (!$ this ->canEditUserProfile ($ user )) {
76- return ;
77- }
78-
7980 $ updateStruct = $ this ->createUpdateStruct ($ data , $ form ->getConfig ()->getOption ('languageCode ' ));
8081
8182 // user / selfedit policy is enough to edit own profile (checked in
@@ -90,7 +91,6 @@ public function onUserUpdate(FormActionEvent $event): void
9091 public function onUserCancel (FormActionEvent $ event ): void
9192 {
9293 $ data = $ event ->getData ();
93-
9494 if (!($ data instanceof UserUpdateData) || !$ this ->isSupported ($ data )) {
9595 return ;
9696 }
@@ -113,7 +113,10 @@ private function createRedirectToUserProfile(User $user): RedirectResponse
113113
114114 private function isSupported (UserUpdateData $ data ): bool
115115 {
116- return $ this ->isUserProfileUpdate ($ data ) && $ this ->canEditUserProfile ($ data ->user );
116+ return
117+ $ this ->doesOriginateFromProfileEditing () &&
118+ $ this ->isUserProfileUpdate ($ data ) &&
119+ $ this ->canEditUserProfile ($ data ->user );
117120 }
118121
119122 private function createUpdateStruct (UserUpdateData $ data , string $ languageCode ): UserUpdateStruct
@@ -150,4 +153,14 @@ private function canEditUserProfile(User $user): bool
150153 $ this ->permissionResolver ->canUser ('user ' , 'selfedit ' , $ user )
151154 && (new IsProfileAvailable ($ this ->configuration ))->isSatisfiedBy ($ user );
152155 }
156+
157+ private function doesOriginateFromProfileEditing (): bool
158+ {
159+ $ request = $ this ->requestStack ->getMainRequest ();
160+ if ($ request === null ) {
161+ return false ;
162+ }
163+
164+ return $ request ->attributes ->get ('_route ' ) === 'ibexa.user.profile.edit ' ;
165+ }
153166}
0 commit comments