Skip to content
85 changes: 85 additions & 0 deletions api/components/com_users/src/Controller/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@

\defined('_JEXEC') or die;

use Joomla\CMS\Filter\InputFilter;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\ApiController;
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
use Tobscure\JsonApi\Exception\InvalidParameterException;

/**
* The users controller
Expand All @@ -37,6 +40,25 @@ class UsersController extends ApiController
*/
protected $default_view = 'users';

/**
* The default view for the display method.
*
* @var array
* @since 3.0
*/
protected $supportedRange
= [
'past_week',
'past_1month',
'past_3month',
'past_6month',
'past_6month',
'past_year',
'post_year',
'today',
'never',
];

/**
* Method to save a record.
*
Expand Down Expand Up @@ -65,4 +87,67 @@ protected function save($recordKey = null)

return parent::save($recordKey);
}

/**
* User list view with filtering of data
*
* @return static A BaseController object to support chaining.
*
* @since __DEPLOY_VERSION__
*/
public function displayList()
{
$apiFilterInfo = $this->input->get('filter', [], 'array');
$filter = InputFilter::getInstance();

if (array_key_exists('state', $apiFilterInfo))
{
$this->modelState->set('filter.state', $filter->clean($apiFilterInfo['state'], 'INT'));
}

if (array_key_exists('active', $apiFilterInfo))
{
$this->modelState->set('filter.active', $filter->clean($apiFilterInfo['active'], 'INT'));
}

if (array_key_exists('groupid', $apiFilterInfo))
{
$this->modelState->set('filter.group_id', $filter->clean($apiFilterInfo['groupid'], 'INT'));
}

if (array_key_exists('search', $apiFilterInfo))
{
$this->modelState->set('filter.search', $filter->clean($apiFilterInfo['search'], 'STRING'));
}

if (array_key_exists('registrationdate', $apiFilterInfo))
{
$rangeFilter = $filter->clean($apiFilterInfo['registrationdate'], 'STRING');

if (!in_array($rangeFilter, $this->supportedRange))
{
// Send the error response
$error = Text::sprintf('JLIB_FORM_VALIDATE_FIELD_INVALID', 'registrationdate');
throw new InvalidParameterException($error);
}

$this->modelState->set('filter.range', $rangeFilter);
}

if (array_key_exists('lastvisitdate', $apiFilterInfo))
{
$rangeFilter = $filter->clean($apiFilterInfo['lastvisitdate'], 'STRING');

if (!in_array($rangeFilter, $this->supportedRange))
{
// Send the error response
$error = Text::sprintf('JLIB_FORM_VALIDATE_FIELD_INVALID', 'lastvisitdate');
throw new InvalidParameterException($error);
}

$this->modelState->set('filter.lastvisitrange', $rangeFilter);
}

return parent::displayList();
}
}