From e489367a2043a638a11887ed75d98014fd3c860a Mon Sep 17 00:00:00 2001 From: Pravin_s Date: Wed, 4 Sep 2019 17:02:00 +0530 Subject: [PATCH 1/2] Task #14 chore: Improvement made in cluster package - updating queries, removed unwanted code and also added JSON controller to get all associated users --- .../administrator/models/clusters.php | 2 +- .../administrator/models/clusteruser.php | 10 ++- .../administrator/models/clusterusers.php | 18 ----- .../site/controllers/clusterusers.json.php | 67 +++++++++++++++++++ 4 files changed, 75 insertions(+), 22 deletions(-) create mode 100644 src/components/com_cluster/site/controllers/clusterusers.json.php diff --git a/src/components/com_cluster/administrator/models/clusters.php b/src/components/com_cluster/administrator/models/clusters.php index 25ac53d..803e2af 100644 --- a/src/components/com_cluster/administrator/models/clusters.php +++ b/src/components/com_cluster/administrator/models/clusters.php @@ -58,7 +58,7 @@ protected function getListQuery() $query = $db->getQuery(true); // Create the base select statement. - $query->select(array('cl.*','users.name as uname')); + $query->select(array('cl.*','users.name as uname','cl.id as cluster_id')); $query->from($db->quoteName('#__tj_clusters', 'cl')); $query->join('LEFT', $db->quoteName('#__users', 'users') . ' ON (' . $db->quoteName('cl.created_by') . ' = ' . $db->quoteName('users.id') . ')'); diff --git a/src/components/com_cluster/administrator/models/clusteruser.php b/src/components/com_cluster/administrator/models/clusteruser.php index 3357797..15b46de 100644 --- a/src/components/com_cluster/administrator/models/clusteruser.php +++ b/src/components/com_cluster/administrator/models/clusteruser.php @@ -131,11 +131,13 @@ public function getUsersClusters($userId = null) JLoader::import("/components/com_cluster/includes/cluster", JPATH_ADMINISTRATOR); // If user is not allowed to view all the clusters then return the clusters in which user is a part else return al cluster - if (!$user->authorise('core.manageall.cluster', 'com_cluster')) + if (!$user->authorise('core.manageall', 'com_cluster')) { $clusterUsersModel = ClusterFactory::model('ClusterUsers', array('ignore_request' => true)); $clusterUsersModel->setState('list.group_by_client_id', 1); $clusterUsersModel->setState('filter.published', 1); + $clusterUsersModel->setState('list.ordering', 'cl.name'); + $clusterUsersModel->setState('list.direction', 'ASC'); $clusterUsersModel->setState('filter.user_id', $user->id); // Get all assigned cluster entries @@ -147,6 +149,8 @@ public function getUsersClusters($userId = null) // Get all cluster entries $clusterModel->setState('filter.state', 1); + $clusterModel->setState('list.ordering', 'cl.name'); + $clusterModel->setState('list.direction', 'ASC'); $clusters = $clusterModel->getItems(); } @@ -162,12 +166,12 @@ public function getUsersClusters($userId = null) if (!empty($clusters)) { - if ($subUserExist && (!$user->authorise('core.manageall.cluster', 'com_cluster'))) + if ($subUserExist && (!$user->authorise('core.manageall', 'com_cluster'))) { foreach ($clusters as $cluster) { // Check user has permission for mentioned cluster - if (RBACL::authorise($user->id, 'com_cluster', 'core.manage.cluster', $cluster->id)) + if (RBACL::authorise($user->id, 'com_cluster', 'core.manage', $cluster->cluster_id)) { $usersClusters[] = $cluster; } diff --git a/src/components/com_cluster/administrator/models/clusterusers.php b/src/components/com_cluster/administrator/models/clusterusers.php index 1e5ebfa..c4de463 100644 --- a/src/components/com_cluster/administrator/models/clusterusers.php +++ b/src/components/com_cluster/administrator/models/clusterusers.php @@ -62,24 +62,6 @@ protected function getListQuery() . $db->quoteName('users.id') . ')'); $query->join('INNER', $db->quoteName('#__tj_clusters', 'cl') . ' ON (' . $db->quoteName('cl.id') . ' = ' . $db->quoteName('cu.cluster_id') . ')'); - // Get com_subusers component status - $subUserExist = ComponentHelper::getComponent('com_subusers', true)->enabled; - - if ($subUserExist) - { - $roleId = $this->getState('filter.role_id'); - - if (is_numeric($roleId)) - { - $query->join('INNER', $db->qn('#__tjsu_users', 'su') . - ' ON (' . $db->qn('users.id') . ' = ' . $db->qn('su.user_id') . ' AND ' . $db->qn('su.client') . ' = "com_multiagency" )'); - $query->join('INNER', $db->qn('#__tjsu_roles', 'r') . - ' ON (' . $db->qn('r.id') . ' = ' . $db->qn('su.role_id') . ' AND ' . $db->qn('r.state') . ' = 1 )'); - - $query->where($db->quoteName('su.role_id') . ' != ' . (int) $roleId); - } - } - // Filter by search in title. $search = $this->getState('filter.search'); diff --git a/src/components/com_cluster/site/controllers/clusterusers.json.php b/src/components/com_cluster/site/controllers/clusterusers.json.php new file mode 100644 index 0000000..7eda3fa --- /dev/null +++ b/src/components/com_cluster/site/controllers/clusterusers.json.php @@ -0,0 +1,67 @@ + + * @copyright Copyright (C) 2009 - 2019 Techjoomla. All rights reserved. + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +// No direct access +defined('_JEXEC') or die; + +use Joomla\CMS\Factory; +use Joomla\CMS\Language\Text; +use Joomla\CMS\HTML\HTMLHelper; +use Joomla\CMS\Component\ComponentHelper; +use Joomla\CMS\MVC\Controller\BaseController; +use Joomla\CMS\Session\Session; + +JLoader::import("/components/com_cluster/includes/cluster", JPATH_ADMINISTRATOR); + +/** + * Get Cluster Users controller class. + * + * @since 1.0.0 + */ +class ClusterControllerClusterUsers extends BaseController +{ + /** + * Method to get user list depending on the client chosen. + * + * @return null + * + * @since 1.0.0 + */ + public function getUsersByClientId() + { + // Check for request forgeries. + Session::checkToken() or jexit(JText::_('JINVALID_TOKEN')); + + $clusterIds = Factory::getApplication()->input->getInt('cluster_id', 0); + $userOptions = $allUsers = array(); + + // Initialize array to store dropdown options + $userOptions[] = HTMLHelper::_('select.option', "", Text::_('COM_TJFIELDS_OWNERSHIP_USER')); + + // Check cluster selected or not + if ($clusterIds) + { + $clusterObj = ClusterFactory::model('ClusterUsers', array('ignore_request' => true)); + $clusterObj->setState('filter.block', 0); + $clusterObj->setState('filter.cluster_id', $clusterIds); + $clusterObj->setState('list.group_by_user_id', 1); + $allUsers = $clusterObj->getItems(); + } + + if (!empty($allUsers)) + { + foreach ($allUsers as $user) + { + $userOptions[] = HTMLHelper::_('select.option', $user->user_id, trim($user->uname . ' (' . $user->uemail . ')')); + } + } + + echo new JResponseJson($userOptions); + jexit(); + } +} From ef93b1b85778447fc90f59fa247ced9d64578612 Mon Sep 17 00:00:00 2001 From: Pravin_s Date: Mon, 14 Oct 2019 16:37:09 +0530 Subject: [PATCH 2/2] Task #14 chore: resolve MR comments --- .../site/controllers/clusterusers.json.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/components/com_cluster/site/controllers/clusterusers.json.php b/src/components/com_cluster/site/controllers/clusterusers.json.php index 7eda3fa..2753758 100644 --- a/src/components/com_cluster/site/controllers/clusterusers.json.php +++ b/src/components/com_cluster/site/controllers/clusterusers.json.php @@ -12,7 +12,7 @@ use Joomla\CMS\Factory; use Joomla\CMS\Language\Text; use Joomla\CMS\HTML\HTMLHelper; -use Joomla\CMS\Component\ComponentHelper; +use Joomla\CMS\Response\JsonResponse; use Joomla\CMS\MVC\Controller\BaseController; use Joomla\CMS\Session\Session; @@ -34,14 +34,20 @@ class ClusterControllerClusterUsers extends BaseController */ public function getUsersByClientId() { + $app = Factory::getApplication(); + // Check for request forgeries. - Session::checkToken() or jexit(JText::_('JINVALID_TOKEN')); + if (!Session::checkToken()) + { + echo new JsonResponse(null, Text::_('JINVALID_TOKEN'), true); + $app->close(); + } - $clusterIds = Factory::getApplication()->input->getInt('cluster_id', 0); + $clusterIds = $app->input->getInt('cluster_id', 0); $userOptions = $allUsers = array(); // Initialize array to store dropdown options - $userOptions[] = HTMLHelper::_('select.option', "", Text::_('COM_TJFIELDS_OWNERSHIP_USER')); + $userOptions[] = HTMLHelper::_('select.option', "", Text::_('COM_CLUSTER_OWNERSHIP_USER')); // Check cluster selected or not if ($clusterIds) @@ -61,7 +67,7 @@ public function getUsersByClientId() } } - echo new JResponseJson($userOptions); - jexit(); + echo new JsonResponse($userOptions); + $app->close(); } }