From 8b1f9b82c02b4b081846fcd633c5bff8db9df380 Mon Sep 17 00:00:00 2001
From: crevillo <crevillo@gmail.com>
Date: Wed, 18 Jul 2018 20:12:17 +0200
Subject: [PATCH] Fix EZP-29433. Unauthorized exception user has no permission
 to read the parent location

---
 src/bundle/Resources/translations/content_url.en.xliff         |  5 +++++
 .../views/content/tab/url/modal_add_custom_url.html.twig       |  8 +++++++-
 src/lib/Tab/LocationView/UrlsTab.php                           | 10 ++++++++--
 3 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/src/bundle/Resources/translations/content_url.en.xliff b/src/bundle/Resources/translations/content_url.en.xliff
index da1f6c20f..c1a7523ce 100644
--- a/src/bundle/Resources/translations/content_url.en.xliff
+++ b/src/bundle/Resources/translations/content_url.en.xliff
@@ -121,6 +121,11 @@
         <target state="new">URL</target>
         <note>key: tab.urls.url</note>
       </trans-unit>
+      <trans-unit id="8cbd9ea97c79240d3a2f41a84328c79908cd8265" resname="tabs.urls.add.site_root.helper.no_parent_name">
+        <source>Unchecked will create the new alias under the parent of the location</source>
+        <target state="new">Unchecked will create the new alias under the parent of the location</target>
+        <note>key: tabs.urls.add.site_root.helper.no_parent_name</note>
+      </trans-unit>
     </body>
   </file>
 </xliff>
diff --git a/src/bundle/Resources/views/content/tab/url/modal_add_custom_url.html.twig b/src/bundle/Resources/views/content/tab/url/modal_add_custom_url.html.twig
index e32697ae7..7a56323a2 100644
--- a/src/bundle/Resources/views/content/tab/url/modal_add_custom_url.html.twig
+++ b/src/bundle/Resources/views/content/tab/url/modal_add_custom_url.html.twig
@@ -28,7 +28,13 @@
                 <span class="ez-modal--custom-url-alias__label">{{ 'tab.urls.add.site_root'|trans|desc('Place alias at the site root') }}</span>
                 {{ form_widget(form.site_root) }}
                 <div class="ez-modal--custom-url-alias__info-text">{{ 'tab.urls.add.site_root.helper.checked'|trans|desc('Checked will create the alias at the site root.') }}</div>
-                <div class="ez-modal--custom-url-alias__info-text">{{ 'tab.urls.add.site_root.helper.unchecked'|trans({'%parent_name%': parent_name})|desc('Unchecked will create the new alias under %parent_name%') }}</div>
+                <div class="ez-modal--custom-url-alias__info-text">
+                    {% if parent_name is not null %}
+                        {{ 'tab.urls.add.site_root.helper.unchecked'|trans({'%parent_name%': parent_name})|desc('Unchecked will create the new alias under %parent_name%') }}
+                    {% else %}
+                        {{ 'tab.urls.add.site_root.helper.no_parent_name'|trans|desc('Unchecked will create the new alias under the parent of this location') }}
+                    {% endif %}
+                </div>
             </div>
             <div class="modal-footer justify-content-center">
                 <button type="button" class="btn btn-dark" data-dismiss="modal">
diff --git a/src/lib/Tab/LocationView/UrlsTab.php b/src/lib/Tab/LocationView/UrlsTab.php
index 5fa208421..019d3857a 100644
--- a/src/lib/Tab/LocationView/UrlsTab.php
+++ b/src/lib/Tab/LocationView/UrlsTab.php
@@ -8,6 +8,7 @@
 
 namespace EzSystems\EzPlatformAdminUi\Tab\LocationView;
 
+use eZ\Publish\API\Repository\Exceptions\UnauthorizedException;
 use eZ\Publish\API\Repository\LocationService;
 use eZ\Publish\API\Repository\URLAliasService;
 use eZ\Publish\API\Repository\Values\Content\Location;
@@ -130,14 +131,12 @@ public function renderView(array $parameters): string
 
         $customUrlAddForm = $this->createCustomUrlAddForm($location);
         $customUrlRemoveForm = $this->createCustomUrlRemoveForm($location, $customUrlPagerfanta->getCurrentPageResults());
-        $parentLocation = $this->locationService->loadLocation($location->parentLocationId);
 
         $canEditCustomUrl = $this->permissionResolver->hasAccess('content', 'urltranslator');
 
         $viewParameters = [
             'form_custom_url_add' => $customUrlAddForm->createView(),
             'form_custom_url_remove' => $customUrlRemoveForm->createView(),
-            'parent_name' => $parentLocation->contentInfo->name,
             'custom_urls_pager' => $customUrlPagerfanta,
             'custom_urls_pagination_params' => $customUrlsPaginationParams,
             'system_urls_pager' => $systemUrlPagerfanta,
@@ -145,6 +144,13 @@ public function renderView(array $parameters): string
             'can_edit_custom_url' => $canEditCustomUrl,
         ];
 
+        try {
+            $parentLocation = $this->locationService->loadLocation($location->parentLocationId);
+            $viewParameters['parent_name'] = $parentLocation->contentInfo->name;
+        } catch (UnauthorizedException $exception) {
+            $viewParameters['parent_name'] = null;
+        }
+
         return $this->twig->render(
             '@ezdesign/content/tab/urls.html.twig',
             array_merge($viewParameters, $parameters)
