Skip to content

Commit

Permalink
Do not add default or active Itemid to every link without own menu it…
Browse files Browse the repository at this point in the history
…em (#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
  • Loading branch information
csthomas authored and Michael Babker committed Jan 3, 2018
1 parent 20424c4 commit d068868
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 85 deletions.
20 changes: 8 additions & 12 deletions libraries/src/Component/Router/Rules/MenuRules.php
Expand Up @@ -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;
}
}
}

Expand Down
18 changes: 3 additions & 15 deletions libraries/src/Router/SiteRouter.php
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions modules/mod_login/tmpl/default.php
Expand Up @@ -15,7 +15,7 @@
JHtml::_('bootstrap.tooltip');

?>
<form action="<?php echo JRoute::_('index.php', true, $params->get('usesecure')); ?>" method="post" id="login-form" class="form-inline">
<form action="<?php echo JRoute::_('index.php?option=com_users&task=user.login', true, $params->get('usesecure')); ?>" method="post" id="login-form" class="form-inline">
<?php if ($params->get('pretext')) : ?>
<div class="pretext">
<p><?php echo $params->get('pretext'); ?></p>
Expand Down Expand Up @@ -111,8 +111,6 @@
<?php echo JText::_('MOD_LOGIN_FORGOT_YOUR_PASSWORD'); ?></a>
</li>
</ul>
<input type="hidden" name="option" value="com_users" />
<input type="hidden" name="task" value="user.login" />
<input type="hidden" name="return" value="<?php echo $return; ?>" />
<?php echo JHtml::_('form.token'); ?>
</div>
Expand Down
55 changes: 31 additions & 24 deletions plugins/system/languagefilter/languagefilter.php
Expand Up @@ -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
Expand All @@ -617,45 +616,53 @@ 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,
* This will override the automatic change to the user preferred site language.
* 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.
Expand Down
4 changes: 1 addition & 3 deletions templates/beez3/html/mod_login/default.php
Expand Up @@ -12,7 +12,7 @@

JHtml::_('behavior.keepalive');
?>
<form action="<?php echo JRoute::_('index.php', true, $params->get('usesecure')); ?>" method="post" id="login-form" >
<form action="<?php echo JRoute::_('index.php?option=com_users&task=user.login', true, $params->get('usesecure')); ?>" method="post" id="login-form" >
<?php if ($params->get('pretext')) : ?>
<div class="pretext">
<p><?php echo $params->get('pretext'); ?></p>
Expand Down Expand Up @@ -49,8 +49,6 @@
</p>
<?php endif; ?>
<input type="submit" name="Submit" class="button" value="<?php echo JText::_('JLOGIN') ?>" />
<input type="hidden" name="option" value="com_users" />
<input type="hidden" name="task" value="user.login" />
<input type="hidden" name="return" value="<?php echo $return; ?>" />
<?php echo JHtml::_('form.token'); ?>
<ul>
Expand Down
4 changes: 1 addition & 3 deletions templates/protostar/offline.php
Expand Up @@ -116,7 +116,7 @@
<?php endif; ?>
</div>
<jdoc:include type="message" />
<form action="<?php echo JRoute::_('index.php', true); ?>" method="post" id="form-login">
<form action="<?php echo JRoute::_('index.php?option=com_users&task=user.login'); ?>" method="post" id="form-login">
<fieldset>
<label for="username"><?php echo JText::_('JGLOBAL_USERNAME'); ?></label>
<input name="username" id="username" type="text" title="<?php echo JText::_('JGLOBAL_USERNAME'); ?>" />
Expand All @@ -131,8 +131,6 @@

<input type="submit" name="Submit" class="btn btn-primary" value="<?php echo JText::_('JLOGIN'); ?>" />

<input type="hidden" name="option" value="com_users" />
<input type="hidden" name="task" value="user.login" />
<input type="hidden" name="return" value="<?php echo base64_encode(JUri::base()); ?>" />
<?php echo JHtml::_('form.token'); ?>
</fieldset>
Expand Down
4 changes: 1 addition & 3 deletions templates/system/offline.php
Expand Up @@ -58,7 +58,7 @@
<?php echo JText::_('JOFFLINE_MESSAGE'); ?>
</p>
<?php endif; ?>
<form action="<?php echo JRoute::_('index.php', true); ?>" method="post" id="form-login">
<form action="<?php echo JRoute::_('index.php?option=com_users&task=user.login'); ?>" method="post" id="form-login">
<fieldset class="input">
<p id="form-login-username">
<label for="username"><?php echo JText::_('JGLOBAL_USERNAME'); ?></label>
Expand All @@ -77,8 +77,6 @@
<p id="submit-buton">
<input type="submit" name="Submit" class="button login" value="<?php echo JText::_('JLOGIN'); ?>" />
</p>
<input type="hidden" name="option" value="com_users" />
<input type="hidden" name="task" value="user.login" />
<input type="hidden" name="return" value="<?php echo base64_encode(JUri::base()); ?>" />
<?php echo JHtml::_('form.token'); ?>
</fieldset>
Expand Down
Expand Up @@ -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'),
Expand Down
18 changes: 0 additions & 18 deletions tests/unit/suites/libraries/cms/router/JRouterSiteTest.php
Expand Up @@ -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'
),
);
}

Expand Down

1 comment on commit d068868

@Quy
Copy link
Contributor

@Quy Quy commented on d068868 Jan 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@csthomas The changes in libraries/src/Component/Router/Rules/MenuRules.php affected the archived pagination. I replaced it with v3.8.3 and problem fixed. See #19409.

Please sign in to comment.