Commit 39d0b2c1 authored by Vassilis Kanellopoulos's avatar Vassilis Kanellopoulos
Browse files

school service, inputfilter; use in createschool in sch_sync; update delete teacher like asset

parent 0b138bbe
......@@ -13,8 +13,7 @@ return [
'routes' => [
['/school', ['school'], ['get']],
['/school/labs', ['school'], ['get']],
['/school/staff', ['school'], ['get', 'post']],
['/school/staff/{id:[1-9][0-9]*}', ['school'], ['delete']],
['/school/staff', ['school'], ['get', 'post', 'delete']],
['/school/assets', ['school'], ['get', 'post', 'delete']],
],
],
......
<?php
use GrEduLabs\Schools\InputFilter\School as SchoolInputFilter;
use GrEduLabs\Schools\Service\SchoolServiceInterface;
use SchMM\FetchUnit;
use SchSync\Middleware\CreateSchool;
use SchSync\Middleware\CreateUser;
use Slim\App;
/**
* gredu_labs.
*
......@@ -8,7 +15,7 @@
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
return function (Slim\App $app) {
return function (App $app) {
$container = $app->getContainer();
$events = $container['events'];
......@@ -18,33 +25,35 @@ return function (Slim\App $app) {
});
$events('on', 'app.services', function ($stop, $container) {
$container[SchSync\Middleware\CreateUser::class] = function ($c) {
return new SchSync\Middleware\CreateUser(
$c['authentication_service'],
$c['router']->pathFor('user.login'),
$c['router']->pathFor('user.logout.sso'),
$c['flash'],
$c['logger']
$container[CreateUser::class] = function ($c) {
return new CreateUser(
$c->get('authentication_service'),
$c->get('router')->pathFor('user.login'),
$c->get('router')->pathFor('user.logout.sso'),
$c->get('flash'),
$c->get('logger')
);
};
$container[SchSync\Middleware\CreateSchool::class] = function ($c) {
return new SchSync\Middleware\CreateSchool(
$c['ldap'],
$c[SchMM\FetchUnit::class],
$c['authentication_service'],
$c['router']->pathFor('user.login'),
$c['router']->pathFor('user.logout.sso'),
$c['flash'],
$c['logger']
$container[CreateSchool::class] = function ($c) {
return new CreateSchool(
$c->get('ldap'),
$c->get(FetchUnit::class),
$c->get('authentication_service'),
$c->get(SchoolServiceInterface::class),
$c->get(SchoolInputFilter::class),
$c->get('router')->pathFor('user.login'),
$c->get('router')->pathFor('user.logout.sso'),
$c->get('flash'),
$c->get('logger')
);
};
});
$events('on', 'app.bootstrap', function ($stop, $app, $container) {
foreach ($container['router']->getRoutes() as $route) {
foreach ($container->get('router')->getRoutes() as $route) {
if ('user.login.sso' === $route->getName()) {
$route->add(SchSync\Middleware\CreateUser::class)
->add(SchSync\Middleware\CreateSchool::class);
$route->add(CreateUser::class)
->add(CreateSchool::class);
break;
}
}
......
......@@ -10,7 +10,10 @@
namespace SchSync\Middleware;
use Exception;
use GrEduLabs\Authentication\Identity;
use GrEduLabs\Schools\InputFilter\School as SchoolInputFilter;
use GrEduLabs\Schools\Service\SchoolServiceInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Log\LoggerInterface;
......@@ -38,6 +41,18 @@ class CreateSchool
*/
private $authService;
/**
*
* @var SchoolServiceInterface
*/
private $schoolService;
/**
*
* @var SchoolInputFilter
*/
private $schoolInputFilter;
/**
* @var string
*/
......@@ -62,6 +77,8 @@ class CreateSchool
Ldap $ldap,
callable $fetchUnitFromMM,
AuthenticationServiceInterface $authService,
SchoolServiceInterface $schoolService,
SchoolInputFilter $schoolInputFilter,
$unitNotFoundRedirectUrl,
$ssoLogoutUrl,
Messages $flash,
......@@ -70,6 +87,8 @@ class CreateSchool
$this->ldap = $ldap;
$this->fetchUnit = $fetchUnitFromMM;
$this->authService = $authService;
$this->schoolService = $schoolService;
$this->schoolInputFilter = $schoolInputFilter;
$this->unitNotFoundRedirectUrl = (string) $unitNotFoundRedirectUrl;
$this->ssoLogoutUrl = (string) $ssoLogoutUrl;
$this->flash = $flash;
......@@ -113,33 +132,36 @@ class CreateSchool
$school = R::findOne('school', 'registry_no = ?', [$registryNo]);
try {
if (!$school) {
R::begin();
$school = R::dispense('school');
$school->registry_no = $unit['registry_no'];
$school->name = $unit['name'];
$school->street_address = $unit['street_address'];
$school->postal_code = $unit['postal_code'];
$school->phone_number = $unit['phone_number'];
$school->fax_number = $unit['fax_number'];
$school->email = $unit['email'];
$school->municipality = $unit['municipality'];
$school->schooltype_id = $unit['unit_type_id'];
$school->prefecture_id = $unit['prefecture_id'];
$school->educationlevel_id = $unit['education_level_id'];
$school->eduadmin_id = $unit['edu_admin_id'];
$school->created = time();
$school->creator = $identity->mail;
$school_id = R::store($school);
$this->logger->info(sprintf('School %s imported from MM to database', $registryNo), ['creator' => $identity->mail]);
$data = [
'id' => '',
'registry_no' => $unit['registry_no'],
'name' => $unit['name'],
'street_address' => $unit['street_address'],
'postal_code' => $unit['postal_code'],
'phone_number' => $unit['phone_number'],
'fax_number' => $unit['fax_number'],
'email' => $unit['email'],
'municipality' => $unit['municipality'],
'schooltype_id' => $unit['unit_type_id'],
'prefecture_id' => $unit['prefecture_id'],
'educationlevel_id' => $unit['education_level_id'],
'eduadmin_id' => $unit['edu_admin_id'],
'creator' => $identity->mail,
];
$filtered = call_user_func($this->schoolInputFilter, $data);
if (!$filtered['is_valid']) {
$this->logger->error('Invalid data', $filtered);
throw new Exception('Invalid data');
}
$school = $this->schoolService->createSchool($filtered['values']);
$this->logger->info(sprintf('School %s imported from MM to database', $registryNo), $filtered['values']);
$user = R::load('user', $identity->id);
$user->school_id = $school_id;
$user->school_id = $school['id'];
R::store($user);
$this->logger->info(sprintf('Set school %s to user %s', $registryNo, $identity->mail));
R::commit();
}
} catch (\Exception $e) {
R::rollback();
} catch (Exception $e) {
$this->logger->error(sprintf('Problem inserting school %s form MM in database', $registryNo));
$this->logger->debug('Exception', [$e->getMessage(), $e->getTraceAsString()]);
......
......@@ -8,6 +8,11 @@
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
use GrEduLabs\Schools\Action;
use GrEduLabs\Schools\InputFilter;
use GrEduLabs\Schools\Middleware;
use GrEduLabs\Schools\Service;
return function (Slim\App $app) {
$container = $app->getContainer();
......@@ -21,130 +26,136 @@ return function (Slim\App $app) {
// actions
$container[GrEduLabs\Schools\Action\Index::class] = function ($c) {
return new GrEduLabs\Schools\Action\Index(
$container[Action\Index::class] = function ($c) {
return new Action\Index(
$c->get('view'),
$c->get('schoolservice')
$c->get(Service\SchoolServiceInterface::class)
);
};
$container[GrEduLabs\Schools\Action\Staff\ListAll::class] = function ($c) {
return new GrEduLabs\Schools\Action\Staff\ListAll(
$container[Action\Staff\ListAll::class] = function ($c) {
return new Action\Staff\ListAll(
$c->get('view'),
$c->get(GrEduLabs\Schools\Service\StaffServiceInterface::class)
$c->get(Service\StaffServiceInterface::class)
);
};
$container[GrEduLabs\Schools\Action\Staff\PersistTeacher::class] = function ($c) {
return new GrEduLabs\Schools\Action\Staff\PersistTeacher(
$c->get(GrEduLabs\Schools\Service\StaffServiceInterface::class)
$container[Action\Staff\PersistTeacher::class] = function ($c) {
return new Action\Staff\PersistTeacher(
$c->get(Service\StaffServiceInterface::class)
);
};
$container[GrEduLabs\Schools\Action\Staff\DeleteTeacher::class] = function ($c) {
return new GrEduLabs\Schools\Action\Staff\DeleteTeacher(
$c->get(GrEduLabs\Schools\Service\StaffServiceInterface::class)
$container[Action\Staff\DeleteTeacher::class] = function ($c) {
return new Action\Staff\DeleteTeacher(
$c->get(Service\StaffServiceInterface::class)
);
};
$container[GrEduLabs\Schools\Action\Labs::class] = function ($c) {
return new GrEduLabs\Schools\Action\Labs(
$container[Action\Labs::class] = function ($c) {
return new Action\Labs(
$c->get('view')
);
};
$container[GrEduLabs\Schools\Action\LabCreate::class] = function ($c) {
return new GrEduLabs\Schools\Action\LabCreate(
$container[Action\LabCreate::class] = function ($c) {
return new Action\LabCreate(
$c->get('labservice')
);
};
$container[GrEduLabs\Schools\Action\Assets\ListAssets::class] = function ($c) {
return new GrEduLabs\Schools\Action\Assets\ListAssets(
$container[Action\Assets\ListAssets::class] = function ($c) {
return new Action\Assets\ListAssets(
$c->get('view'),
$c->get(GrEduLabs\Schools\Service\AssetServiceInterface::class),
$c->get(GrEduLabs\Schools\Service\SchoolAssetsInterface::class),
$c->get(GrEduLabs\Schools\Service\LabServiceInterface::class)
$c->get(Service\AssetServiceInterface::class),
$c->get(Service\SchoolAssetsInterface::class),
$c->get(Service\LabServiceInterface::class)
);
};
$container[GrEduLabs\Schools\Action\Assets\PersistAsset::class] = function ($c) {
return new GrEduLabs\Schools\Action\Assets\PersistAsset(
$c->get(GrEduLabs\Schools\Service\SchoolAssetsInterface::class)
$container[Action\Assets\PersistAsset::class] = function ($c) {
return new Action\Assets\PersistAsset(
$c->get(Service\SchoolAssetsInterface::class)
);
};
$container[GrEduLabs\Schools\Action\Assets\DeleteAsset::class] = function ($c) {
return new GrEduLabs\Schools\Action\Assets\DeleteAsset(
$c->get(GrEduLabs\Schools\Service\SchoolAssetsInterface::class)
$container[Action\Assets\DeleteAsset::class] = function ($c) {
return new Action\Assets\DeleteAsset(
$c->get(Service\SchoolAssetsInterface::class)
);
};
// services
$container['schoolservice'] = function ($c) {
return $c->get(GrEduLabs\Schools\Service\SchoolServiceInterface::class);
return $c->get(Service\SchoolServiceInterface::class);
};
$container['staffservice'] = function ($c) {
return $c->get(GrEduLabs\Schools\Service\StaffServiceInterface::class);
return $c->get(Service\StaffServiceInterface::class);
};
$container['labservice'] = function ($c) {
return $c->get(GrEduLabs\Schools\Service\LabServiceInterface::class);
return $c->get(Service\LabServiceInterface::class);
};
$container[GrEduLabs\Schools\Service\SchoolServiceInterface::class] = function ($c) {
return new GrEduLabs\Schools\Service\SchoolService();
$container[Service\SchoolServiceInterface::class] = function ($c) {
return new Service\SchoolService();
};
$container[GrEduLabs\Schools\Service\StaffServiceInterface::class] = function ($c) {
return new GrEduLabs\Schools\Service\StaffService();
$container[Service\StaffServiceInterface::class] = function ($c) {
return new Service\StaffService();
};
$container[GrEduLabs\Schools\Service\LabServiceInterface::class] = function ($c) {
return new GrEduLabs\Schools\Service\LabService(
$c->get(GrEduLabs\Schools\Service\SchoolServiceInterface::class),
$c->get(GrEduLabs\Schools\Service\StaffServiceInterface::class)
$container[Service\LabServiceInterface::class] = function ($c) {
return new Service\LabService(
$c->get(Service\SchoolServiceInterface::class),
$c->get(Service\StaffServiceInterface::class)
);
};
$container[GrEduLabs\Schools\Service\AssetServiceInterface::class] = function ($c) {
return new GrEduLabs\Schools\Service\AssetService();
$container[Service\AssetServiceInterface::class] = function ($c) {
return new Service\AssetService();
};
$container[GrEduLabs\Schools\Service\SchoolAssetsInterface::class] = function ($c) {
return $c->get(GrEduLabs\Schools\Service\AssetServiceInterface::class);
$container[Service\SchoolAssetsInterface::class] = function ($c) {
return $c->get(Service\AssetServiceInterface::class);
};
// middleware
$container[GrEduLabs\Schools\Middleware\InputFilterTeacher::class] = function ($c) {
return new GrEduLabs\Schools\Middleware\InputFilterTeacher(
$c->get(GrEduLabs\Schools\InputFilter\Teacher::class)
$container[Middleware\InputFilterTeacher::class] = function ($c) {
return new Middleware\InputFilterTeacher(
$c->get(InputFilter\Teacher::class)
);
};
$container[GrEduLabs\Schools\Middleware\InputFilterSchoolAsset::class] = function ($c) {
return new GrEduLabs\Schools\Middleware\InputFilterSchoolAsset(
$c->get(GrEduLabs\Schools\InputFilter\SchoolAsset::class)
$container[Middleware\InputFilterSchoolAsset::class] = function ($c) {
return new Middleware\InputFilterSchoolAsset(
$c->get(InputFilter\SchoolAsset::class)
);
};
$container[GrEduLabs\Schools\Middleware\FetchSchoolFromIdentity::class] = function ($c) {
return new GrEduLabs\Schools\Middleware\FetchSchoolFromIdentity($c['authentication_service']);
$container[Middleware\FetchSchoolFromIdentity::class] = function ($c) {
return new Middleware\FetchSchoolFromIdentity($c['authentication_service']);
};
// inputfilters
$container[GrEduLabs\Schools\InputFilter\Teacher::class] = function ($c) {
return new GrEduLabs\Schools\InputFilter\Teacher();
$container[InputFilter\Teacher::class] = function ($c) {
return new InputFilter\Teacher();
};
$container[InputFilter\SchoolAsset::class] = function ($c) {
return new InputFilter\SchoolAsset(
$c->get(Service\LabServiceInterface::class),
$c->get(Service\AssetServiceInterface::class)
);
};
$container[GrEduLabs\Schools\InputFilter\SchoolAsset::class] = function ($c) {
return new GrEduLabs\Schools\InputFilter\SchoolAsset(
$c->get(GrEduLabs\Schools\Service\LabServiceInterface::class),
$c->get(GrEduLabs\Schools\Service\AssetServiceInterface::class)
$container[InputFilter\School::class] = function ($c) {
return new InputFilter\School(
$c->get(Service\SchoolServiceInterface::class)
);
};
......@@ -155,23 +166,21 @@ return function (Slim\App $app) {
$app->group('/school', function () {
$this->get('', GrEduLabs\Schools\Action\Index::class)->setName('school');
$this->get('', Action\Index::class)->setName('school');
$this->get('/staff', GrEduLabs\Schools\Action\Staff\ListAll::class)->setName('school.staff');
$this->post('/staff', GrEduLabs\Schools\Action\Staff\PersistTeacher::class)
->add(GrEduLabs\Schools\Middleware\InputFilterTeacher::class)
->setName('school.staffcreate');
$this->delete('/staff/{id:[1-9][0-9]*}', GrEduLabs\Schools\Action\Staff\DeleteTeacher::class)
->setName('school.staffdelete');
$this->get('/staff', Action\Staff\ListAll::class)->setName('school.staff');
$this->post('/staff', Action\Staff\PersistTeacher::class)
->add(Middleware\InputFilterTeacher::class);
$this->delete('/staff', Action\Staff\DeleteTeacher::class);
$this->get('/labs', GrEduLabs\Schools\Action\Labs::class)->setName('school.labs');
$this->post('/labs', GrEduLabs\Schools\Action\LabCreate::class)->setName('school.labcreate');
$this->get('/labs', Action\Labs::class)->setName('school.labs');
$this->post('/labs', Action\LabCreate::class)->setName('school.labcreate');
$this->get('/assets', GrEduLabs\Schools\Action\Assets\ListAssets::class)->setName('school.assets');
$this->post('/assets', GrEduLabs\Schools\Action\Assets\PersistAsset::class)
->add(GrEduLabs\Schools\Middleware\InputFilterSchoolAsset::class);
$this->delete('/assets', GrEduLabs\Schools\Action\Assets\DeleteAsset::class);
$this->get('/assets', Action\Assets\ListAssets::class)->setName('school.assets');
$this->post('/assets', Action\Assets\PersistAsset::class)
->add(Middleware\InputFilterSchoolAsset::class);
$this->delete('/assets', Action\Assets\DeleteAsset::class);
})->add(GrEduLabs\Schools\Middleware\FetchSchoolFromIdentity::class);
})->add(Middleware\FetchSchoolFromIdentity::class);
});
};
......@@ -162,9 +162,12 @@
return;
}
$.ajax({
url: this.url + '/' + this.teacher.get('id'),
url: that.url,
type: 'delete',
dataType: 'json'
dataType: 'json',
data: {
'id': that.teacher.get('id')
}
}).done(function () {
that.model.remove(that.teacher.get('id'));
that.hide();
......
<?php
/**
* gredu_labs.
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
*
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
namespace GrEduLabs\Schools\Action;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Views\Twig;
class Assets
{
protected $view;
public function __construct(Twig $view)
{
$this->view = $view;
}
public function __invoke(Request $req, Response $res, array $args = [])
{
return $this->view->render($res, 'schools/assets.twig', [
'assets' => [
[
'id' => 1,
'type' => 1,
'typeName' => 'ΒΙΝΤΕΟΠΡΟΒΟΛΕΑΣ',
'lab' => 1,
'labName' => 'ΕΡΓΑΣΤΗΡΙΟ ΠΛΗΡΟΦ/ΚΗΣ 1',
'quantity' => 2,
'acquisitionYear' => '2001',
'comments' => 'test',
],
[
'id' => 2,
'type' => 3,
'typeName' => 'LAPTOP',
'lab' => 2,
'labName' => 'ΕΡΓΑΣΤΗΡΙΟ ΠΛΗΡΟΦ/ΚΗΣ 2',
'quantity' => 1,
'acquisitionYear' => '2003',
'comments' => 'test',
],
[
'id' => 3,
'type' => 2,
'typeName' => 'ΠΛΗΚΤΡΟΛΟΓΙΟ',
'lab' => 2,
'labName' => 'ΕΡΓΑΣΤΗΡΙΟ ΠΛΗΡΟΦ/ΚΗΣ 2',
'quantity' => 10,
'acquisitionYear' => '2008',
'comments' => 'test',
],
],
]);
}
}
......@@ -28,7 +28,7 @@ class Index
$school = $req->getAttribute('school');
return $this->view->render($res, 'schools/index.twig', [
'school' => $school->export(),
'school' => $school,
]);
}
}
......@@ -28,13 +28,13 @@ class DeleteTeacher
$this->staffService = $staffService;
}
public function __invoke(Request $req, Response $res, array $args = [])
public function __invoke(Request $req, Response $res)
{
$school = $req->getAttribute('school', false);
if (!$school->id) {
return $res->withStatus(403, 'No school');
}
$id = isset($args['id']) ? $args['id'] : false;
$id = $req->getParam('id', false);
if (!$id) {
$res = $res->withStatus(404);
......
<?php
/**
* gredu_labs
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
namespace GrEduLabs\Schools\InputFilter;
use Exception;
use GrEduLabs\Schools\Service\SchoolServiceInterface;
use Zend\Filter;
use Zend\InputFilter\Input;
use Zend\InputFilter\InputFilter;
use Zend\Validator;
class School
{
use InputFilterTrait;
public function __construct(
SchoolServiceInterface $schoolService
) {
$id = new Input('id');
$id->setRequired(false)
->getValidatorChain()
->attach(new Validator\Digits());
$registryNo = new Input('registry_no');
$registryNo->setRequired(true)
->getValidatorChain()
->attach(new Validator\NotEmpty())
->attach(new Validator\Callback([
'callback' => function ($value, $context = []) use ($schoolService) {
$id = isset($context['id']) ? $context['id'] : false;
if (false === $id) {
throw new Exception('No id in context');
}
try {
$school = $schoolService->findSchoolByRegistryNo($value);
if (!$school || (isset($school['id']) && !$school['id'])) {
return true;
}
return $school['id'] == $id;
} catch (Exception $ex) {
return false;
}
},
'messageTemplate' => 'Το σχολείο με κωδικό %value% υπάρχει ήδη',
]));
$name = new Input('name');
$name->setRequired(true)
->getFilterChain()