Commit 0619a8c8 authored by François Agneray's avatar François Agneray
Browse files

Management of user groups by instance

parent a5e4ce96
Pipeline #3592 passed with stages
in 1 minute and 51 seconds
......@@ -24,12 +24,12 @@ $app->group('', function (RouteCollectorProxy $group) {
$group->map([OPTIONS, GET], '/database/{id}/table', App\Action\TableListAction::class);
$group->map([OPTIONS, GET, POST], '/project', App\Action\ProjectListAction::class);
$group->map([OPTIONS, GET, PUT, DELETE], '/project/{name}', App\Action\ProjectAction::class);
$group->map([OPTIONS, GET, POST], '/group', App\Action\GroupListAction::class);
$group->map([OPTIONS, GET, PUT, DELETE], '/group/{id}', App\Action\GroupAction::class);
$group->map([OPTIONS, GET, POST], '/instance', App\Action\InstanceListAction::class);
$group->map([OPTIONS, GET, PUT, DELETE], '/instance/{name}', App\Action\InstanceAction::class);
$group->map([OPTIONS, GET, POST], '/instance/{name}/group', App\Action\GroupListAction::class);
$group->map([OPTIONS, GET, POST], '/instance/{name}/dataset-family', App\Action\DatasetFamilyListAction::class);
$group->map([OPTIONS, GET], '/instance/{name}/dataset', App\Action\DatasetListByInstanceAction::class);
$group->map([OPTIONS, GET, PUT, DELETE], '/group/{id}', App\Action\GroupAction::class);
$group->map([OPTIONS, GET, PUT, DELETE], '/dataset-family/{id}', App\Action\DatasetFamilyAction::class);
$group->map([OPTIONS, GET, POST], '/dataset-family/{id}/dataset', App\Action\DatasetListAction::class);
$group->map([OPTIONS, GET, PUT, DELETE], '/dataset/{name}', App\Action\DatasetAction::class);
......
......@@ -196,5 +196,5 @@ curl -d '{"id":57,"name":"spec1dnoise","table_name":"aspic_vipers_dr2_w1","label
curl -d '{"id":58,"name":"spec1dsky","table_name":"aspic_vipers_dr2_w1","label":"spec1dsky","form_label":"spec1dsky","description":null,"output_display":580,"criteria_display":580,"search_flag":null,"search_type":null,"type":"text","operator":null,"min":null,"max":null,"placeholder_min":null,"placeholder_max":null,"renderer":"download","renderer_config":{"display":"icon-button","text":"DOWNLOAD","icon":"fas fa-download"},"display_detail":580,"selected":true,"order_by":false,"order_display":580,"detail":true,"renderer_detail":null,"options":null,"vo_utype":null,"vo_ucd":null,"vo_unit":null,"vo_description":null,"vo_datatype":null,"vo_size":null,"id_criteria_family":null,"id_output_category":8}' --header 'Content-Type: application/json' --header "Authorization: Bearer $admin_token" -X PUT http://localhost/dataset/vipers_dr2_w1/attribute/58
# Add groups
curl -d '{"label":"SVOM","datasets":["obs_cat","observations"]}' --header 'Content-Type: application/json' --header "Authorization: Bearer $admin_token" -X POST http://localhost/group
curl -d '{"label":"ASPIC","datasets":[]}' --header 'Content-Type: application/json' --header "Authorization: Bearer $admin_token" -X POST http://localhost/group
curl -d '{"label":"SVOM","datasets":["obs_cat","observations"]}' --header 'Content-Type: application/json' --header "Authorization: Bearer $admin_token" -X POST http://localhost/instance/default/group
curl -d '{"label":"ASPIC","datasets":[]}' --header 'Content-Type: application/json' --header "Authorization: Bearer $admin_token" -X POST http://localhost/instance/default/group
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAj2lDSbVhNIgJl6C91sgQuVPWWo9DN5c4a89i1scqizMutt0Vre+C9c0Zxko/DnkThnFZBAaE5e7gL7mwRWFY8yQAZWi1e03nZfL+zr8Z7ppLyUdDo4pGT6D62LdVglwDGgBvRtobI8CvRLQO0TQr+I09j/hFAHUnmRMvL9ZvyIBmC8Cje5AANelObInto8oBLXaptMr7BcTFtd/0PFuXEUMH9qlQ+FWNGyryVPExW3pqYRcaFwBs0clF52q6iBE3m6vzA5sSv+tO/tPOnSghvAtD9zSyQ9OHldgh070vSJM4t0I6feMYJkOn0SOa2ZcLxkop5gwe852hctGo/UCatwIDAQAB
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsoH0IhiG5WNeUu/UytNnEIO5Uf9PXdFknRG4L6c2xuIQeqb6Bn/Xco5R9aV2upn0nk77LvPVG1cmjgl96rgfsYsWB7rPxy7BLhb7xWU0LNOQMuvClWxhDeNQyKE8H1URgHMQjhu+i0hQnXBVDK2fantbjLeo4v30DuVw2ZIco8YBzKI7ENDEcMLdCV5FI7Jq0XNG8ZCzd0ocg9UuwMTPREWHN0SZyr2V4HPmYASNokPuO9fNhekIii7yLWxU2K/qpTiSFMxDORRjtMalHjKXWZBGtipZ6LAb42BFNx0HDUYA7uUcZEk0Fr5KVcN1qu4hoamyj5L40quljCHFakqRLQIDAQAB
-----END PUBLIC KEY-----
......@@ -39,7 +39,7 @@ final class DatasetFamilyListAction extends AbstractAction
$instance = $this->em->find('App\Entity\Instance', $args['name']);
// Returns HTTP 404 if the dataset is not found
// Returns HTTP 404 if the instance is not found
if (is_null($instance)) {
throw new HttpNotFoundException(
$request,
......
......@@ -14,7 +14,9 @@ namespace App\Action;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Exception\HttpNotFoundException;
use Slim\Exception\HttpBadRequestException;
use App\Entity\Instance;
use App\Entity\Group;
final class GroupListAction extends AbstractAction
......@@ -35,9 +37,19 @@ final class GroupListAction extends AbstractAction
return $response->withHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
}
$instance = $this->em->find('App\Entity\Instance', $args['name']);
// Returns HTTP 404 if the instance is not found
if (is_null($instance)) {
throw new HttpNotFoundException(
$request,
'Instance with name ' . $args['name'] . ' is not found'
);
}
if ($request->getMethod() === GET) {
// Retrieve group with id
$groups = $this->em->getRepository('App\Entity\Group')->findAll();
$groups = $this->em->getRepository('App\Entity\Group')->findByInstance($instance);
$payload = json_encode($groups);
}
......@@ -54,7 +66,7 @@ final class GroupListAction extends AbstractAction
}
}
$group = $this->postGroup($parsedBody);
$group = $this->postGroup($parsedBody, $instance);
$payload = json_encode($group);
$response = $response->withStatus(201);
}
......@@ -64,13 +76,16 @@ final class GroupListAction extends AbstractAction
}
/**
* Add a new group into the metamodel
* Add a new group for an instance into the metamodel
*
* @param array $parsedBody Contains the values ​​of the new group sent by the user
* @param Instance $instance Instance in which the group will be added
*
* @return Group The newly created group
*/
private function postGroup(array $parsedBody): Group
private function postGroup(array $parsedBody, Instance $instance): Group
{
$group = new Group();
$group = new Group($instance);
$group->setLabel($parsedBody['label']);
$group->setDatasets($this->getDatasets($parsedBody['datasets']));
......
......@@ -88,6 +88,11 @@ class DatasetFamily implements \JsonSerializable
$this->display = $display;
}
public function getInstance()
{
return $this->instance;
}
public function getDatasets()
{
return $this->datasets;
......
......@@ -34,6 +34,14 @@ class Group implements \JsonSerializable
*/
protected $label;
/**
* @var Anis\Entity\Instance
*
* @ManyToOne(targetEntity="Instance")
* @JoinColumn(name="instance_name", referencedColumnName="name", nullable=false)
*/
protected $instance;
/**
* Many Groups have Many Datasets privileges.
*
......@@ -46,6 +54,11 @@ class Group implements \JsonSerializable
*/
protected $datasets;
public function __construct(Instance $instance)
{
$this->instance = $instance;
}
public function getId()
{
return $this->id;
......@@ -61,6 +74,11 @@ class Group implements \JsonSerializable
$this->label = $label;
}
public function getInstance()
{
return $this->instance;
}
public function getDatasets()
{
return $this->datasets;
......@@ -81,6 +99,7 @@ class Group implements \JsonSerializable
return [
'id' => $this->getId(),
'label' => $this->getLabel(),
'instance_name' => $this->getInstance()->getName(),
'datasets' => $datasets
];
}
......
......@@ -79,7 +79,15 @@ final class GroupActionTest extends TestCase
$this->addAGroup();
$request = $this->getRequest('PUT')->withParsedBody($fields);
$response = ($this->action)($request, new Response(), array('id' => 1));
$this->assertSame(json_encode(array_merge(['id' => 1], $fields)), (string) $response->getBody());
$this->assertSame(
json_encode(array(
'id' => 1,
'label' => $fields['label'],
'instance_name' => 'default',
'datasets' => $fields['datasets']
)),
(string) $response->getBody()
);
}
public function testEditAGroupWithDataset(): void
......@@ -89,10 +97,18 @@ final class GroupActionTest extends TestCase
'label' => 'New_label',
'datasets' => array($dataset->getName())
);
$this->addAGroup();
$this->addAGroup($dataset->getDatasetFamily()->getInstance());
$request = $this->getRequest('PUT')->withParsedBody($fields);
$response = ($this->action)($request, new Response(), array('id' => 1));
$this->assertSame(json_encode(array_merge(['id' => 1], $fields)), (string) $response->getBody());
$this->assertSame(
json_encode(array(
'id' => 1,
'label' => $fields['label'],
'instance_name' => 'default',
'datasets' => $fields['datasets']
)),
(string) $response->getBody()
);
}
public function testDeleteAGroup(): void
......@@ -118,9 +134,13 @@ final class GroupActionTest extends TestCase
));
}
private function addAGroup(): Group
private function addAGroup($instance = null): Group
{
$group = new Group();
if (!$instance) {
$instance = $this->addInstance();
}
$group = new Group($instance);
$group->setLabel('Group1');
$group->setDatasets(array());
$this->entityManager->persist($group);
......@@ -154,8 +174,8 @@ final class GroupActionTest extends TestCase
private function addInstance(): Instance
{
$instance = new Instance('aspic', 'Aspic');
$instance->setClientUrl('http://cesam.lam.fr/aspic');
$instance = new Instance('default', 'Default instance');
$instance->setClientUrl('http://anis.lam.fr');
$this->entityManager->persist($instance);
$this->entityManager->flush();
return $instance;
......
......@@ -42,11 +42,11 @@ final class GroupListActionTest extends TestCase
$this->assertSame($response->getHeaderLine('Access-Control-Allow-Methods'), 'GET, POST, OPTIONS');
}
public function testGetAllGroups(): void
public function testGetAllGroupsOfAnInstanc(): void
{
$groups = $this->addGroups();
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array());
$response = ($this->action)($request, new Response(), array('name' => 'default'));
$this->assertSame(
json_encode($groups),
(string) $response->getBody()
......@@ -55,10 +55,11 @@ final class GroupListActionTest extends TestCase
public function testAddANewGroupEmptyLabelField(): void
{
$this->addInstance();
$this->expectException(HttpBadRequestException::class);
$this->expectExceptionMessage('Param label needed to add a new group');
$request = $this->getRequest('POST')->withParsedBody(array());
$response = ($this->action)($request, new Response(), array());
$response = ($this->action)($request, new Response(), array('name' => 'default'));
$this->assertEquals(400, (int) $response->getStatusCode());
}
......@@ -68,10 +69,16 @@ final class GroupListActionTest extends TestCase
'label' => 'group1',
'datasets' => []
);
$this->addInstance();
$request = $this->getRequest('POST')->withParsedBody($fields);
$response = ($this->action)($request, new Response(), array());
$response = ($this->action)($request, new Response(), array('name' => 'default'));
$this->assertSame(
json_encode(array_merge(['id' => 1], $fields)),
json_encode(array(
'id' => 1,
'label' => $fields['label'],
'instance_name' => 'default',
'datasets' => $fields['datasets']
)),
(string) $response->getBody()
);
$this->assertEquals(201, (int) $response->getStatusCode());
......@@ -85,9 +92,14 @@ final class GroupListActionTest extends TestCase
'datasets' => array($dataset->getName())
);
$request = $this->getRequest('POST')->withParsedBody($fields);
$response = ($this->action)($request, new Response(), array());
$response = ($this->action)($request, new Response(), array('name' => 'default'));
$this->assertSame(
json_encode(array_merge(['id' => 1], $fields)),
json_encode(array(
'id' => 1,
'label' => $fields['label'],
'instance_name' => 'default',
'datasets' => $fields['datasets']
)),
(string) $response->getBody()
);
$this->assertEquals(201, (int) $response->getStatusCode());
......@@ -107,12 +119,14 @@ final class GroupListActionTest extends TestCase
private function addGroups(): array
{
$group = new Group();
$instance = $this->addInstance();
$group = new Group($instance);
$group->setLabel('Group1');
$group->setDatasets(array());
$this->entityManager->persist($group);
$group2 = new Group();
$group2 = new Group($instance);
$group2->setLabel('Group2');
$group2->setDatasets(array());
$this->entityManager->persist($group2);
......@@ -147,8 +161,8 @@ final class GroupListActionTest extends TestCase
private function addInstance(): Instance
{
$instance = new Instance('aspic', 'Aspic');
$instance->setClientUrl('http://cesam.lam.fr/aspic');
$instance = new Instance('default', 'Default instance');
$instance->setClientUrl('http://anis.lam.fr');
$this->entityManager->persist($instance);
$this->entityManager->flush();
return $instance;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment