Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions app/Http/Controllers/Api/OAuth2/OAuth2GroupApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php namespace App\Http\Controllers\Api\OAuth2;
/**
* Copyright 2025 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

use App\Http\Controllers\GetAllTrait;
use App\libs\Auth\Repositories\IGroupRepository;
use App\ModelSerializers\SerializerRegistry;
use OAuth2\IResourceServerContext;
use Utils\Services\ILogService;

/**
* Class OAuth2GroupApiController
* @package App\Http\Controllers\Api\OAuth2
*/
final class OAuth2GroupApiController extends OAuth2ProtectedController
{
use GetAllTrait;

/**
* OAuth2UserApiController constructor.
* @param IGroupRepository $repository
* @param IResourceServerContext $resource_server_context
* @param ILogService $log_service
*/
public function __construct
(
IGroupRepository $repository,
IResourceServerContext $resource_server_context,
ILogService $log_service,
)
{
parent::__construct($resource_server_context, $log_service);
$this->repository = $repository;
}

protected function getAllSerializerType(): string
{
return SerializerRegistry::SerializerType_Public;
}

/**
* @return array
*/
protected function getFilterRules(): array
{
return [
'slug' => ['=='],
];
}

/**
* @return array
*/
protected function getFilterValidatorRules(): array
{
return [
'slug' => 'sometimes|required|string',
];
}

public function getOrderRules(): array
{
return [
'id',
'name',
'slug'
];
}
}
1 change: 0 additions & 1 deletion app/Http/Controllers/Traits/GetAllTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use utils\Filter;
use utils\FilterParser;
use utils\OrderParser;
use utils\PagingInfo;
use Exception;
Expand Down
15 changes: 9 additions & 6 deletions app/Http/Controllers/Traits/ParseFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* limitations under the License.
**/

use Illuminate\Support\Facades\Request;
use App\Http\Utils\Filters\FiltersParams;
use utils\Filter;
use utils\FilterParser;

Expand All @@ -34,15 +34,18 @@ public function getFilter(array $filterRules, array $filterValidationRules = [])
{
$filter = null;

if (Request::has(Filter::FilterRequestParam)) {
$filter = FilterParser::parse(Request::input(Filter::FilterRequestParam), $filterRules);
if (FiltersParams::hasFilterParam()) {
$filter = FilterParser::parse
(
FiltersParams::getFilterParam(),
$filterRules
);
}

if (is_null($filter)) $filter = new Filter();
if(is_null($filter)) $filter = new Filter();

if (count($filterValidationRules)) {
if(count($filterValidationRules)) {
$filter->validate($filterValidationRules);

}

return $filter;
Expand Down
9 changes: 7 additions & 2 deletions app/Http/Utils/Filters/AbstractFilterElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ abstract class AbstractFilterElement
*/
protected $operator;

const OperatorMappings = [
'start_like' => 'like'
];

/**
* @param string $operator
*/
Expand All @@ -28,9 +32,10 @@ protected function __construct($operator)
}

/**
* @return string
* @return string|array
*/
public function getOperator(){
return $this->operator;
if(is_array($this->operator)) return $this->operator;
return isset(self::OperatorMappings[$this->operator]) ? self::OperatorMappings[$this->operator] : $this->operator;
}
}
179 changes: 179 additions & 0 deletions app/Http/Utils/Filters/DoctrineCollectionFieldsFilterMapping.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
<?php namespace utils;
/**
* Copyright 2021 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;

/**
* Class DoctrineCollectionFieldsFilterMapping
* @package App\Http\Utils\Filters
*/
class DoctrineCollectionFieldsFilterMapping extends DoctrineJoinFilterMapping
{
private $allowed_collection_fields = [];

private $joins = [];

/**
* DoctrineCollectionFieldsFilterMapping constructor.
* @param string $table
* @param string $alias
* @param array $joins
* @param array $allowed_collection_fields
*/
public function __construct
(
string $table,
string $alias,
array $joins = [],
array $allowed_collection_fields = []
)
{
$this->allowed_collection_fields = $allowed_collection_fields;
$this->joins = $joins;
parent::__construct($table, $alias, "");
}

/**
* @param FilterElement $filter
* @param array $bindings
* @return string
*/
public function toRawSQL(FilterElement $filter, array $bindings = []):string
{
throw new \Exception;
}

/**
* @param string $exp
* @return FilterElement|null
* @throws FilterParserException
*/
private function parseFilter(string $exp):?FilterElement
{
list ($field, $op, $value) = FilterParser::filterExpresion($exp);
if (!key_exists($field, $this->allowed_collection_fields))
throw new FilterParserException(sprintf("Field %s is not allowed as filter", $field));

return FilterParser::buildFilter($this->allowed_collection_fields[$field], $op, $value);
}

/**
* @param QueryBuilder $query
* @param FilterElement $filter
* @return QueryBuilder
* @throws \models\exceptions\ValidationException
*/
public function apply(QueryBuilder $query, FilterElement $filter):QueryBuilder
{
$value = $filter->getValue();

if (is_array($value)) {

$inner_where = '';

foreach ($value as $val) {

$filterElement = $this->parseFilter($val);
$param_count = $query->getParameters()->count() + 1;
if (!empty($inner_where))
$inner_where .= sprintf(" %s ", $filter->getSameFieldOp());
$inner_where .= sprintf("%s %s %s", $filterElement->getField(), $filterElement->getOperator(), ":value_" . $param_count);
$query->setParameter(":value_" . $param_count, $filterElement->getValue());
}

if (!in_array($this->alias, $query->getAllAliases()))
$query->innerJoin($this->table, $this->alias, Join::WITH);

foreach ($this->joins as $join => $join_alias){
if (!in_array($join_alias, $query->getAllAliases()))
$query->innerJoin(sprintf("%s.%s", $this->alias, $join), $join_alias, Join::WITH);
}

$inner_where = sprintf("( %s )", $inner_where);

if($this->main_operator === Filter::MainOperatorAnd)
return $query->andWhere($inner_where);
else
return $query->orWhere($inner_where);
}

$param_count = $query->getParameters()->count() + 1;
$filterElement = $this->parseFilter($value);
$where = sprintf("%s %s %s", $filterElement->getField(), $filterElement->getOperator(), ":value_" . $param_count);
$query->setParameter(":value_" . $param_count, $filterElement->getValue());
if (!in_array($this->alias, $query->getAllAliases()))
$query->innerJoin($this->table, $this->alias, Join::WITH);

foreach ($this->joins as $join => $join_alias){
if (!in_array($join_alias, $query->getAllAliases()))
$query->innerJoin(sprintf("%s.%s", $this->alias, $join), $join_alias, Join::WITH);
}

if($this->main_operator === Filter::MainOperatorAnd)
return $query->andWhere($where);
else
return $query->orWhere($where);
}

/**
* @param QueryBuilder $query
* @param FilterElement $filter
* @return string
*/
public function applyOr(QueryBuilder $query, FilterElement $filter):string
{
$value = $filter->getValue();

if (is_array($value)) {
$inner_where = '';

foreach ($value as $val) {
$filterElement = $this->parseFilter($val);
$param_count = $query->getParameters()->count() + 1;
if (!empty($inner_where))
$inner_where .= sprintf(" %s ", $filter->getSameFieldOp());
$inner_where .= sprintf("%s %s %s", $filterElement->getField(), $filterElement->getOperator(), ":value_" . $param_count);
$query->setParameter(":value_" . $param_count, $filterElement->getValue());
}

$inner_where = sprintf("( %s )", $inner_where);

if (!in_array($this->alias, $query->getAllAliases()))
$query->innerJoin($this->table, $this->alias, Join::WITH);

foreach ($this->joins as $join => $join_alias){
if (!in_array($join_alias, $query->getAllAliases()))
$query->innerJoin(sprintf("%s.%s", $this->alias, $join), $join_alias, Join::WITH);
}

return $inner_where;
}

$param_count = $query->getParameters()->count() + 1;
$filterElement = $this->parseFilter($value);
$where = sprintf("%s %s %s", $filterElement->getField(), $filterElement->getOperator(), ":value_" . $param_count);
$query->setParameter(":value_" . $param_count, $filterElement->getValue());
if (!in_array($this->alias, $query->getAllAliases()))
$query->innerJoin($this->table, $this->alias, Join::WITH);

foreach ($this->joins as $join => $join_alias){
if (!in_array($join_alias, $query->getAllAliases()))
$query->innerJoin(sprintf("%s.%s", $this->alias, $join), $join_alias, Join::WITH);
}

return $where;
}

}
Loading
Loading