Commit 2794b6f3 authored by François Agneray's avatar François Agneray

Add users and groups features => done

parent fc89ad89
......@@ -18,8 +18,8 @@ $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], '/user', App\Action\UserListAction::class);
$app->map([OPTIONS, GET], '/user/{email}', App\Action\UserAction::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, POST], '/group/{id}', App\Action\GroupAction::class);
$app->map([OPTIONS, GET, POST], '/instance', App\Action\InstanceListAction::class);
......
......@@ -141,6 +141,10 @@ curl -d '{"id":56,"name":"spec1d","table_name":"aspic_vipers_dr2_w1","label":"sp
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"}' -H "Content-Type: application/json" -X POST http://localhost/group
curl -d '{"label":"ASPIC"}' -H "Content-Type: application/json" -X POST http://localhost/group
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
......@@ -89,6 +89,34 @@ final class GroupAction extends AbstractAction
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();
}
}
......@@ -54,7 +54,7 @@ final class GroupListAction extends AbstractAction
}
}
$group = $this->postDatabase($parsedBody);
$group = $this->postGroup($parsedBody);
$payload = json_encode($group);
$response = $response->withStatus(201);
}
......@@ -68,9 +68,12 @@ final class GroupListAction extends AbstractAction
*
* @param array $parsedBody Contains the values ​​of the new group sent by the user
*/
private function postDatabase(array $parsedBody): Group
private function postGroup(array $parsedBody): Group
{
$group = new Group();
$group = new Group(
$this->getUsers($parsedBody['users']),
$this->getDatasets($parsedBody['datasets'])
);
$group->setLabel($parsedBody['label']);
$this->em->persist($group);
......@@ -78,4 +81,34 @@ final class GroupListAction extends AbstractAction
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();
}
}
......@@ -21,7 +21,9 @@ use App\Entity\User;
final class UserAction extends AbstractAction
{
/**
* `GET` Returns the user found
* `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
......@@ -32,7 +34,7 @@ final class UserAction extends AbstractAction
public function __invoke(Request $request, Response $response, array $args): Response
{
if ($request->getMethod() === OPTIONS) {
return $response->withHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
return $response->withHeader('Access-Control-Allow-Methods', 'GET, PUT, DELETE, OPTIONS');
}
// Search the correct user with primary key
......@@ -50,7 +52,43 @@ final class UserAction extends AbstractAction
$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();
}
}
......@@ -21,6 +21,7 @@ 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
......@@ -31,7 +32,7 @@ final class UserListAction extends AbstractAction
public function __invoke(Request $request, Response $response, array $args): Response
{
if ($request->getMethod() === OPTIONS) {
return $response->withHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
return $response->withHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
}
if ($request->getMethod() === GET) {
......@@ -39,7 +40,41 @@ final class UserListAction extends AbstractAction
$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;
}
}
......@@ -39,9 +39,13 @@ class Group implements \JsonSerializable
/**
* Many Groups have Many Users.
* @var Collection
*
* @ManyToMany(targetEntity="User", mappedBy="groups")
* @ManyToMany(targetEntity="User", inversedBy="groups")
* @JoinTable(
* name="groups_users",
* joinColumns={@JoinColumn(name="group_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="user_email", referencedColumnName="email")}
* )
*/
protected $users;
......@@ -57,10 +61,10 @@ class Group implements \JsonSerializable
*/
protected $datasets;
public function __construct()
public function __construct(array $users, array $datasets)
{
$this->users = new ArrayCollection();
$this->datasets = new ArrayCollection();
$this->users = new ArrayCollection($users);
$this->datasets = new ArrayCollection($datasets);
}
public function getId()
......@@ -83,11 +87,21 @@ class Group implements \JsonSerializable
return $this->users;
}
public function setUsers($users)
{
$this->users = $users;
}
public function getDatasets()
{
return $this->datasets;
}
public function setDatasets($datasets)
{
$this->datasets = $datasets;
}
public function jsonSerialize()
{
$users = array();
......
......@@ -38,14 +38,8 @@ class User implements \JsonSerializable
/**
* Many Users have Many Groups.
* @var Collection
*
* @ManyToMany(targetEntity="Group", inversedBy="users")
* @JoinTable(
* name="users_groups",
* joinColumns={@JoinColumn(name="email", referencedColumnName="email")},
* inverseJoinColumns={@JoinColumn(name="group_id", referencedColumnName="id")}
* )
* @ManyToMany(targetEntity="Group", mappedBy="users")
*/
protected $groups;
......
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