Feature Documentation Required Webservices
avatar hefferbub
hefferbub
10 Sep 2023

The J4 Rest API includes the ability to apply filters when GETing existing users. However, the documentation does not list any examples of how to do so.

By posting on the Joomla forum on StackExchange, I was able to get an example which I tested and it worked:

/api/index.php/v1/users?filter[search]=montgomery-scott&filter[active]=1

It would be great if this could be added to the API documentation. The code shows the wide variety of filtering syntax available:

`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('registrationDateStart', $apiFilterInfo)) {
        $registrationStartInput = $filter->clean($apiFilterInfo['registrationDateStart'], 'STRING');
        $registrationStartDate  = Date::createFromFormat(\DateTimeInterface::RFC3339, $registrationStartInput);

        if (!$registrationStartDate) {
            // Send the error response
            $error = Text::sprintf('JLIB_FORM_VALIDATE_FIELD_INVALID', 'registrationDateStart');

            throw new InvalidParameterException($error, 400, null, 'registrationDateStart');
        }

        $this->modelState->set('filter.registrationDateStart', $registrationStartDate);
    }

    if (\array_key_exists('registrationDateEnd', $apiFilterInfo)) {
        $registrationEndInput = $filter->clean($apiFilterInfo['registrationDateEnd'], 'STRING');
        $registrationEndDate  = Date::createFromFormat(\DateTimeInterface::RFC3339, $registrationEndInput);

        if (!$registrationEndDate) {
            // Send the error response
            $error = Text::sprintf('JLIB_FORM_VALIDATE_FIELD_INVALID', 'registrationDateEnd');
            throw new InvalidParameterException($error, 400, null, 'registrationDateEnd');
        }

        $this->modelState->set('filter.registrationDateEnd', $registrationEndDate);
    } elseif (
        \array_key_exists('registrationDateStart', $apiFilterInfo)
        && !\array_key_exists('registrationDateEnd', $apiFilterInfo)
    ) {
        // If no end date specified the end date is now
        $this->modelState->set('filter.registrationDateEnd', new Date());
    }

    if (\array_key_exists('lastVisitDateStart', $apiFilterInfo)) {
        $lastVisitStartInput = $filter->clean($apiFilterInfo['lastVisitDateStart'], 'STRING');
        $lastVisitStartDate  = Date::createFromFormat(\DateTimeInterface::RFC3339, $lastVisitStartInput);

        if (!$lastVisitStartDate) {
            // Send the error response
            $error = Text::sprintf('JLIB_FORM_VALIDATE_FIELD_INVALID', 'lastVisitDateStart');
            throw new InvalidParameterException($error, 400, null, 'lastVisitDateStart');
        }

        $this->modelState->set('filter.lastVisitStart', $lastVisitStartDate);
    }

    if (\array_key_exists('lastVisitDateEnd', $apiFilterInfo)) {
        $lastVisitEndInput = $filter->clean($apiFilterInfo['lastVisitDateEnd'], 'STRING');
        $lastVisitEndDate  = Date::createFromFormat(\DateTimeInterface::RFC3339, $lastVisitEndInput);

        if (!$lastVisitEndDate) {
            // Send the error response
            $error = Text::sprintf('JLIB_FORM_VALIDATE_FIELD_INVALID', 'lastVisitDateEnd');

            throw new InvalidParameterException($error, 400, null, 'lastVisitDateEnd');
        }

        $this->modelState->set('filter.lastVisitEnd', $lastVisitEndDate);
    } elseif (
        \array_key_exists('lastVisitDateStart', $apiFilterInfo)
        && !\array_key_exists('lastVisitDateEnd', $apiFilterInfo)
    ) {
        // If no end date specified the end date is now
        $this->modelState->set('filter.lastVisitEnd', new Date());
    }

    return parent::displayList();
}`
avatar hefferbub hefferbub - open - 10 Sep 2023
avatar hefferbub hefferbub - change - 10 Sep 2023
Labels Removed: ?
avatar joomla-cms-bot joomla-cms-bot - change - 10 Sep 2023
Labels Added: No Code Attached Yet
avatar joomla-cms-bot joomla-cms-bot - labeled - 10 Sep 2023
avatar alikon alikon - change - 14 Sep 2023
Labels Added: Documentation Required
avatar alikon alikon - labeled - 14 Sep 2023
avatar alikon alikon - change - 14 Sep 2023
Labels Removed: No Code Attached Yet
avatar alikon alikon - unlabeled - 14 Sep 2023
avatar alikon alikon - change - 14 Sep 2023
Labels Added: ?
avatar alikon alikon - labeled - 14 Sep 2023
avatar Hackwar Hackwar - change - 28 Mar 2024
Labels Added: bug Webservices
Removed: ?
avatar Hackwar Hackwar - labeled - 28 Mar 2024
avatar Hackwar Hackwar - change - 28 Mar 2024
Labels Added: Feature
Removed: bug
avatar Hackwar Hackwar - unlabeled - 28 Mar 2024
avatar Hackwar Hackwar - labeled - 28 Mar 2024
avatar rdeutz rdeutz - change - 27 Apr 2024
Status New Closed
Closed_Date 0000-00-00 00:00:00 2024-04-27 12:53:24
Closed_By rdeutz
avatar rdeutz rdeutz - close - 27 Apr 2024
avatar rdeutz
rdeutz - comment - 27 Apr 2024

Anyone can add documentation, not an issue for here.

Add a Comment

Login with GitHub to post a comment