Added new get groups endpoint

GET api/v1/groups

params:

filter

** code ( available operators : [==, =@]
** title ( available operators : [==, =@]

order

** id
** code
** title

Change-Id: I4e45febb1aa85bb61bd8b4a391e45fb217e2bb95
This commit is contained in:
Sebastian Marcet 2017-11-30 16:49:44 -03:00
parent 56b33f3926
commit 84a58c9eab
14 changed files with 278 additions and 18 deletions

View File

@ -24,7 +24,6 @@ use models\exceptions\EntityNotFoundException;
use models\exceptions\ValidationException;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
/**
* Class OAuth2CompaniesApiController
* @package App\Http\Controllers
@ -46,7 +45,7 @@ final class OAuth2CompaniesApiController extends OAuth2ProtectedController
$this->repository = $company_repository;
}
public function getCompanies(){
public function getAll(){
$values = Input::all();

View File

@ -0,0 +1,134 @@
<?php namespace App\Http\Controllers;
/**
* Copyright 2017 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\IGroupRepository;
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 OAuth2GroupsApiController
* @package App\Http\Controllers
*/
final class OAuth2GroupsApiController extends OAuth2ProtectedController
{
/**
* OAuth2MembersApiController constructor.
* @param IGroupRepository $group_repository
* @param IResourceServerContext $resource_server_context
*/
public function __construct
(
IGroupRepository $group_repository,
IResourceServerContext $resource_server_context
)
{
parent::__construct($resource_server_context);
$this->repository = $group_repository;
}
public function getAll(){
$values = Input::all();
$rules = array
(
'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'), array
(
'code' => ['=@', '=='],
'title' => ['=@', '=='],
));
}
$order = null;
if (Input::has('order'))
{
$order = OrderParser::parse(Input::get('order'), array
(
'code',
'title',
'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

@ -46,7 +46,7 @@ final class OAuth2MembersApiController extends OAuth2ProtectedController
$this->repository = $member_repository;
}
public function getMembers(){
public function getAll(){
$values = Input::all();

View File

@ -47,7 +47,7 @@ final class OAuth2TagsApiController extends OAuth2ProtectedController
$this->repository = $tag_repository;
}
public function getTags(){
public function getAll(){
$values = Input::all();

View File

@ -156,7 +156,7 @@ final class OAuth2SummitSpeakersApiController extends OAuth2ProtectedController
}
public function getAllSpeakers(){
public function getAll(){
try {
$values = Input::all();

View File

@ -27,7 +27,7 @@ Route::group([
], function(){
// members
Route::group(['prefix'=>'members'], function() {
Route::get('', 'OAuth2MembersApiController@getMembers');
Route::get('', 'OAuth2MembersApiController@getAll');
});
// summits
@ -84,7 +84,7 @@ Route::group([
// members
Route::group(['prefix'=>'members'], function(){
Route::get('', 'OAuth2MembersApiController@getMembers');
Route::get('', 'OAuth2MembersApiController@getAll');
Route::group(['prefix'=>'me'], function(){
// get my member info
@ -104,12 +104,17 @@ Route::group([
// tags
Route::group(['prefix'=>'tags'], function(){
Route::get('', 'OAuth2TagsApiController@getTags');
Route::get('', 'OAuth2TagsApiController@getAll');
});
// companies
Route::group(['prefix'=>'companies'], function(){
Route::get('', 'OAuth2CompaniesApiController@getCompanies');
Route::get('', 'OAuth2CompaniesApiController@getAll');
});
// groups
Route::group(['prefix'=>'groups'], function(){
Route::get('', 'OAuth2GroupsApiController@getAll');
});
// teams
@ -300,9 +305,8 @@ Route::group([
});
// speakers
Route::group(array('prefix' => 'speakers'), function () {
Route::get('', 'OAuth2SummitSpeakersApiController@getAllSpeakers');
Route::get('', 'OAuth2SummitSpeakersApiController@getAll');
});
});

View File

@ -24,6 +24,28 @@ final class DoctrineGroupRepository
implements IGroupRepository
{
/**
* @return array
*/
protected function getFilterMappings()
{
return [
'code' => 'e.code:json_string',
'title' => 'e.title:json_string',
];
}
/**
* @return array
*/
protected function getOrderMappings()
{
return [
'id' => 'e.id',
'code' => 'e.code',
'title' => 'e.title',
];
}
/**
* @return string
*/

View File

@ -33,6 +33,7 @@ class ApiEndpointsSeeder extends Seeder
$this->seedTeamEndpoints();
$this->seedTagsEndpoints();
$this->seedCompaniesEndpoints();
$this->seedGroupsEndpoints();
}
/**
@ -512,6 +513,24 @@ class ApiEndpointsSeeder extends Seeder
);
}
private function seedGroupsEndpoints(){
$current_realm = Config::get('app.url');
$this->seedApiEndpoints('groups', [
// members
array(
'name' => 'get-groups',
'route' => '/api/v1/groups',
'http_method' => 'GET',
'scopes' => [
sprintf('%s/summits/read', $current_realm),
sprintf('%s/groups/read', $current_realm)
],
)
]
);
}
private function seedTeamEndpoints(){
$current_realm = Config::get('app.url');

View File

@ -34,6 +34,7 @@ final class ApiScopesSeeder extends Seeder
$this->seedTeamsScopes();
$this->seedTagsScopes();
$this->seedCompaniesScopes();
$this->seedGroupsScopes();
}
private function seedSummitScopes()
@ -214,6 +215,32 @@ final class ApiScopesSeeder extends Seeder
EntityManager::flush();
}
private function seedGroupsScopes(){
$current_realm = Config::get('app.url');
$api = EntityManager::getRepository(\App\Models\ResourceServer\Api::class)->findOneBy(['name' => 'groups']);
$scopes = [
array(
'name' => sprintf('%s/groups/read', $current_realm),
'short_description' => 'Get Groups Data',
'description' => 'Grants read only access for Groups Data',
),
];
foreach ($scopes as $scope_info) {
$scope = new ApiScope();
$scope->setName($scope_info['name']);
$scope->setShortDescription($scope_info['short_description']);
$scope->setDescription($scope_info['description']);
$scope->setActive(true);
$scope->setDefault(false);
$scope->setApi($api);
EntityManager::persist($scope);
}
EntityManager::flush();
}
private function seedTeamsScopes(){
$current_realm = Config::get('app.url');
$api = EntityManager::getRepository(\App\Models\ResourceServer\Api::class)->findOneBy(['name' => 'teams']);

View File

@ -64,7 +64,7 @@ final class ApiSeeder extends Seeder
EntityManager::flush();
//tags
//companies
$api = new Api();
$api->setName('companies');
@ -75,6 +75,17 @@ final class ApiSeeder extends Seeder
EntityManager::flush();
//groups
$api = new Api();
$api->setName('groups');
$api->setActive(true);
$api->setDescription('groups API');
EntityManager::persist($api);
EntityManager::flush();
// teams

View File

@ -27,7 +27,7 @@ class OAuth2CompaniesApiTest extends ProtectedApiTest
$headers = array("HTTP_Authorization" => " Bearer " . $this->access_token);
$response = $this->action(
"GET",
"OAuth2CompaniesApiController@getCompanies",
"OAuth2CompaniesApiController@getAll",
$params,
array(),
array(),

View File

@ -0,0 +1,44 @@
<?php
/**
* Copyright 2017 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 OAuth2GroupsApiTest extends ProtectedApiTest
{
public function testGetGroups()
{
$params = [
//AND FILTER
'filter' => ['code=@adm'],
'order' => '-id'
];
$headers = array("HTTP_Authorization" => " Bearer " . $this->access_token);
$response = $this->action(
"GET",
"OAuth2GroupsApiController@getAll",
$params,
array(),
array(),
array(),
$headers
);
$content = $response->getContent();
$groups = json_decode($content);
$this->assertTrue(!is_null($groups));
$this->assertResponseStatus(200);
}
}

View File

@ -27,7 +27,7 @@ final class OAuth2MembersApiTest extends ProtectedApiTest
$headers = array("HTTP_Authorization" => " Bearer " . $this->access_token);
$response = $this->action(
"GET",
"OAuth2MembersApiController@getMembers",
"OAuth2MembersApiController@getAll",
$params,
array(),
array(),
@ -52,7 +52,7 @@ final class OAuth2MembersApiTest extends ProtectedApiTest
$headers = array("HTTP_Authorization" => " Bearer " . $this->access_token);
$response = $this->action(
"GET",
"OAuth2MembersApiController@getMembers",
"OAuth2MembersApiController@getAll",
$params,
array(),
array(),
@ -75,7 +75,7 @@ final class OAuth2MembersApiTest extends ProtectedApiTest
$headers = array("HTTP_Authorization" => " Bearer " . $this->access_token);
$response = $this->action(
"GET",
"OAuth2MembersApiController@getMembers",
"OAuth2MembersApiController@getAll",
$params,
array(),
array(),
@ -123,7 +123,7 @@ final class OAuth2MembersApiTest extends ProtectedApiTest
$headers = array("HTTP_Authorization" => " Bearer " . $this->access_token);
$response = $this->action(
"GET",
"OAuth2MembersApiController@getMembers",
"OAuth2MembersApiController@getAll",
$params,
array(),
array(),

View File

@ -27,7 +27,7 @@ class OAuth2TagsApiTest extends ProtectedApiTest
$headers = array("HTTP_Authorization" => " Bearer " . $this->access_token);
$response = $this->action(
"GET",
"OAuth2TagsApiController@getTags",
"OAuth2TagsApiController@getAll",
$params,
array(),
array(),