Skip to content

Commit

Permalink
Task techjoomla#10 feat: Add function to get list of cluster to which…
Browse files Browse the repository at this point in the history
… a given have access
  • Loading branch information
ankush-maherwal committed Aug 14, 2019
1 parent cd02980 commit a19167d
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/components/com_cluster/administrator/models/clusteruser.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Model\AdminModel;
use Joomla\CMS\Table\Table;
use Joomla\CMS\Component\ComponentHelper;

/**
* Item Model for an Cluster.
Expand Down Expand Up @@ -110,4 +111,74 @@ public function save($data)

return parent::save($data);
}

/**
* Method to get the list of clusters to which user have access
*
* @param INT $userId Users Id.
*
* @return ARRAY List of clusters.
*
* @since 1.0.0
*/
public function getUsersClusters($userId)
{
$user = Factory::getUser($userId);
$superUser = $user->authorise('core.admin');

$clusters = array();

// Load cluster library file
JLoader::import("/components/com_cluster/includes/cluster", JPATH_ADMINISTRATOR);

if (!$superUser && !$user->authorise('core.manageall.cluster', '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('filter.user_id', $user->id);

// Get all assigned cluster entries
$clusters = $clusterUsersModel->getItems();
}

if ($superUser)
{
$clusterModel = ClusterFactory::model('Clusters', array('ignore_request' => true));

// Get all cluster entries
$clusters = $clusterModel->getItems();
}

// Get com_subusers component status
$subUserExist = ComponentHelper::getComponent('com_subusers', true)->enabled;

if ($subUserExist)
{
JLoader::import("/components/com_subusers/includes/rbacl", JPATH_ADMINISTRATOR);
}

$usersClusters = array();

if (!empty($clusters))
{
if ($subUserExist && (!$superUser && !$user->authorise('core.manageall.cluster', '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))
{
$usersClusters[] = $cluster;
}
}
}
else
{
$usersClusters = $clusters;
}
}

return $usersClusters;
}
}
56 changes: 56 additions & 0 deletions src/components/com_cluster/administrator/models/clusterusers.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,62 @@ protected function getListQuery()
$query->where('cu.cluster_id = ' . (int) $cluster);
}

// Filter by user
$cluster_user = $this->getState('filter.user_id');

if (is_numeric($cluster_user))
{
$query->where('cu.user_id = ' . (int) $cluster_user);
}

// Filter by client_id
$clusterClientId = $this->getState('filter.client_id');

if (is_numeric($clusterClientId))
{
$query->where('cl.client_id = ' . (int) $clusterClientId);
}
elseif (is_array($clusterClientId))
{
$query->where("cl.client_id IN ('" . implode("','", $clusterClientId) . "')");
}

// Filter by state
$published = $this->getState('filter.published');

if (is_numeric($published))
{
$query->where('cl.state = ' . (int) $published);
}
elseif ($published === '')
{
$query->where('(cl.state = 0 OR cl.state = 1)');
}

// Filter by blocked users
$blockUser = $this->getState('filter.block');

if (is_numeric($blockUser))
{
$query->where($db->quoteName('users.block') . ' = ' . (int) $blockUser);
}

// Group by cluster
$clientID = $this->getState('list.group_by_client_id');

if (is_numeric($clientID))
{
$query->group('cl.client_id');
}

// Group by user
$userID = $this->getState('list.group_by_user_id');

if (is_numeric($userID))
{
$query->group('users.id');
}

// Add the list ordering clause.
$orderCol = $this->state->get('list.ordering');
$orderDirn = $this->state->get('list.direction');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ COM_CLUSTER_LIST_VIEW_DESCRIPTION="Description"
COM_CLUSTER_LIST_VIEW_CLIENT="Client"
COM_CLUSTER_LIST_VIEW_ID = "ID"
COM_CLUSTER_LIST_VIEW_CREATEDBY = "Created By"
COM_CLUSTER_OWNERSHIP_USER ="Select User"

0 comments on commit a19167d

Please sign in to comment.