Skip to content

Commit

Permalink
Adding field menuitem to login menu item
Browse files Browse the repository at this point in the history
  • Loading branch information
infograf768 committed May 5, 2016
1 parent 44f96a2 commit 6fa9124
Show file tree
Hide file tree
Showing 7 changed files with 377 additions and 33 deletions.
11 changes: 11 additions & 0 deletions administrator/language/en-GB/en-GB.com_users.ini
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ COM_USERS_ERROR_VIEW_LEVEL_IN_USE="You can't delete the view access level '%d:%s
COM_USERS_ERROR_SECRET_CODE_WITHOUT_TFA="You have entered a Secret Code but two factor authentication is not enabled in your user account. If you want to use a secret code to secure your login please edit your user profile and enable two factor authentication."
COM_USERS_FIELD_CATEGORY_ID_LABEL="Category"
COM_USERS_FIELD_ID_LABEL="ID"
COM_USERS_FIELD_LOGIN_MENUITEM="Menu Item"
COM_USERS_FIELD_LOGIN_REDIRECT_CHOICE_DESC="'Custom URL' lets enter any internal url in the Redirect field while 'Menu Item' let's directly select an existing menu item.<br />For a multilingual site, it is advised to choose 'Menu Item'."
COM_USERS_FIELD_LOGIN_REDIRECT_CHOICE_LABEL="Choose Login Redirect Type"
COM_USERS_FIELD_LOGIN_REDIRECT_ERROR="Only one of the login redirect fields should have a value."
COM_USERS_FIELD_LOGIN_REDIRECTMENU_DESC="Select the page the user will be redirected to after a successful login. Select from all the pages listed in the dropdown menu. Choosing &quot;Default&quot; will return to the same page."
COM_USERS_FIELD_LOGIN_REDIRECTMENU_LABEL="Menu Item Login Redirect"
COM_USERS_FIELD_LOGIN_URL="Custom URL"
COM_USERS_FIELD_LOGOUT_REDIRECT_CHOICE_LABEL="Choose Logout Redirect Type"
COM_USERS_FIELD_LOGOUT_REDIRECT_ERROR="Only one of the logout redirect fields should have a value."
COM_USERS_FIELD_LOGOUT_REDIRECTMENU_DESC="Select the page the user will be redirected to after successfully ending their current session by logging out. Select from all the pages listed in the dropdown menu. Choosing &quot;Default&quot; will return to the same page."
COM_USERS_FIELD_LOGOUT_REDIRECTMENU_LABEL="Menu Item Logout Redirect"
COM_USERS_FIELD_NOTEBODY_DESC="Note"
COM_USERS_FIELD_NOTEBODY_LABEL="Note"
COM_USERS_FIELD_REVIEW_TIME_DESC="Review Date is a manually entered date you can use as fits in your workflow. Examples would be to put in a date when you want to review a user or the last date you reviewed the user."
Expand Down
154 changes: 146 additions & 8 deletions components/com_users/controllers/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,53 @@ public function login()
$data['password'] = $input->$method->get('password', '', 'RAW');
$data['secretkey'] = $input->$method->get('secretkey', '', 'RAW');

// Don't redirect to an external URL.
if (!JUri::isInternal($data['return']))
// Check for a simple menu item id
if (is_numeric($data['return']))
{
$data['return'] = '';
if (JLanguageMultilang::isEnabled())
{

$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select('language')
->from($db->quoteName('#__menu'))
->where('client_id = 0')
->where('id =' . $data['return']);

$db->setQuery($query);

try
{
$language = $db->loadResult();
}
catch (RuntimeException $e)
{
return;
}

if ($language !== '*')
{
$lang = '&lang=' . $language;
}
else
{
$lang = '';
}
}
else
{
$lang = '';
}

$data['return'] = 'index.php?Itemid=' . $data['return'] . $lang;
}
else
{
// Don't redirect to an external URL.
if (!JUri::isInternal($data['return']))
{
$data['return'] = '';
}
}

// Set the return URL if empty.
Expand Down Expand Up @@ -118,11 +161,56 @@ public function logout()
$return = $input->$method->get('return', '', 'BASE64');
$return = base64_decode($return);

if (!JUri::isInternal($return))
// Check for a simple menu item id
if (is_numeric($return))
{
$return = '';
if (JLanguageMultilang::isEnabled())
{

$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select('language')
->from($db->quoteName('#__menu'))
->where('client_id = 0')
->where('id =' . $return);

$db->setQuery($query);

try
{
$language = $db->loadResult();
}
catch (RuntimeException $e)
{
return;
}

if ($language !== '*')
{
$lang = '&lang=' . $language;
}
else
{
$lang = '';
}
}
else
{
$lang = '';
}

$return = 'index.php?Itemid=' . $return . $lang;
}
else
{
// Don't redirect to an external URL.
if (!JUri::isInternal($data['return']))
{
$data['return'] = '';
}
}


// Redirect the user.
$app->redirect(JRoute::_($return, false));
}
Expand All @@ -137,10 +225,60 @@ public function logout()
public function menulogout()
{
// Get the ItemID of the page to redirect after logout
$itemid = JFactory::getApplication()->getMenu()->getActive()->params->get('logout');
$app = JFactory::getApplication();
$itemid = $app->getMenu()->getActive()->params->get('logout');

// Get the language of the page when multilang is on
if (JLanguageMultilang::isEnabled())
{
if ($itemid)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select('language')
->from($db->quoteName('#__menu'))
->where('client_id = 0')
->where('id =' . $itemid);

$db->setQuery($query);

try
{
$language = $db->loadResult();
}
catch (RuntimeException $e)
{
return;
}

if ($language !== '*')
{
$lang = '&lang=' . $language;
}
else
{
$lang = '';
}

// URL to redirect after logout
$url = 'index.php?Itemid=' . $itemid . $lang;
}
else
{
// Logout is set to default. Get the home page ItemID
$lang_code = $app->input->cookie->getString(JApplicationHelper::getHash('language'));
$item = $app->getMenu()->getDefault($lang_code);
$itemid = $item->id;

// URL to redirect after logout, default page if no ItemID is set
$url = $itemid ? 'index.php?Itemid=' . $itemid : JURI::root();
// Redirect to Home page after logout
$url = 'index.php?Itemid=' . $itemid;
}
}
else
{
// URL to redirect after logout, default page if no ItemID is set
$url = $itemid ? 'index.php?Itemid=' . $itemid : JURI::root();
}

// Logout and redirect
$this->setRedirect('index.php?option=com_users&task=user.logout&' . JSession::getFormToken() . '=1&return=' . base64_encode($url));
Expand Down
64 changes: 64 additions & 0 deletions components/com_users/models/rules/loginuniquefield.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* @package Joomla.Site
* @subpackage com_users
*
* @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

defined('JPATH_PLATFORM') or die;

use Joomla\Registry\Registry;

/**
* JFormRule for com_users to be sure only one redirect login field has a value
*
* @since 3.6
*/
class JFormRuleLoginUniqueField extends JFormRule
{
/**
* Method to test if two fields have a value in order to use only one field.
* To use this rule, the form
* XML needs a validate attribute of loginuniquefield and a field attribute
* that is equal to the field to test against.
*
* @param SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
* @param mixed $value The form field value to validate.
* @param string $group The field name group control value. This acts as as an array container for the field.
* For example if the field has name="foo" and the group value is set to "bar" then the
* full field name would end up being "bar[foo]".
* @param Registry $input An optional Registry object with the entire data set to validate against the entire form.
* @param JForm $form The form object for which the field is being tested.
*
* @return boolean True if the value is valid, false otherwise.
*
* @since 3.6
*/
public function test(SimpleXMLElement $element, $value, $group = null, Registry $input = null, JForm $form = null)
{
$field = (string) $element['field'];

$loginRedirectUrl = $input['params']->login_redirect_url;
$loginRedirectMenuitem = $input['params']->login_redirect_menuitem;

if (is_null($form))
{
throw new InvalidArgumentException(sprintf('The value for $form must not be null in %s', get_class($this)));
}

if (is_null($input))
{
throw new InvalidArgumentException(sprintf('The value for $input must not be null in %s', get_class($this)));
}

// Test the input values for login.
if ($loginRedirectUrl != '' && $loginRedirectMenuitem != '')
{
return false;
}

return true;
}
}
64 changes: 64 additions & 0 deletions components/com_users/models/rules/logoutuniquefield.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* @package Joomla.Site
* @subpackage com_users
*
* @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

defined('JPATH_PLATFORM') or die;

use Joomla\Registry\Registry;

/**
* JFormRule for com_users to be sure only one redirect logout field has a value
*
* @since 3.6
*/
class JFormRuleLogoutUniqueField extends JFormRule
{
/**
* Method to test if two fields have a value in order to use only one field.
* To use this rule, the form
* XML needs a validate attribute of logoutuniquefield and a field attribute
* that is equal to the field to test against.
*
* @param SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
* @param mixed $value The form field value to validate.
* @param string $group The field name group control value. This acts as as an array container for the field.
* For example if the field has name="foo" and the group value is set to "bar" then the
* full field name would end up being "bar[foo]".
* @param Registry $input An optional Registry object with the entire data set to validate against the entire form.
* @param JForm $form The form object for which the field is being tested.
*
* @return boolean True if the value is valid, false otherwise.
*
* @since 3.6
*/
public function test(SimpleXMLElement $element, $value, $group = null, Registry $input = null, JForm $form = null)
{
$field = (string) $element['field'];

$logoutRedirectUrl = $input['params']->logout_redirect_url;
$logoutRedirectMenuitem = $input['params']->logout_redirect_menuitem;

if (is_null($form))
{
throw new InvalidArgumentException(sprintf('The value for $form must not be null in %s', get_class($this)));
}

if (is_null($input))
{
throw new InvalidArgumentException(sprintf('The value for $input must not be null in %s', get_class($this)));
}

// Test the input values for logout.
if ($logoutRedirectUrl != '' && $logoutRedirectMenuitem != '')
{
return false;
}

return true;
}
}

0 comments on commit 6fa9124

Please sign in to comment.