Commit 37725163 authored by Vassilis Kanellopoulos's avatar Vassilis Kanellopoulos Committed by kanellov
Browse files

assert no application form for school

parent f2b7874e
...@@ -27,7 +27,7 @@ return [ ...@@ -27,7 +27,7 @@ return [
['/school/staff', ['school'], ['get', 'post', 'delete']], ['/school/staff', ['school'], ['get', 'post', 'delete']],
['/school/assets', ['school'], ['get', 'post', 'delete']], ['/school/assets', ['school'], ['get', 'post', 'delete']],
['/school/labs/attachment', ['school'], ['get', 'delete']], ['/school/labs/attachment', ['school'], ['get', 'delete']],
['/application-form', ['school'], ['get', 'post']], ['/application-form', ['school'], ['get', 'post'], 'GrEduLabs\ApplicationForm\Acl\Assertion\CanSubmit'],
['/application-form/submit-success', ['school'], ['get']], ['/application-form/submit-success', ['school'], ['get']],
], ],
], ],
......
...@@ -61,6 +61,13 @@ return function (Slim\App $app) { ...@@ -61,6 +61,13 @@ return function (Slim\App $app) {
$c->get('router')->pathFor('application_form') $c->get('router')->pathFor('application_form')
); );
}; };
$container[GrEduLabs\ApplicationForm\Acl\Assertion\CanSubmit::class] = function ($c) {
return new GrEduLabs\ApplicationForm\Acl\Assertion\CanSubmit(
$c->get('authentication_service'),
$c->get(GrEduLabs\ApplicationForm\Service\ApplicationFormServiceInterface::class)
);
};
}); });
$events('on', 'app.bootstrap', function ($stop, $app, $container) { $events('on', 'app.bootstrap', function ($stop, $app, $container) {
......
<?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\ApplicationForm\Acl\Assertion;
use GrEduLabs\ApplicationForm\Service\ApplicationFormServiceInterface;
use RedBeanPHP\R;
use Zend\Authentication\AuthenticationServiceInterface;
use Zend\Permissions\Acl\Acl;
use Zend\Permissions\Acl\Assertion\AssertionInterface;
use Zend\Permissions\Acl\Resource\ResourceInterface;
use Zend\Permissions\Acl\Role\RoleInterface;
class CanSubmit implements AssertionInterface
{
/**
*
* @var AuthenticationServiceInterface
*/
protected $authService;
/**
*
* @var ApplicationFormServiceInterface
*/
protected $appFormService;
public function __construct(
AuthenticationServiceInterface $authService,
ApplicationFormServiceInterface $appFormService
) {
$this->authService = $authService;
$this->appFormService = $appFormService;
}
public function assert(Acl $acl, RoleInterface $role = null, ResourceInterface $resource = null, $privilege = null)
{
$identity = $this->authService->getIdentity();
$user = R::load('user', $identity->id);
if (!($school = $user->school)) {
return false;
}
$appForm = $this->appFormService->findSchoolApplicationForm($school->id);
return null === $appForm;
}
}
...@@ -29,7 +29,13 @@ class ApplicationForm extends InputFilter ...@@ -29,7 +29,13 @@ class ApplicationForm extends InputFilter
->getFilterChain() ->getFilterChain()
->attach(new Filter\ToInt()); ->attach(new Filter\ToInt());
$schoolId->getValidatorChain() $schoolId->getValidatorChain()
->attach(new Validator\NotEmpty()); ->attach(new Validator\NotEmpty())
->attach(new Validator\Callback([
'callback' => function ($value) use ($appFormService) {
return null === $appFormService->findSchoolApplicationForm($value);
},
'message' => 'Έχει ήδη γίνει αίτηση',
]));
$applyFor = new Input('apply_for'); $applyFor = new Input('apply_for');
$applyFor->setRequired(true) $applyFor->setRequired(true)
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
namespace GrEduLabs\ApplicationForm\Service; namespace GrEduLabs\ApplicationForm\Service;
use RedBeanPHP\OODBBean;
use RedBeanPHP\R; use RedBeanPHP\R;
class ApplicationFormService implements ApplicationFormServiceInterface class ApplicationFormService implements ApplicationFormServiceInterface
...@@ -52,7 +53,17 @@ class ApplicationFormService implements ApplicationFormServiceInterface ...@@ -52,7 +53,17 @@ class ApplicationFormService implements ApplicationFormServiceInterface
return $this->exportApplicationForm($appForm); return $this->exportApplicationForm($appForm);
} }
private function exportApplicationForm(\RedBeanPHP\OODBBean $bean) public function findSchoolApplicationForm($schoolId)
{
$appForm = R::findOne('applicationform', ' school_id = ? ', [$schoolId]);
if (null === $appForm) {
return;
}
return $this->exportApplicationForm($appForm);
}
private function exportApplicationForm(OODBBean $bean)
{ {
$appForm = $bean->export(); $appForm = $bean->export();
$appForm['items'] = array_map(function ($itemBean) { $appForm['items'] = array_map(function ($itemBean) {
......
...@@ -15,4 +15,6 @@ interface ApplicationFormServiceInterface ...@@ -15,4 +15,6 @@ interface ApplicationFormServiceInterface
public function getApplyForChoices(); public function getApplyForChoices();
public function submit(array $data); public function submit(array $data);
public function findSchoolApplicationForm($schoolId);
} }
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