Commit 5cd66f11 authored by François Agneray's avatar François Agneray
Browse files

Merge branch '34-gestion-des-instances-multi-metamodel' into 'develop'

Resolve "Gestion des instances (multi metamodel)"

Closes #34

See merge request !34
parents 19817746 4ed2cbf1
Pipeline #1469 passed with stages
in 7 minutes and 2 seconds
......@@ -6,18 +6,17 @@ list:
@echo "Useful targets:"
@echo ""
@echo " install > install php composer dependancies"
@echo " up > build php image and start anis-v3-server containers for dev only (php + mailer)"
@echo " start > start anis-v3-server containers"
@echo " restart > restart anis-v3-server containers"
@echo " stop > stop and kill running anis-v3-server containers"
@echo " logs > display anis-v3-server containers logs"
@echo " shell > shell into php anis-v3-server container"
@echo " up > build php image and start anis-server containers for dev only (php + mailer)"
@echo " start > start anis-server containers"
@echo " restart > restart anis-server containers"
@echo " stop > stop and kill running anis-server containers"
@echo " logs > display anis-server containers logs"
@echo " shell > shell into php anis-server container"
@echo " phpunit > run php unit test suite"
@echo " phpcs > run php code sniffer test suite"
@echo " init-db > generate a new metadata database (anis-v3-server containers running needed)"
@echo " remove-db > remove the metadata database"
@echo " validate-db > validate the doctrine mapping entity files (anis-v3-server containers running needed)"
@echo " add-superuser > add an Anis server superuser. Example: make email=admin@anis.fr password=admin add-superuser"
@echo " anis-init > generate a new anis admin database and a default metamodel database (anis-server containers running needed)"
@echo " dev-init > add metadata datasets for devlopment purpose"
@echo " remove-pgdata > remove the metadata database"
@echo " gen-key > generate a new encryption key that you can use to encrypt data (see anis server config file)"
@echo ""
......@@ -54,24 +53,16 @@ phpunit:
phpcs:
@docker run --init -it --rm --user $(UID):$(GID) \
-v $(CURDIR):/project \
-w /project jakzal/phpqa phpcs --standard=PSR2 --extensions=php --colors src
-w /project jakzal/phpqa phpcs --standard=PSR2 --extensions=php --colors src tests
init-meta-db:
@docker-compose exec php sh ./conf-dev/metamodel_init.sh
anis-init:
@docker-compose exec php sh ./conf-dev/anis_init.sh
init-db-dev:
@docker-compose exec php sh ./conf-dev/init-db-dev.sh
dev-init:
@docker-compose exec php sh ./conf-dev/dev-init.sh
remove-db:
remove-pgdata:
@docker volume rm anis-server_pgdata
validate-db:
@docker-compose exec php ./vendor/bin/doctrine orm:validate-schema
gen-key:
@docker-compose exec php php ./conf-dev/generate_encryption_key.php
add-superuser:
@[ "${email}" ] || ( echo ">> email is not set"; exit 1 )
@[ "${password}" ] || ( echo ">> password is not set"; exit 1 )
@docker-compose exec php curl -d '{"email":"${email}","password":"${password}"}' -H "Content-Type: application/json" -X POST http://localhost/metadata/superuser
\ No newline at end of file
@docker-compose exec php php ./conf-dev/generate_encryption_key.php
\ No newline at end of file
......@@ -3,20 +3,20 @@
require 'vendor/autoload.php';
$settings = require './src/settings.php';
$metadata = $settings['settings']['metadata'];
$adminDb = $settings['settings']['admin_db'];
$c = \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration(array('src/Entity'), $metadata['dev_mode']);
$c->setProxyDir(getcwd() . '/' . $metadata['path_proxy']);
if ($metadata['dev_mode']) {
$c->setAutoGenerateProxyClasses(true);
$c = \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration(array('src/Entity/Admin'), $adminDb['dev_mode']);
$c->setProxyDir(getcwd() . '/' . $adminDb['path_proxy']);
if ($adminDb['dev_mode']) {
$c->setAutoGenerateProxyClasses(true);
} else {
$c->setAutoGenerateProxyClasses(false);
$c->setAutoGenerateProxyClasses(false);
}
$em = \Doctrine\ORM\EntityManager::create($metadata['connection_options'], $c);
$em = \Doctrine\ORM\EntityManager::create($adminDb['connection_options'], $c);
$helpers = new Symfony\Component\Console\Helper\HelperSet(array(
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));
return \Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($em);
#!/bin/sh
set -e
# Create the anis admin database (only tables)
./vendor/bin/doctrine orm:schema-tool:create
curl -d '{"label":"Admin"}' -H "Content-Type: application/json" -X POST http://localhost/metadata/group
curl -d '{"label":"Default dataset family","display":10}' -H "Content-Type: application/json" -X POST http://localhost/metadata/family/dataset
curl -d '{"label":"Default criteria family","display":10}' -H "Content-Type: application/json" -X POST http://localhost/metadata/family/criteria
curl -d '{"label":"Default output family","display":10}' -H "Content-Type: application/json" -X POST http://localhost/metadata/family/output
curl -d '{"label":"Default output category","display":10,"id_output_family":1}' -H "Content-Type: application/json" -X POST http://localhost/metadata/output-category
curl -d '{"name":"search_flag","label":"Search flag"}' -H "Content-Type: application/json" -X POST http://localhost/settings/select
curl -d '{"name":"search_type","label":"Search Type"}' -H "Content-Type: application/json" -X POST http://localhost/settings/select
curl -d '{"name":"operator","label":"Operator"}' -H "Content-Type: application/json" -X POST http://localhost/settings/select
curl -d '{"name":"renderer","label":"Renderer"}' -H "Content-Type: application/json" -X POST http://localhost/settings/select
curl -d '{"name":"renderer_detail","label":"Renderer detail"}' -H "Content-Type: application/json" -X POST http://localhost/settings/select
curl -d '{"label":"ID","value":"ID","display":10,"id_settings_select":1}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"RA","value":"RA","display":20,"id_settings_select":1}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"DEC","value":"DEC","display":30,"id_settings_select":1}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"Field","value":"field","display":10,"id_settings_select":2}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"Between","value":"between","display":20,"id_settings_select":2}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"Select","value":"select","display":30,"id_settings_select":2}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"Select multiple","value":"select-multiple","display":40,"id_settings_select":2}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"Datalist","value":"datalist","display":50,"id_settings_select":2}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"Radio","value":"radio","display":60,"id_settings_select":2}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"Checkbox","value":"checkbox","display":70,"id_settings_select":2}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"Between date","value":"between-date","display":80,"id_settings_select":2}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"Date","value":"date","display":90,"id_settings_select":2}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"Time","value":"time","display":100,"id_settings_select":2}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"Date time","value":"date-time","display":110,"id_settings_select":2}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"JSON","value":"json","display":120,"id_settings_select":2}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"=","value":"eq","display":10,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"≠","value":"neq","display":20,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"between","value":"bw","display":30,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":">","value":"gt","display":40,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":">=","value":"gte","display":50,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"<","value":"lt","display":60,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"<=","value":"lte","display":70,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"like","value":"lk","display":80,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"not like","value":"nlk","display":90,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"in","value":"in","display":100,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"not in","value":"nin","display":110,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"null","value":"nl","display":120,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"not null","value":"nnl","display":130,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"json","value":"js","display":140,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"Image","value":"img","display":10,"id_settings_select":4}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"Thumbnail","value":"thumbnail","display":20,"id_settings_select":4}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"Link","value":"link","display":30,"id_settings_select":4}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"Button","value":"btn","display":40,"id_settings_select":4}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"Detail link","value":"detail-link","display":50,"id_settings_select":4}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"Detail button","value":"detail-btn","display":60,"id_settings_select":4}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"Download","value":"download","display":70,"id_settings_select":4}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"JSON","value":"json","display":80,"id_settings_select":4}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
curl -d '{"label":"Image","value":"img","display":10,"id_settings_select":5}' -H "Content-Type: application/json" -X POST http://localhost/settings/option
# Add the first anis superuser
curl -d '{"email":"admin@anis.fr","password":"admin"}' -H "Content-Type: application/json" -X POST http://localhost/login/register
curl -d '{"adminsi":true,"superuser":true,"activated":true}' -H "Content-Type: application/json" -X PUT http://localhost/admin/user/admin@anis.fr
# Add settings for admin inteface
curl -d '{"name":"search_flag","label":"Search flag"}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/select
curl -d '{"name":"search_type","label":"Search Type"}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/select
curl -d '{"name":"operator","label":"Operator"}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/select
curl -d '{"name":"renderer","label":"Renderer"}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/select
curl -d '{"name":"renderer_detail","label":"Renderer detail"}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/select
curl -d '{"label":"ID","value":"ID","display":10,"id_settings_select":1}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"RA","value":"RA","display":20,"id_settings_select":1}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"DEC","value":"DEC","display":30,"id_settings_select":1}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"Field","value":"field","display":10,"id_settings_select":2}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"Between","value":"between","display":20,"id_settings_select":2}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"Select","value":"select","display":30,"id_settings_select":2}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"Select multiple","value":"select-multiple","display":40,"id_settings_select":2}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"Datalist","value":"datalist","display":50,"id_settings_select":2}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"Radio","value":"radio","display":60,"id_settings_select":2}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"Checkbox","value":"checkbox","display":70,"id_settings_select":2}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"Between date","value":"between-date","display":80,"id_settings_select":2}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"Date","value":"date","display":90,"id_settings_select":2}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"Time","value":"time","display":100,"id_settings_select":2}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"Date time","value":"date-time","display":110,"id_settings_select":2}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"JSON","value":"json","display":120,"id_settings_select":2}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"=","value":"eq","display":10,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"≠","value":"neq","display":20,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"between","value":"bw","display":30,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":">","value":"gt","display":40,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":">=","value":"gte","display":50,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"<","value":"lt","display":60,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"<=","value":"lte","display":70,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"like","value":"lk","display":80,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"not like","value":"nlk","display":90,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"in","value":"in","display":100,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"not in","value":"nin","display":110,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"null","value":"nl","display":120,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"not null","value":"nnl","display":130,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"json","value":"js","display":140,"id_settings_select":3}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"Image","value":"img","display":10,"id_settings_select":4}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"Thumbnail","value":"thumbnail","display":20,"id_settings_select":4}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"Link","value":"link","display":30,"id_settings_select":4}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"Button","value":"btn","display":40,"id_settings_select":4}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"Detail link","value":"detail-link","display":50,"id_settings_select":4}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"Detail button","value":"detail-btn","display":60,"id_settings_select":4}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"Download","value":"download","display":70,"id_settings_select":4}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"JSON","value":"json","display":80,"id_settings_select":4}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
curl -d '{"label":"Image","value":"img","display":10,"id_settings_select":5}' -H "Content-Type: application/json" -X POST http://localhost/admin/settings/option
# Generate default metamodel
curl -d '{"name":"default","label":"Default instance","dev_mode":true,"driver":"pdo_pgsql","path":"","host":"db","port":5432,"login":"anis","password":"anis","path_proxy":"/tmp/doctrine_proxy","dbname":"anis_metamodel"}' -H "Content-Type: application/json" -X POST http://localhost/admin/instance
curl -X GET http://localhost/admin/instance/default/create-database
# Add admin group to the default metamodel
curl -d '{"label":"Admin"}' -H "Content-Type: application/json" -X POST http://localhost/metadata/default/group
# Add all default families
curl -d '{"label":"Default dataset family","display":10}' -H "Content-Type: application/json" -X POST http://localhost/metadata/default/family/dataset
curl -d '{"label":"Default criteria family","display":10}' -H "Content-Type: application/json" -X POST http://localhost/metadata/default/family/criteria
curl -d '{"label":"Default output family","display":10}' -H "Content-Type: application/json" -X POST http://localhost/metadata/default/family/output
curl -d '{"label":"Default output category","display":10,"id_output_family":1}' -H "Content-Type: application/json" -X POST http://localhost/metadata/default/output-category
\ No newline at end of file
curl -d '{"email":"admin@anis.fr","password":"admin"}' -H "Content-Type: application/json" -X POST http://localhost/metadata/superuser
#!/bin/sh
set -e
curl -d '{"label":"Test","dbname":"anis_test","dbtype":"pdo_pgsql","dbhost":"db","dbport":5432,"dblogin":"anis","dbpassword":"anis"}' -H "Content-Type: application/json" -X POST http://localhost/metadata/database
curl -d '{"name":"anis_project","label":"Anis Project Test","description":"Project used for testing","link":"http://project.com","manager":"M. Durand","id_database":1}' -H "Content-Type: application/json" -X POST http://localhost/metadata/project
curl -d '{"label":"Test","dbname":"anis_test","dbtype":"pdo_pgsql","dbhost":"db","dbport":5432,"dblogin":"anis","dbpassword":"anis"}' -H "Content-Type: application/json" -X POST http://localhost/metadata/default/database
curl -d '{"name":"anis_project","label":"Anis Project Test","description":"Project used for testing","link":"http://project.com","manager":"M. Durand","id_database":1}' -H "Content-Type: application/json" -X POST http://localhost/metadata/default/project
curl -d '{"name":"obs_cat","table_ref":"obs_cat","label":"ObsCat dataset","description":"ObsCat","display":"10","count":"10000","vo":false,"data_path":"/mnt/mount","project_name":"anis_project","id_dataset_family":1}' -H "Content-Type: application/json" -X POST http://localhost/metadata/dataset
curl -d '{"name":"observations","table_ref":"observations_info","label":"Observations dataset","description":"Observations","display":"20","count":"177454","vo":false,"data_path":"/mnt/mount","project_name":"anis_project","id_dataset_family":1}' -H "Content-Type: application/json" -X POST http://localhost/metadata/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","project_name":"anis_project","id_dataset_family":1}' -H "Content-Type: application/json" -X POST http://localhost/metadata/default/dataset
curl -d '{"name":"observations","table_ref":"observations_info","label":"Observations dataset","description":"Observations","display":"20","count":"177454","vo":false,"data_path":"/mnt/mount","project_name":"anis_project","id_dataset_family":1}' -H "Content-Type: application/json" -X POST http://localhost/metadata/default/dataset
......@@ -3,6 +3,7 @@ set -e
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE USER anis LOGIN PASSWORD 'anis';
CREATE DATABASE anis_admin;
CREATE DATABASE anis_metamodel;
CREATE DATABASE anis_test;
GRANT ALL PRIVILEGES ON DATABASE anis_metamodel TO anis;
......
......@@ -12,7 +12,7 @@ services:
METADATA_DB_DRIVER: "pdo_pgsql"
METADATA_DB_HOST: "db"
METADATA_DB_PORT: 5432
METADATA_DB_DBNAME: "anis_metamodel"
METADATA_DB_DBNAME: "anis_admin"
METADATA_DB_USER: "anis"
METADATA_DB_PASSWORD: "anis"
MAILER_HOST: "mailer"
......
<?php declare(strict_types=1);
/*
* This file is part of ANIS SERVER API.
*
* (c) François Agneray <francois.agneray@lam.fr>
* (c) Chrystel Moreau <chrystel.moreau@lam.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Action\Admin;
use Psr\Log\LoggerInterface;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use App\Utils\ActionTrait;
use App\Entity\Admin\Instance;
final class InstanceAction
{
use ActionTrait;
private $logger;
private $em;
public function __construct(LoggerInterface $logger, EntityManagerInterface $em)
{
$this->logger = $logger;
$this->em = $em;
}
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->logger->info('Instance action dispatched');
if ($request->isOptions()) {
return $response->withHeader('Access-Control-Allow-Methods', 'GET, PUT, DELETE, OPTIONS');
}
$instance = $this->em->find('App\Entity\Admin\Instance', $args['name']);
if (is_null($instance)) {
return $this->dispatchHttpError(
$response,
'Invalid request',
'Instance with name ' . $args['name'] . ' is not found'
)->withStatus(404);
}
if ($request->isGet()) {
$newResponse = $response->withJson($instance);
}
if ($request->isPut()) {
$parsedBody = $request->getParsedBody();
// Vérification des champs vides
$fields = array(
'label',
'path_proxy',
'dev_mode',
'driver',
'path',
'host',
'port',
'dbname',
'login',
'password'
);
foreach ($fields as $f) {
if ($this->isEmptyField($f, $parsedBody)) {
return $this->dispatchHttpError(
$response,
'Invalid request',
'Param ' . $f . ' needed to edit an instance'
);
}
}
$this->editInstance($instance, $parsedBody);
$newResponse = $response->withJson($instance);
}
if ($request->isDelete()) {
$name = $instance->getName();
$this->em->remove($instance);
$this->em->flush();
$newResponse = $response->withJson(array('message' => 'Instance with name ' . $name . ' is removed!'));
}
return $newResponse;
}
private function editInstance(Instance $instance, array $parsedBody): void
{
$instance->setLabel($parsedBody['label']);
$instance->setPathProxy($parsedBody['path_proxy']);
$instance->setDevMode($parsedBody['dev_mode']);
$instance->setDriver($parsedBody['driver']);
$instance->setPath($parsedBody['path']);
$instance->setHost($parsedBody['host']);
$instance->setPort($parsedBody['port']);
$instance->setDbName($parsedBody['dbname']);
$instance->setLogin($parsedBody['login']);
$instance->setPassword($parsedBody['password']);
$this->em->flush();
}
}
......@@ -8,7 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Action\Meta;
namespace App\Action\Admin;
use Psr\Log\LoggerInterface;
use Doctrine\ORM\EntityManagerInterface;
......@@ -16,10 +16,9 @@ use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use App\Utils\ActionTrait;
use App\Entity\User;
use App\Entity\Group;
use App\Entity\Admin\Instance;
final class SuperuserAction
final class InstanceListAction
{
use ActionTrait;
......@@ -34,93 +33,68 @@ final class SuperuserAction
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->logger->info('User list action dispatched');
$this->logger->info('Instance list action dispatched');
if ($request->isOptions()) {
return $response->withHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
return $response->withHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
}
if ($request->isGet()) {
$instances = $this->em->getRepository('App\Entity\Admin\Instance')->findAll();
$newResponse = $response->withJson($instances);
}
if ($request->isPost()) {
$parsedBody = $request->getParsedBody();
// Vérification des champs vides
foreach (array('email', 'password') as $a) {
if ($this->isEmptyField($a, $parsedBody)) {
$fields = array(
'name',
'label',
'path_proxy',
'dev_mode',
'driver',
'path',
'host',
'port',
'dbname',
'login',
'password'
);
foreach ($fields as $f) {
if ($this->isEmptyField($f, $parsedBody)) {
return $this->dispatchHttpError(
$response,
'Invalid request',
'Param ' . $a . ' needed to add a new superuser'
'Param ' . $f . ' needed to add a new instance'
);
}
}
// Vérification de l'éxistence de l'utilisateur dans la base
if ($this->isExistUser($parsedBody)) {
return $this->dispatchHttpError(
$response,
'Invalid user',
'A user with the email address ' . $parsedBody['email'] . ' already exists'
);
}
// Vérification adresse e-mail correct
if (!$this->isVerifEmailOk($parsedBody)) {
return $this->dispatchHttpError(
$response,
'Invalid email',
'Bad email adress; a well-defined email address is needed to finalize the registration'
);
}
// Vérification de l'existence du groupe admin
$group = $this->em->getRepository('App\Entity\Group')->findOneBy(array('label' => 'Admin'));
if (is_null($group)) {
return $this->dispatchHttpError(
$response,
'Invalid request',
'Group with label Admin is not found'
)->withStatus(404);
}
$superuser = $this->postSuperuser($parsedBody, $group);
$newResponse = $response->withJson($superuser, 201);
$instance = $this->postInstance($parsedBody);
$newResponse = $response->withJson($instance, 201);
}
return $newResponse;
}
private function isExistUser(array $parsedBody): bool
private function postInstance(array $parsedBody): Instance
{
$user = $this->em->getRepository('App\Entity\User')->findOneBy(array('email' => $parsedBody['email']));
if (isset($user)) {
return true;
} else {
return false;
}
}
private function isVerifEmailOk(array $parsedBody): bool
{
if (filter_var($parsedBody['email'], FILTER_VALIDATE_EMAIL)) {
return true;
} else {
return false;
}
}
private function postSuperuser(array $parsedBody, Group $group): User
{
$user = new User($parsedBody['email']);
$user->setPassword(password_hash($parsedBody['password'], PASSWORD_DEFAULT));
$user->setAdminsi(true);
$user->setSuperuser(true);
$user->setActivationKey(''); // La clé d'activation n'est pas nécessaire pour un superuser
$user->setActivated(true); // On active le superuser par défaut
$user->setGroup($group);
$this->em->persist($user);
$instance = new Instance($parsedBody['name']);
$instance->setLabel($parsedBody['label']);
$instance->setPathProxy($parsedBody['path_proxy']);
$instance->setDevMode($parsedBody['dev_mode']);
$instance->setDriver($parsedBody['driver']);
$instance->setPath($parsedBody['path']);
$instance->setHost($parsedBody['host']);
$instance->setPort($parsedBody['port']);
$instance->setDbName($parsedBody['dbname']);
$instance->setLogin($parsedBody['login']);
$instance->setPassword($parsedBody['password']);
$this->em->persist($instance);
$this->em->flush();
return $user;
return $instance;
}
}
<?php declare(strict_types=1);
/*
* This file is part of ANIS SERVER API.
*
* (c) François Agneray <francois.agneray@lam.fr>
* (c) Chrystel Moreau <chrystel.moreau@lam.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Action\Admin;
use Psr\Log\LoggerInterface;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\SchemaTool;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use App\Utils\ActionTrait;
use App\Utils\MetaEntityManagerFactory;
final class MetamodelAction
{
use ActionTrait;
private $logger;
private $aem;
private $memf;
public function __construct(LoggerInterface $logger, EntityManagerInterface $aem, MetaEntityManagerFactory $memf)
{
$this->logger = $logger;
$this->aem = $aem;
$this->memf = $memf;
}
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->logger->info('Metamodel action dispatched');
if ($request->isOptions()) {
return $response->withHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
}
$instance = $this->aem->find('App\Entity\Admin\Instance', $args['name']);
if (is_null($instance)) {
return $this->dispatchHttpError(
$response,
'Invalid request',
'Instance with name ' . $args['name'] . ' is not found'
)->withStatus(404);
}
if (!in_array($args['action'], array('create-database', 'update-database', 'delete-database'))) {
return $this->dispatchHttpError(
$response,
'Invalid request',
'Action ' . $args['action'] . ' is not allowed!'
)->withStatus(400);
}
$this->memf->createMetaEntityManager($args['name']);
$instanceEntityManager = $this->memf->getMetaEntityManager();
$schemaTool = new SchemaTool($instanceEntityManager);
$classes = $instanceEntityManager->getMetadataFactory()->getAllMetadata();
switch ($args['action']) {
case 'create-database':
$schemaTool->createSchema($classes);
$message = 'Database metamodel has been created!';
break;
case 'update-database':
$schemaTool->updateSchema($classes);
$message = 'Database metamodel has been updated!';
break;