Commit 41f4acb9 authored by François Agneray's avatar François Agneray

Merge branch '57-user-group-et-priviliges-dans-la-metamodel' into 'develop'

Resolve "User, group et priviliges dans la metamodel"

Closes #57

See merge request !52
parents 666ed8ea e060333f
Pipeline #3320 passed with stages
in 1 minute and 56 seconds
3.4.0
\ No newline at end of file
3.5.0
\ No newline at end of file
......@@ -76,6 +76,22 @@ $container->set('App\Action\ProjectAction', function (ContainerInterface $c) {
return new App\Action\ProjectAction($c->get('em'));
});
$container->set('App\Action\UserListAction', function (ContainerInterface $c) {
return new App\Action\UserListAction($c->get('em'));
});
$container->set('App\Action\UserAction', function (ContainerInterface $c) {
return new App\Action\UserAction($c->get('em'));
});
$container->set('App\Action\GroupListAction', function (ContainerInterface $c) {
return new App\Action\GroupListAction($c->get('em'));
});
$container->set('App\Action\GroupAction', function (ContainerInterface $c) {
return new App\Action\GroupAction($c->get('em'));
});
$container->set('App\Action\FamilyListAction', function (ContainerInterface $c) {
return new App\Action\FamilyListAction($c->get('em'));
});
......
......@@ -18,6 +18,10 @@ $app->map([OPTIONS, GET, PUT, DELETE], '/database/{id}', App\Action\DatabaseActi
$app->map([OPTIONS, GET], '/database/{id}/table', App\Action\TableListAction::class);
$app->map([OPTIONS, GET, POST], '/project', App\Action\ProjectListAction::class);
$app->map([OPTIONS, GET, PUT, DELETE], '/project/{name}', App\Action\ProjectAction::class);
$app->map([OPTIONS, GET, POST], '/user', App\Action\UserListAction::class);
$app->map([OPTIONS, GET, PUT, DELETE], '/user/{email}', App\Action\UserAction::class);
$app->map([OPTIONS, GET, POST], '/group', App\Action\GroupListAction::class);
$app->map([OPTIONS, GET, PUT, DELETE], '/group/{id}', App\Action\GroupAction::class);
$app->map([OPTIONS, GET, POST], '/instance', App\Action\InstanceListAction::class);
$app->map([OPTIONS, GET, PUT, DELETE], '/instance/{name}', App\Action\InstanceAction::class);
$app->map([OPTIONS, GET, POST], '/instance/{name}/dataset-family', App\Action\DatasetFamilyListAction::class);
......
......@@ -17,10 +17,10 @@ curl -d '{"name":"colibri","label":"Colibri Project Test","description":"Project
# Add default dataset family and ANIS datasets
curl -d '{"label":"Default dataset family","display":10}' -H "Content-Type: application/json" -X POST http://localhost/instance/default/dataset-family
curl -d '{"name":"obs_cat","table_ref":"obs_cat","label":"ObsCat dataset","description":"ObsCat","display":10,"count":10000,"vo":false,"data_path":"/mnt/mount","config":{"cone_search":{"enabled":true,"column_ra":2,"column_dec":3},"selectable_row":true},"project_name":"anis_project"}' -H "Content-Type: application/json" -X POST http://localhost/dataset-family/1/dataset
curl -d '{"name":"observations","table_ref":"observations_info","label":"Observations dataset","description":"Observations","display":20,"count":177454,"vo":false,"data_path":"/mnt/mount","config":{"selectable_row":false},"project_name":"anis_project"}' -H "Content-Type: application/json" -X POST http://localhost/dataset-family/1/dataset
curl -d '{"name":"vipers_dr2_w1","table_ref":"aspic_vipers_dr2_w1","label":"VIPERS-W1 (DR2)","description":"VIPERS W1 dataset","display":30,"count":1000,"vo":false,"data_path":"/mnt/mount","config":{"selectable_row":false},"project_name":"anis_project"}' -H "Content-Type: application/json" -X POST http://localhost/dataset-family/1/dataset
curl -d '{"name":"gama_g02_dr3","table_ref":"aspic_gama_g02","label":"GAMA G02 (DR3)","description":"GAMA G02 dataset","display":40,"count":8,"vo":false,"data_path":"/mnt/mount","config":{"selectable_row":false},"project_name":"anis_project"}' -H "Content-Type: application/json" -X POST http://localhost/dataset-family/1/dataset
curl -d '{"name":"obs_cat","table_ref":"obs_cat","label":"ObsCat dataset","description":"ObsCat","display":10,"count":10000,"vo":false,"data_path":"/mnt/mount","config":{"cone_search":{"enabled":true,"column_ra":2,"column_dec":3},"selectable_row":true},"public":true,"project_name":"anis_project"}' -H "Content-Type: application/json" -X POST http://localhost/dataset-family/1/dataset
curl -d '{"name":"observations","table_ref":"observations_info","label":"Observations dataset","description":"Observations","display":20,"count":177454,"vo":false,"data_path":"/mnt/mount","config":{"selectable_row":false},"public":true,"project_name":"anis_project"}' -H "Content-Type: application/json" -X POST http://localhost/dataset-family/1/dataset
curl -d '{"name":"vipers_dr2_w1","table_ref":"aspic_vipers_dr2_w1","label":"VIPERS-W1 (DR2)","description":"VIPERS W1 dataset","display":30,"count":1000,"vo":false,"data_path":"/mnt/mount","config":{"selectable_row":false},"public":true,"project_name":"anis_project"}' -H "Content-Type: application/json" -X POST http://localhost/dataset-family/1/dataset
curl -d '{"name":"gama_g02_dr3","table_ref":"aspic_gama_g02","label":"GAMA G02 (DR3)","description":"GAMA G02 dataset","display":40,"count":8,"vo":false,"data_path":"/mnt/mount","config":{"selectable_row":false},"public":true,"project_name":"anis_project"}' -H "Content-Type: application/json" -X POST http://localhost/dataset-family/1/dataset
# Add ANIS obs_cat default criteria family, default output family and default output category
curl -d '{"label":"Default criteria family","display":10}' -H "Content-Type: application/json" -X POST http://localhost/dataset/obs_cat/criteria-family
......@@ -29,9 +29,9 @@ curl -d '{"label":"Default output category","display":10}' -H "Content-Type: app
# Add SVOM dataset family and SVOM datasets
curl -d '{"label":"Svom dataset family","display":20}' -H "Content-Type: application/json" -X POST http://localhost/instance/default/dataset-family
curl -d '{"name":"l1","table_ref":"public.v_rawproducts","label":"L0 & L1","description":"L0 & L1 products list","display":10,"count":0,"vo":false,"data_path":"/mnt/mount","config":{"results_server_link":{"enabled":true},"selectable_row":false},"project_name":"svom"}' -H "Content-Type: application/json" -X POST http://localhost/dataset-family/2/dataset
curl -d '{"name":"products","table_ref":"public.v_products","label":"Scientific Products","description":"SR3 & SR4 products list","display":20,"count":0,"vo":false,"data_path":"/mnt/mount","config":{"results_server_link":{"enabled":true},"selectable_row":false},"project_name":"svom"}' -H "Content-Type: application/json" -X POST http://localhost/dataset-family/2/dataset
curl -d '{"name":"sp_cards","table_ref":"sp_cards","label":"SP Metadata","description":"Contains metadata of scientific products (Core Program & General Program)","display":30,"count":100,"vo":false,"data_path":"/mnt/mount","config":{"results_server_link":{"enabled":true},"selectable_row":false},"project_name":"svom"}' -H "Content-Type: application/json" -X POST http://localhost/dataset-family/2/dataset
curl -d '{"name":"l1","table_ref":"public.v_rawproducts","label":"L0 & L1","description":"L0 & L1 products list","display":10,"count":0,"vo":false,"data_path":"/mnt/mount","config":{"results_server_link":{"enabled":true},"selectable_row":false},"public":true,"project_name":"svom"}' -H "Content-Type: application/json" -X POST http://localhost/dataset-family/2/dataset
curl -d '{"name":"products","table_ref":"public.v_products","label":"Scientific Products","description":"SR3 & SR4 products list","display":20,"count":0,"vo":false,"data_path":"/mnt/mount","config":{"results_server_link":{"enabled":true},"selectable_row":false},"public":true,"project_name":"svom"}' -H "Content-Type: application/json" -X POST http://localhost/dataset-family/2/dataset
curl -d '{"name":"sp_cards","table_ref":"sp_cards","label":"SP Metadata","description":"Contains metadata of scientific products (Core Program & General Program)","display":30,"count":100,"vo":false,"data_path":"/mnt/mount","config":{"results_server_link":{"enabled":true},"selectable_row":false},"public":true,"project_name":"svom"}' -H "Content-Type: application/json" -X POST http://localhost/dataset-family/2/dataset
# Add SVOM l1 criteria families, default output family and default output category
curl -d '{"label":"Obs","display":10}' -H "Content-Type: application/json" -X POST http://localhost/dataset/l1/criteria-family
......@@ -100,7 +100,7 @@ curl -d '{"id":9,"name":"json_schema_uploaded","table_name":"sp_cards","label":"
# Add COLIBRI dataset family and COLIBRI dataset
curl -d '{"label":"Colibri dataset family","display":30}' -H "Content-Type: application/json" -X POST http://localhost/instance/default/dataset-family
curl -d '{"name":"anis_observation","table_ref":"anis_observation","label":"COLIBRI OBS","description":"colibri observations","display":10,"count":0,"vo":true,"data_path":"/mnt/mount","config":{"cone_search":{"enabled":true,"column_ra":2,"column_dec":3},"selectable_row":true},"project_name":"colibri"}' -H "Content-Type: application/json" -X POST http://localhost/dataset-family/3/dataset
curl -d '{"name":"anis_observation","table_ref":"anis_observation","label":"COLIBRI OBS","description":"colibri observations","display":10,"count":0,"vo":true,"data_path":"/mnt/mount","config":{"cone_search":{"enabled":true,"column_ra":2,"column_dec":3},"selectable_row":true},"public":true,"project_name":"colibri"}' -H "Content-Type: application/json" -X POST http://localhost/dataset-family/3/dataset
# Add COLIBRI anis_observation default criteria family, output families and output categories
curl -d '{"label":"Default criteria family","display":10}' -H "Content-Type: application/json" -X POST http://localhost/dataset/anis_observation/criteria-family
......@@ -140,3 +140,11 @@ curl -d '{"id":9,"name":"zflg","table_name":"aspic_vipers_dr2_w1","label":"zflg"
curl -d '{"id":56,"name":"spec1d","table_name":"aspic_vipers_dr2_w1","label":"spec1d","form_label":"spec1d","description":null,"output_display":560,"criteria_display":560,"search_flag":"SPECTRUM_1D","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":560,"selected":true,"order_by":false,"order_display":560,"detail":true,"renderer_detail":"spectra_graph","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}' -H "Content-Type: application/json" -X PUT http://localhost/dataset/vipers_dr2_w1/attribute/56
curl -d '{"id":57,"name":"spec1dnoise","table_name":"aspic_vipers_dr2_w1","label":"spec1dnoise","form_label":"spec1dnoise","description":null,"output_display":570,"criteria_display":570,"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":570,"selected":true,"order_by":false,"order_display":570,"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}' -H "Content-Type: application/json" -X PUT http://localhost/dataset/vipers_dr2_w1/attribute/57
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}' -H "Content-Type: application/json" -X PUT http://localhost/dataset/vipers_dr2_w1/attribute/58
# Add users
curl -d '{"email":"charles.degaulle@lam.fr"}' -H "Content-Type: application/json" -X POST http://localhost/user
curl -d '{"email":"georges.pompidou@lam.fr"}' -H "Content-Type: application/json" -X POST http://localhost/user
# Add groups
curl -d '{"label":"SVOM","users":["charles.degaulle@lam.fr"],"datasets":["obs_cat","observations"]}' -H "Content-Type: application/json" -X POST http://localhost/group
curl -d '{"label":"ASPIC","users":[],"datasets":[]}' -H "Content-Type: application/json" -X POST http://localhost/group
......@@ -48,7 +48,7 @@ final class DatasetAction extends AbstractAction
'Dataset with name ' . $args['name'] . ' is not found'
);
}
if ($request->getMethod() === GET) {
$payload = json_encode($dataset, JSON_UNESCAPED_SLASHES);
}
......@@ -65,6 +65,7 @@ final class DatasetAction extends AbstractAction
'vo',
'data_path',
'config',
'public',
'id_dataset_family'
);
foreach ($fields as $a) {
......@@ -84,7 +85,7 @@ final class DatasetAction extends AbstractAction
'Dataset family with id ' . $parsedBody['id_dataset_family'] . ' is not found'
);
}
$this->editDataset($dataset, $parsedBody, $family);
$payload = json_encode($dataset, JSON_UNESCAPED_SLASHES);
}
......@@ -117,6 +118,7 @@ final class DatasetAction extends AbstractAction
$dataset->setVo($parsedBody['vo']);
$dataset->setDataPath($parsedBody['data_path']);
$dataset->setConfig($parsedBody['config']);
$dataset->setPublic($parsedBody['public']);
$this->em->flush();
}
}
......@@ -84,6 +84,7 @@ final class DatasetListAction extends AbstractAction
'vo',
'data_path',
'config',
'public',
'project_name'
);
foreach ($fields as $a) {
......@@ -136,6 +137,7 @@ final class DatasetListAction extends AbstractAction
$dataset->setVo($parsedBody['vo']);
$dataset->setDataPath($parsedBody['data_path']);
$dataset->setConfig($parsedBody['config']);
$dataset->setPublic($parsedBody['public']);
$dataset->setProject($project);
$dataset->setDatasetFamily($datasetFamily);
......
<?php
/*
* This file is part of Anis Server.
*
* (c) Laboratoire d'Astrophysique de Marseille / CNRS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace App\Action;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Exception\HttpBadRequestException;
use Slim\Exception\HttpNotFoundException;
use App\Entity\Group;
final class GroupAction extends AbstractAction
{
/**
* `GET` Returns the Group found
* `PUT` Full update the Group and returns the new version
* `DELETE` Delete the Group found and return a confirmation message
*
* @param ServerRequestInterface $request PSR-7 This object represents the HTTP request
* @param ResponseInterface $response PSR-7 This object represents the HTTP response
* @param string[] $args This table contains information transmitted in the URL (see routes.php)
*
* @return ResponseInterface
*/
public function __invoke(Request $request, Response $response, array $args): Response
{
if ($request->getMethod() === OPTIONS) {
return $response->withHeader('Access-Control-Allow-Methods', 'GET, PUT, DELETE, OPTIONS');
}
// Search the correct group with primary key
$group = $this->em->find('App\Entity\Group', $args['id']);
// If group is not found 404
if (is_null($group)) {
throw new HttpNotFoundException(
$request,
'Group with id ' . $args['id'] . ' is not found'
);
}
if ($request->getMethod() === GET) {
$payload = json_encode($group);
}
if ($request->getMethod() === PUT) {
$parsedBody = $request->getParsedBody();
// If mandatories empty fields 400
foreach (array('label') as $a) {
if ($this->isEmptyField($a, $parsedBody)) {
throw new HttpBadRequestException(
$request,
'Param ' . $a . ' needed to edit the group'
);
}
}
$this->editGroup($group, $parsedBody);
$payload = json_encode($group);
}
if ($request->getMethod() === DELETE) {
$id = $group->getId();
$this->em->remove($group);
$this->em->flush();
$payload = json_encode(array('message' => 'Group with id ' . $id . ' is removed!'));
}
$response->getBody()->write($payload);
return $response;
}
/**
* Update group object with setters
*
* @param Group $group The group to update
* @param array $parsedBody Contains the new values ​​of the group sent by the user
*/
private function editGroup(Group $group, array $parsedBody): void
{
$group->setLabel($parsedBody['label']);
$group->setUsers($this->getUsers($parsedBody['users']));
$group->setDatasets($this->getDatasets($parsedBody['datasets']));
$this->em->flush();
}
private function getUsers(array $listOfUsersIds)
{
if (count($listOfUsersIds) < 1) {
return array();
}
$dql = 'SELECT u FROM App\Entity\User u WHERE u.id IN (' . implode(',', $listOfUsersIds) . ')';
$query = $this->em->createQuery($dql);
return $query->getResult();
}
private function getDatasets(array $listOfDatasetsNames)
{
if (count($listOfDatasetsNames) < 1) {
return array();
}
$in = implode(',', array_map(function ($d) {
return "'" . $d . "'";
}, $listOfDatasetsNames));
$dql = 'SELECT d FROM App\Entity\Dataset d WHERE d.name IN (' . $in . ')';
$query = $this->em->createQuery($dql);
return $query->getResult();
}
}
<?php
/*
* This file is part of Anis Server.
*
* (c) Laboratoire d'Astrophysique de Marseille / CNRS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace App\Action;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Exception\HttpBadRequestException;
use App\Entity\Group;
final class GroupListAction extends AbstractAction
{
/**
* `GET` Returns a list of all groups listed in the metamodel database
* `POST` Add a new group
*
* @param ServerRequestInterface $request PSR-7 This object represents the HTTP request
* @param ResponseInterface $response PSR-7 This object represents the HTTP response
* @param string[] $args This table contains information transmitted in the URL (see routes.php)
*
* @return ResponseInterface
*/
public function __invoke(Request $request, Response $response, array $args): Response
{
if ($request->getMethod() === OPTIONS) {
return $response->withHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
}
if ($request->getMethod() === GET) {
// Retrieve group with id
$groups = $this->em->getRepository('App\Entity\Group')->findAll();
$payload = json_encode($groups);
}
if ($request->getMethod() === POST) {
$parsedBody = $request->getParsedBody();
// To work this action needs group information
foreach (array('label') as $a) {
if ($this->isEmptyField($a, $parsedBody)) {
throw new HttpBadRequestException(
$request,
'Param ' . $a . ' needed to add a new group'
);
}
}
$group = $this->postGroup($parsedBody);
$payload = json_encode($group);
$response = $response->withStatus(201);
}
$response->getBody()->write($payload);
return $response;
}
/**
* Add a new group into the metamodel
*
* @param array $parsedBody Contains the values ​​of the new group sent by the user
*/
private function postGroup(array $parsedBody): Group
{
$group = new Group(
$this->getUsers($parsedBody['users']),
$this->getDatasets($parsedBody['datasets'])
);
$group->setLabel($parsedBody['label']);
$this->em->persist($group);
$this->em->flush();
return $group;
}
private function getUsers(array $listOfUsersEmails)
{
if (count($listOfUsersEmails) < 1) {
return array();
}
$in = implode(',', array_map(function ($u) {
return "'" . $u . "'";
}, $listOfUsersEmails));
$dql = 'SELECT u FROM App\Entity\User u WHERE u.email IN (' . $in . ')';
$query = $this->em->createQuery($dql);
return $query->getResult();
}
private function getDatasets(array $listOfDatasetsNames)
{
if (count($listOfDatasetsNames) < 1) {
return array();
}
$in = implode(',', array_map(function ($d) {
return "'" . $d . "'";
}, $listOfDatasetsNames));
$dql = 'SELECT d FROM App\Entity\Dataset d WHERE d.name IN (' . $in . ')';
$query = $this->em->createQuery($dql);
return $query->getResult();
}
}
......@@ -55,7 +55,7 @@ final class ProjectAction extends AbstractAction
if ($request->getMethod() === PUT) {
$parsedBody = $request->getParsedBody();
// If mandatories empty fields 400
foreach (array('label', 'description', 'link', 'manager', 'id_database') as $a) {
if ($this->isEmptyField($a, $parsedBody)) {
......
......@@ -83,7 +83,7 @@ final class SearchAction extends AbstractAction
$connection = $this->connectionFactory->create($dataset->getProject()->getDatabase());
$queryBuilder = $connection->createQueryBuilder();
$queryBuilder->from($dataset->getTableRef());
$queryParams = $request->getQueryParams();
// The parameter "a" is mandatory
......
<?php
/*
* This file is part of Anis Server.
*
* (c) Laboratoire d'Astrophysique de Marseille / CNRS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace App\Action;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Exception\HttpBadRequestException;
use Slim\Exception\HttpNotFoundException;
use App\Entity\User;
final class UserAction extends AbstractAction
{
/**
* `GET` Returns the user found
* `PUT` Full update the user and returns the new version
* `DELETE` Delete the user found and return a confirmation message
*
* @param ServerRequestInterface $request PSR-7 This object represents the HTTP request
* @param ResponseInterface $response PSR-7 This object represents the HTTP response
* @param string[] $args This table contains information transmitted in the URL (see routes.php)
*
* @return ResponseInterface
*/
public function __invoke(Request $request, Response $response, array $args): Response
{
if ($request->getMethod() === OPTIONS) {
return $response->withHeader('Access-Control-Allow-Methods', 'GET, PUT, DELETE, OPTIONS');
}
// Search the correct user with primary key
$user = $this->em->find('App\Entity\User', $args['email']);
// If user is not found 404
if (is_null($user)) {
throw new HttpNotFoundException(
$request,
'User with email ' . $args['email'] . ' is not found'
);
}
if ($request->getMethod() === GET) {
$payload = json_encode($user);
}
if ($request->getMethod() === PUT) {
$parsedBody = $request->getParsedBody();
// If mandatories empty fields 400
foreach (array('role') as $a) {
if ($this->isEmptyField($a, $parsedBody)) {
throw new HttpBadRequestException(
$request,
'Param ' . $a . ' needed to edit the user'
);
}
}
$this->editUser($user, $parsedBody);
$payload = json_encode($user);
}
if ($request->getMethod() === DELETE) {
$email = $user->getEmail();
$this->em->remove($user);
$this->em->flush();
$payload = json_encode(array('message' => 'User with email ' . $email . ' is removed!'));
}
$response->getBody()->write($payload);
return $response;
}
/**
* Update user object with setters
*
* @param User $user The user to update
* @param array $parsedBody Contains the new values ​​of the user
*/
private function editUser(User $user, array $parsedBody): void
{
$user->setRole($parsedBody['role']);
$this->em->flush();
}
}
<?php
/*
* This file is part of Anis Server.
*
* (c) Laboratoire d'Astrophysique de Marseille / CNRS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace App\Action;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Exception\HttpBadRequestException;
use App\Entity\User;
final class UserListAction extends AbstractAction
{
/**
* `GET` Returns a list of all users listed in the metamodel
* `POST` Add a new user
*
* @param ServerRequestInterface $request PSR-7 This object represents the HTTP request
* @param ResponseInterface $response PSR-7 This object represents the HTTP response
* @param string[] $args This table contains information transmitted in the URL (see routes.php)
*
* @return ResponseInterface
*/
public function __invoke(Request $request, Response $response, array $args): Response
{
if ($request->getMethod() === OPTIONS) {
return $response->withHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
}
if ($request->getMethod() === GET) {
$users = $this->em->getRepository('App\Entity\User')->findAll();
$payload = json_encode($users);
}
if ($request->getMethod() === POST) {
$parsedBody = $request->getParsedBody();
// To work this action needs user information
foreach (array('email') as $a) {
if ($this->isEmptyField($a, $parsedBody)) {
throw new HttpBadRequestException(
$request,
'Param ' . $a . ' needed to add a new user'
);
}
}
$user = $this->postUser($parsedBody);
$payload = json_encode($user);
$response = $response->withStatus(201);
}
$response->getBody()->write($payload);
return $response;
}
/**
* Add a new user into the metamodel
*
* @param array $parsedBody Contains the values ​​of the new user
*/
private function postUser(array $parsedBody): User
{
$user = new User($parsedBody['email']);
$user->setRole('user');
$this->em->persist($user);
$this->em->flush();
return $user;
}
}
......@@ -134,7 +134,7 @@ class Attribute implements \JsonSerializable
* @Column(type="string", name="placeholder_min", nullable=true)
*/
protected $placeholderMin;
/**
* @var string
*
......
......@@ -26,7 +26,7 @@ class CriteriaFamily implements \JsonSerializable
* @GeneratedValue
*/
protected $id;
/**
* @var string
*
......@@ -58,12 +58,12 @@ class CriteriaFamily implements \JsonSerializable
{
return $this->id;
}
public function getLabel()
{
return $this->label;
}