From d0688686669d344d8d0a4c87d55611eff4b74d4d Mon Sep 17 00:00:00 2001 From: Tomasz Narloch Date: Wed, 3 Jan 2018 01:14:24 +0100 Subject: [PATCH] Do not add default or active Itemid to every link without own menu item (#19099) * Do not add default or active Itemid to every link without own menu item * Allow to login when there is no menu item for login page and home page is restricted * Correctly redirect user to preferred site language association * Fix link for com_users login form --- .../src/Component/Router/Rules/MenuRules.php | 20 +++---- libraries/src/Router/SiteRouter.php | 18 +----- modules/mod_login/tmpl/default.php | 4 +- .../system/languagefilter/languagefilter.php | 55 +++++++++++-------- templates/beez3/html/mod_login/default.php | 4 +- templates/protostar/offline.php | 4 +- templates/system/offline.php | 4 +- .../rules/JComponentRouterRulesMenuTest.php | 8 +-- .../libraries/cms/router/JRouterSiteTest.php | 18 ------ 9 files changed, 50 insertions(+), 85 deletions(-) diff --git a/libraries/src/Component/Router/Rules/MenuRules.php b/libraries/src/Component/Router/Rules/MenuRules.php index 973d01095c252..467074704f72d 100644 --- a/libraries/src/Component/Router/Rules/MenuRules.php +++ b/libraries/src/Component/Router/Rules/MenuRules.php @@ -156,20 +156,16 @@ public function preprocess(&$query) } } - // Check if the active menuitem matches the requested language - if ($active && $active->component === 'com_' . $this->router->getName() - && ($language === '*' || in_array($active->language, array('*', $language)) || !\JLanguageMultilang::isEnabled())) + // If there is no view and task in query then add the default item id + if (!isset($query['view']) && !isset($query['task'])) { - $query['Itemid'] = $active->id; - return; - } - - // If not found, return language specific home link - $default = $this->router->menu->getDefault($language); + // If not found, return language specific home link + $default = $this->router->menu->getDefault($language); - if (!empty($default->id)) - { - $query['Itemid'] = $default->id; + if (!empty($default->id)) + { + $query['Itemid'] = $default->id; + } } } diff --git a/libraries/src/Router/SiteRouter.php b/libraries/src/Router/SiteRouter.php index 3653222cf0006..b9845fa92e253 100644 --- a/libraries/src/Router/SiteRouter.php +++ b/libraries/src/Router/SiteRouter.php @@ -707,26 +707,14 @@ protected function createUri($url) if ($itemid === null) { - if ($option = $uri->getVar('option')) + if (!$uri->getVar('option')) { - $item = $this->menu->getItem($this->getVar('Itemid')); + $option = $this->getVar('option'); - if ($item !== null && $item->component === $option) - { - $uri->setVar('Itemid', $item->id); - } - } - else - { - if ($option = $this->getVar('option')) + if ($option) { $uri->setVar('option', $option); } - - if ($itemid = $this->getVar('Itemid')) - { - $uri->setVar('Itemid', $itemid); - } } } else diff --git a/modules/mod_login/tmpl/default.php b/modules/mod_login/tmpl/default.php index fcd3b1c962c94..5ea82c8cbc019 100644 --- a/modules/mod_login/tmpl/default.php +++ b/modules/mod_login/tmpl/default.php @@ -15,7 +15,7 @@ JHtml::_('bootstrap.tooltip'); ?> -
+ get('pretext')) : ?>

get('pretext'); ?>

@@ -111,8 +111,6 @@ - -
diff --git a/plugins/system/languagefilter/languagefilter.php b/plugins/system/languagefilter/languagefilter.php index 2019b139e1e98..a40de1d8faa50 100644 --- a/plugins/system/languagefilter/languagefilter.php +++ b/plugins/system/languagefilter/languagefilter.php @@ -597,7 +597,6 @@ public function onUserLogin($user, $options = array()) { if ($this->params->get('automatic_change', 1)) { - $assoc = JLanguageAssociations::isEnabled(); $lang_code = $user['language']; // If no language is specified for this user, we set it to the site default language @@ -617,11 +616,38 @@ public function onUserLogin($user, $options = array()) $lang_code = $this->current_lang; } + $foundAssociation = false; + + $assoc = JLanguageAssociations::isEnabled(); + + // If association is enabled + if ($assoc) + { + // Retrieves the Itemid from a login form. + $uri = new JUri($this->app->getUserState('users.login.form.return')); + + // Get Itemid from SEF or home page + $query = $this->app::getRouter()->parse($uri); + + // Check, if the login form contains a menu item redirection. + if (!empty($query['Itemid'])) + { + // Try to get associations from that menu item. + $associations = MenusHelper::getAssociations($query['Itemid']); + + // If any association set to the user preferred site language, redirect to that page. + if (isset($associations[$lang_code]) && $menu->getItem($associations[$lang_code])) + { + $associationItemid = $associations[$lang_code]; + $this->app->setUserState('users.login.form.return', 'index.php?Itemid=' . $associationItemid); + $foundAssociation = true; + } + } + } + // Try to get association from the current active menu item $active = $menu->getActive(); - $foundAssociation = false; - /** * Looking for associations. * If the login menu item form contains an internal URL redirection, @@ -629,33 +655,14 @@ public function onUserLogin($user, $options = array()) * In that case we use the redirect as defined in the menu item. * Otherwise we redirect, when available, to the user preferred site language. */ - if ($active && !$active->params['login_redirect_url']) + if (!$foundAssociation && $active && !$active->params['login_redirect_url']) { if ($assoc) { $associations = MenusHelper::getAssociations($active->id); } - // Retrieves the Itemid from a login form. - $uri = new JUri($this->app->getUserState('users.login.form.return')); - - if ($uri->getVar('Itemid')) - { - // The login form contains a menu item redirection. Try to get associations from that menu item. - // If any association set to the user preferred site language, redirect to that page. - if ($assoc) - { - $associations = MenusHelper::getAssociations($uri->getVar('Itemid')); - } - - if (isset($associations[$lang_code]) && $menu->getItem($associations[$lang_code])) - { - $associationItemid = $associations[$lang_code]; - $this->app->setUserState('users.login.form.return', 'index.php?Itemid=' . $associationItemid); - $foundAssociation = true; - } - } - elseif (isset($associations[$lang_code]) && $menu->getItem($associations[$lang_code])) + if (isset($associations[$lang_code]) && $menu->getItem($associations[$lang_code])) { /** * The login form does not contain a menu item redirection. diff --git a/templates/beez3/html/mod_login/default.php b/templates/beez3/html/mod_login/default.php index 77af148d0d2af..9dcac1fe54983 100644 --- a/templates/beez3/html/mod_login/default.php +++ b/templates/beez3/html/mod_login/default.php @@ -12,7 +12,7 @@ JHtml::_('behavior.keepalive'); ?> - + get('pretext')) : ?>

get('pretext'); ?>

@@ -49,8 +49,6 @@

- -
    diff --git a/templates/protostar/offline.php b/templates/protostar/offline.php index 7dfc8ff60409b..5abbdca8cf7fd 100644 --- a/templates/protostar/offline.php +++ b/templates/protostar/offline.php @@ -116,7 +116,7 @@
- +
@@ -131,8 +131,6 @@ - -
diff --git a/templates/system/offline.php b/templates/system/offline.php index 80a03fa6d4b45..0988bc12aeda6 100644 --- a/templates/system/offline.php +++ b/templates/system/offline.php @@ -58,7 +58,7 @@

- +

@@ -77,8 +77,6 @@

- -
diff --git a/tests/unit/suites/libraries/cms/component/router/rules/JComponentRouterRulesMenuTest.php b/tests/unit/suites/libraries/cms/component/router/rules/JComponentRouterRulesMenuTest.php index 5d0307b3d590b..4225cf0a11663 100644 --- a/tests/unit/suites/libraries/cms/component/router/rules/JComponentRouterRulesMenuTest.php +++ b/tests/unit/suites/libraries/cms/component/router/rules/JComponentRouterRulesMenuTest.php @@ -164,13 +164,13 @@ public function casesPreprocess() $cases[] = array(array('option' => 'com_content', 'view' => 'article', 'id' => '42', 'catid' => '22', 'lang' => 'en-GB'), array('option' => 'com_content', 'view' => 'article', 'id' => '42', 'catid' => '22', 'lang' => 'en-GB', 'Itemid' => '49')); - // Check non-existing menu link + // Check non-existing menu link with a key $cases[] = array(array('option' => 'com_content', 'view' => 'categories', 'id' => '42'), - array('option' => 'com_content', 'view' => 'categories', 'id' => '42', 'Itemid' => '49')); + array('option' => 'com_content', 'view' => 'categories', 'id' => '42')); - // Check indirect link to a single view behind a nested view with a key and language + // Check non-existing menu link with a key and language $cases[] = array(array('option' => 'com_content', 'view' => 'categories', 'id' => '42', 'lang' => 'en-GB'), - array('option' => 'com_content', 'view' => 'categories', 'id' => '42', 'lang' => 'en-GB', 'Itemid' => '49')); + array('option' => 'com_content', 'view' => 'categories', 'id' => '42', 'lang' => 'en-GB')); // Check if a query with existing Itemid that is not the current active menu-item is not touched $cases[] = array(array('option' => 'com_content', 'view' => 'categories', 'id' => '42', 'Itemid' => '99'), diff --git a/tests/unit/suites/libraries/cms/router/JRouterSiteTest.php b/tests/unit/suites/libraries/cms/router/JRouterSiteTest.php index 3128233d2b795..a3f79af20de3d 100644 --- a/tests/unit/suites/libraries/cms/router/JRouterSiteTest.php +++ b/tests/unit/suites/libraries/cms/router/JRouterSiteTest.php @@ -1265,24 +1265,6 @@ public function casesCreateUri() 'preset' => array('option' => 'com_test'), 'expected' => 'index.php?var1=value1&option=com_test' ), - // Check if a URL with no Itemid and no option, but globally set Itemid is added the Itemid - array( - 'url' => 'index.php?var1=value1', - 'preset' => array('Itemid' => '42'), - 'expected' => 'index.php?var1=value1&Itemid=42' - ), - // Check if a URL without an Itemid, but with an option set and a global Itemid available, which fits the option of the menu item gets the Itemid appended - array( - 'url' => 'index.php?var1=value&option=com_test', - 'preset' => array('Itemid' => '42'), - 'expected' => 'index.php?var1=value&option=com_test&Itemid=42' - ), - // Check if a URL without an Itemid, but with an option set and a global Itemid available, which does not fit the option of the menu item gets returned identically - array( - 'url' => 'index.php?var1=value&option=com_test3', - 'preset' => array('Itemid' => '42'), - 'expected' => 'index.php?var1=value&option=com_test3' - ), ); }