Added new endpoints get organizations

GET /api/v1/organizations

params

Change-Id: I86c864b6a861cdb28382c0cbdbcae405d82eb74f
filter: name
order: id, name
This commit is contained in:
Sebastian Marcet 2018-09-03 15:40:35 -03:00
parent 06e7252276
commit d395d6e3f9
6 changed files with 232 additions and 0 deletions

View File

@ -0,0 +1,131 @@
<?php namespace App\Http\Controllers;
/**
* Copyright 2018 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 models\main\IOrganizationRepository;
use models\oauth2\IResourceServerContext;
use utils\Filter;
use utils\FilterParser;
use utils\FilterParserException;
use utils\OrderParser;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Log;
use utils\PagingInfo;
use models\exceptions\EntityNotFoundException;
use models\exceptions\ValidationException;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
/**
* Class OAuth2OrganizationsApiController
* @package App\Http\Controllers
*/
final class OAuth2OrganizationsApiController extends OAuth2ProtectedController
{
/**
* OAuth2MembersApiController constructor.
* @param IOrganizationRepository $organization_repository
* @param IResourceServerContext $resource_server_context
*/
public function __construct
(
IOrganizationRepository $company_repository,
IResourceServerContext $resource_server_context
)
{
parent::__construct($resource_server_context);
$this->repository = $company_repository;
}
public function getAll(){
$values = Input::all();
$rules = [
'page' => 'integer|min:1',
'per_page' => 'required_with:page|integer|min:5|max:100',
];
try {
$validation = Validator::make($values, $rules);
if ($validation->fails()) {
$ex = new ValidationException();
throw $ex->setMessages($validation->messages()->toArray());
}
// default values
$page = 1;
$per_page = 5;
if (Input::has('page')) {
$page = intval(Input::get('page'));
$per_page = intval(Input::get('per_page'));
}
$filter = null;
if (Input::has('filter')) {
$filter = FilterParser::parse(Input::get('filter'), [
'name' => ['=@', '=='],
]);
}
$order = null;
if (Input::has('order'))
{
$order = OrderParser::parse(Input::get('order'), [
'name',
'id',
]);
}
if(is_null($filter)) $filter = new Filter();
$data = $this->repository->getAllByPage(new PagingInfo($page, $per_page), $filter, $order);
$fields = Request::input('fields', '');
$fields = !empty($fields) ? explode(',', $fields) : [];
$relations = Request::input('relations', '');
$relations = !empty($relations) ? explode(',', $relations) : [];
return $this->ok
(
$data->toArray
(
Request::input('expand', ''),
$fields,
$relations
)
);
}
catch (EntityNotFoundException $ex1) {
Log::warning($ex1);
return $this->error404();
}
catch (ValidationException $ex2) {
Log::warning($ex2);
return $this->error412($ex2->getMessages());
}
catch(FilterParserException $ex3){
Log::warning($ex3);
return $this->error412($ex3->getMessages());
}
catch (\Exception $ex) {
Log::error($ex);
return $this->error500($ex);
}
}
}

View File

@ -71,6 +71,11 @@ Route::group([
Route::get('', 'OAuth2CompaniesApiController@getAll');
});
// organizations
Route::group(['prefix'=>'organizations'], function(){
Route::get('', 'OAuth2OrganizationsApiController@getAll');
});
// groups
Route::group(['prefix'=>'groups'], function(){
Route::get('', 'OAuth2GroupsApiController@getAll');

View File

@ -30,4 +30,25 @@ final class DoctrineOrganizationRepository
{
return Organization::class;
}
/**
* @return array
*/
protected function getFilterMappings()
{
return [
'name' => 'e.name:json_string'
];
}
/**
* @return array
*/
protected function getOrderMappings()
{
return [
'id' => 'e.id',
'name' => 'e.name',
];
}
}

View File

@ -33,6 +33,7 @@ class ApiEndpointsSeeder extends Seeder
$this->seedTagsEndpoints();
$this->seedCompaniesEndpoints();
$this->seedGroupsEndpoints();
$this->seedOrganizationsEndpoints();
}
/**
@ -1855,6 +1856,25 @@ class ApiEndpointsSeeder extends Seeder
);
}
private function seedOrganizationsEndpoints(){
$current_realm = Config::get('app.url');
$this->seedApiEndpoints('organizations', [
// organizations
[
'name' => 'get-organizations',
'route' => '/api/v1/organizations',
'http_method' => 'GET',
'scopes' => [
sprintf(SummitScopes::ReadAllSummitData, $current_realm),
sprintf(SummitScopes::ReadSummitData, $current_realm),
sprintf('%s/organizations/read', $current_realm)
],
]
]
);
}
private function seedGroupsEndpoints(){
$current_realm = Config::get('app.url');

View File

@ -98,5 +98,16 @@ final class ApiSeeder extends Seeder
EntityManager::flush();
// organizations
$api = new Api();
$api->setName('organizations');
$api->setActive(true);
$api->setDescription('Organizations API');
EntityManager::persist($api);
EntityManager::flush();
}
}

View File

@ -0,0 +1,44 @@
<?php
/**
* Copyright 2018 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.
**/
class OAuth2OrganizationsApiTest extends ProtectedApiTest
{
public function testGetOrganizations()
{
$params = [
//AND FILTER
'filter' => ['name=@tip'],
'order' => '-id'
];
$headers = array("HTTP_Authorization" => " Bearer " . $this->access_token);
$response = $this->action(
"GET",
"OAuth2OrganizationsApiController@getAll",
$params,
array(),
array(),
array(),
$headers
);
$content = $response->getContent();
$organizations = json_decode($content);
$this->assertTrue(!is_null($organizations));
$this->assertResponseStatus(200);
}
}