split bootstraping process

parent 16e3778b
......@@ -11,88 +11,85 @@
return function (Slim\App $app) {
$container = $app->getContainer();
$events = $container['events'];
// setup RedbeanPHP
define('REDBEAN_MODEL_PREFIX', '');
RedBeanPHP\R::setup(
$container['settings']['db']['dsn'],
$container['settings']['db']['user'],
$container['settings']['db']['pass']
);
$container['autoloader']->addPsr4('GrEduLabs\\Application\\', __DIR__ . '/src');
$container['view'] = function ($c) {
$settings = $c['settings'];
$view = new Slim\Views\Twig(
$settings['view']['template_path'],
$settings['view']['twig']
);
$view->addExtension(new Slim\Views\TwigExtension(
$c['router'],
$c['request']->getUri()
));
$view->addExtension($c[GrEduLabs\Application\Twig\Extension\Navigation::class]);
$view->addExtension(new Knlv\Slim\Views\TwigMessages(
$c['flash']
));
return $view;
};
$container[GrEduLabs\Application\Twig\Extension\Navigation::class] = function ($c) {
return new GrEduLabs\Application\Twig\Extension\Navigation(
$c['settings']['navigation'],
$c['router'],
$c['request']
);
};
$container['flash'] = function ($c) {
return new \Slim\Flash\Messages();
};
$container['logger'] = function ($c) {
$settings = $c['settings'];
$logger = new Monolog\Logger($settings['logger']['name']);
$logger->pushProcessor(new Monolog\Processor\UidProcessor());
$logger->pushHandler(new Monolog\Handler\RotatingFileHandler(
$settings['logger']['path'],
$settings['logger']['max_files'],
Monolog\Logger::INFO
));
return $logger;
};
$container['csrf'] = function ($c) {
return new \Slim\Csrf\Guard();
};
$container['GrEduLabs\\Application\\Action\\Index'] = function ($c) {
return new GrEduLabs\Application\Action\Index($c['view']);
};
$events = $container['events'];
$events('on', 'app.autoload', function ($stop, $autoloader) {
$autoloader->addPsr4('GrEduLabs\\Application\\', __DIR__ . '/src');
});
$events('on', 'bootstrap', function () use ($container) {
$events('on', 'app.services', function ($stop, $container) {
session_name('GrEduLabs');
session_start();
}, 100000000000);
$events('on', 'bootstrap', function () use ($container) {
// setup RedbeanPHP
define('REDBEAN_MODEL_PREFIX', '');
RedBeanPHP\R::setup(
$container['settings']['db']['dsn'],
$container['settings']['db']['user'],
$container['settings']['db']['pass']
);
$container['view'] = function ($c) {
$settings = $c['settings'];
$view = new Slim\Views\Twig(
$settings['view']['template_path'],
$settings['view']['twig']
);
$view->addExtension(new Slim\Views\TwigExtension(
$c['router'],
$c['request']->getUri()
));
$view->addExtension($c[GrEduLabs\Application\Twig\Extension\Navigation::class]);
$view->addExtension(new Knlv\Slim\Views\TwigMessages(
$c['flash']
));
return $view;
};
$container[GrEduLabs\Application\Twig\Extension\Navigation::class] = function ($c) {
return new GrEduLabs\Application\Twig\Extension\Navigation(
$c['settings']['navigation'],
$c['router'],
$c['request']
);
};
$container['flash'] = function ($c) {
return new \Slim\Flash\Messages();
};
$container['logger'] = function ($c) {
$settings = $c['settings'];
$logger = new Monolog\Logger($settings['logger']['name']);
$logger->pushProcessor(new Monolog\Processor\UidProcessor());
$logger->pushHandler(new Monolog\Handler\RotatingFileHandler(
$settings['logger']['path'],
$settings['logger']['max_files'],
Monolog\Logger::INFO
));
return $logger;
};
$container['csrf'] = function ($c) {
return new \Slim\Csrf\Guard();
};
$container['GrEduLabs\\Application\\Action\\Index'] = function ($c) {
return new GrEduLabs\Application\Action\Index($c['view']);
};
});
$events('on', 'app.bootstrap', function ($stop, $app, $container) {
foreach ($container['router']->getRoutes() as $route) {
if ('user.login' === $route->getName()) {
$route->add('csrf');
break;
}
}
});
$app->get('/', 'GrEduLabs\\Application\\Action\\Index')->setName('index');
$app->get('/', 'GrEduLabs\\Application\\Action\\Index')->setName('index');
});
};
......@@ -71,7 +71,7 @@ return function (Slim\App $app) {
$container = $app->getContainer();
$events = $container['events'];
$events('on', 'bootstrap', function () use ($app, $container, $mimeTypes) {
$events('on', 'app.bootstrap', function ($stop, $app, $container) use ($mimeTypes) {
$app->add(function (
ServerRequestInterface $req,
ResponseInterface $res,
......
......@@ -11,82 +11,85 @@
return function (Slim\App $app) {
$container = $app->getContainer();
$events = $container['events'];
$container['autoloader']->addPsr4('GrEduLabs\\Authentication\\', __DIR__ . '/src');
$container['authentication_storage'] = function ($c) {
return new GrEduLabs\Authentication\Storage\PhpSession();
};
$container['authentication_adapter'] = function ($c) {
return new GrEduLabs\Authentication\Adapter\RedBeanPHP(
$c['events'],
$c['identity_class_resolver'],
$c['authentication_crypt']
);
};
$container['authentication_service'] = function ($c) {
return new Zend\Authentication\AuthenticationService(
$c['authentication_storage'],
$c['authentication_adapter']
);
};
$container['identity_class_resolver'] = $container->protect(function () {
return 'GrEduLabs\\Authentication\\Identity';
$events('on', 'app.autoload', function ($stop, $autoloader) {
$autoloader->addPsr4('GrEduLabs\\Authentication\\', __DIR__ . '/src');
});
$container['authentication_crypt'] = function ($c) {
$service = new Zend\Crypt\Password\Bcrypt();
if (isset($c['settings']['authentication']['bcrypt']['salt'])) {
$service->setSalt($c->settings['authentication']['bcrypt']['salt']);
}
if (isset($c['settings']['authentication']['bcrypt']['cost'])) {
$service->setCost($c->settings['authentication']['bcrypt']['cost']);
}
return $service;
};
$container[GrEduLabs\Authentication\Action\User\Login::class] = function ($c) {
return new GrEduLabs\Authentication\Action\User\Login(
$c['view'],
$c['authentication_service'],
$c['flash'],
$c['csrf'],
$c['router']->pathFor('index')
);
};
$container[GrEduLabs\Authentication\Action\User\Logout::class] = function ($c) {
return new GrEduLabs\Authentication\Action\User\Logout(
$c['authentication_service'],
$c['events'],
$c['router']->pathFor('index')
);
};
$nav = $container['settings']->get('navigation');
$nav['authentication'] = [
'login' => [
'label' => 'Σύνδεση',
'route' => 'user.login',
'icon' => 'unlock',
],
'logout' => [
'label' => 'Αποσύνδεση',
'route' => 'user.logout',
'id' => 'nav-logout',
'icon' => 'lock',
],
];
$container['settings']->set('navigation', $nav);
$events('on', 'app.services', function ($stop, $container) {
$container['authentication_storage'] = function ($c) {
return new GrEduLabs\Authentication\Storage\PhpSession();
};
$container['authentication_adapter'] = function ($c) {
return new GrEduLabs\Authentication\Adapter\RedBeanPHP(
$c['events'],
$c['identity_class_resolver'],
$c['authentication_crypt']
);
};
$container['authentication_service'] = function ($c) {
return new Zend\Authentication\AuthenticationService(
$c['authentication_storage'],
$c['authentication_adapter']
);
};
$container['identity_class_resolver'] = $container->protect(function () {
return 'GrEduLabs\\Authentication\\Identity';
});
$events = $container['events'];
$container['authentication_crypt'] = function ($c) {
$service = new Zend\Crypt\Password\Bcrypt();
if (isset($c['settings']['authentication']['bcrypt']['salt'])) {
$service->setSalt($c->settings['authentication']['bcrypt']['salt']);
}
if (isset($c['settings']['authentication']['bcrypt']['cost'])) {
$service->setCost($c->settings['authentication']['bcrypt']['cost']);
}
return $service;
};
$container[GrEduLabs\Authentication\Action\User\Login::class] = function ($c) {
return new GrEduLabs\Authentication\Action\User\Login(
$c['view'],
$c['authentication_service'],
$c['flash'],
$c['csrf'],
$c['router']->pathFor('index')
);
};
$container[GrEduLabs\Authentication\Action\User\Logout::class] = function ($c) {
return new GrEduLabs\Authentication\Action\User\Logout(
$c['authentication_service'],
$c['events'],
$c['router']->pathFor('index')
);
};
$nav = $container['settings']->get('navigation');
$nav['authentication'] = [
'login' => [
'label' => 'Σύνδεση',
'route' => 'user.login',
'icon' => 'unlock',
],
'logout' => [
'label' => 'Αποσύνδεση',
'route' => 'user.logout',
'id' => 'nav-logout',
'icon' => 'lock',
],
];
$container['settings']->set('navigation', $nav);
});
$events('on', 'bootstrap', function () use ($app, $container) {
$events('on', 'app.services', function ($stop, $container) {
$container->extend('view', function ($view, $c) {
$view->getEnvironment()->getLoader()->prependPath(__DIR__ . '/templates');
$view->addExtension(new GrEduLabs\Authentication\Twig\Extension\Identity(
......@@ -95,7 +98,9 @@ return function (Slim\App $app) {
return $view;
});
}, -10);
$events('on', 'app.bootstrap', function ($stop, $app, $container) {
$app->group('/user', function () {
$this->map(['GET', 'POST'], '/login', GrEduLabs\Authentication\Action\User\Login::class)
->setName('user.login');
......
......@@ -11,57 +11,59 @@
return function (Slim\App $app) {
$container = $app->getContainer();
$events = $container['events'];
$container['autoloader']->addPsr4('GrEduLabs\\Authorization\\', __DIR__ . '/src');
$container['settings']->set('determineRouteBeforeAppMiddleware', true);
$container[GrEduLabs\Authorization\Acl::class] = function ($c) {
$settings = $c['settings'];
return new GrEduLabs\Authorization\Acl($settings['acl'], $c);
};
$container['acl'] = $container->protect(function () use ($container) {
return $container[GrEduLabs\Authorization\Acl::class];
$events('on', 'app.autoload', function ($stop, $autoloader) {
$autoloader->addPsr4('GrEduLabs\\Authorization\\', __DIR__ . '/src');
});
$container['current_role'] = $container->protect(function () use ($container) {
$settings = $container['settings'];
$defaultRole = $settings['acl']['default_role'];
$identity = $container['authentication_service']->getIdentity();
if ($identity && $identity instanceof GrEduLabs\Authorization\RoleAwareInterface &&
($role = $identity->getRole())) {
return $role;
}
$events('on', 'app.services', function ($stop, $container) {
$container['settings']->set('determineRouteBeforeAppMiddleware', true);
return $defaultRole;
});
$container[GrEduLabs\Authorization\Acl::class] = function ($c) {
$settings = $c['settings'];
$container[GrEduLabs\Authorization\RouteGuard::class] = function ($c) {
$role = call_user_func($c['current_role']);
return new GrEduLabs\Authorization\Acl($settings['acl'], $c);
};
return new GrEduLabs\Authorization\RouteGuard($c[GrEduLabs\Authorization\Acl::class], $role);
};
$container[GrEduLabs\Authorization\Middleware\RoleProvider::class] = function ($c) {
return new GrEduLabs\Authorization\Middleware\RoleProvider(
$c['authentication_service'],
$c[GrEduLabs\Authorization\Acl::class]
);
};
$container['acl'] = $container->protect(function () use ($container) {
return $container[GrEduLabs\Authorization\Acl::class];
});
$container[GrEduLabs\Authorization\Listener\RoleProvider::class] = function ($c) {
return new GrEduLabs\Authorization\Listener\RoleProvider(
$c['authentication_storage'],
$c[GrEduLabs\Authorization\Acl::class]
);
};
$container['current_role'] = $container->protect(function () use ($container) {
$settings = $container['settings'];
$defaultRole = $settings['acl']['default_role'];
$identity = $container['authentication_service']->getIdentity();
if ($identity && $identity instanceof GrEduLabs\Authorization\RoleAwareInterface &&
($role = $identity->getRole())) {
return $role;
}
$events = $container['events'];
return $defaultRole;
});
$events('on', 'bootstrap', function () use ($app, $container) {
$container[GrEduLabs\Authorization\RouteGuard::class] = function ($c) {
$role = call_user_func($c['current_role']);
return new GrEduLabs\Authorization\RouteGuard($c[GrEduLabs\Authorization\Acl::class], $role);
};
$container[GrEduLabs\Authorization\Middleware\RoleProvider::class] = function ($c) {
return new GrEduLabs\Authorization\Middleware\RoleProvider(
$c['authentication_service'],
$c[GrEduLabs\Authorization\Acl::class]
);
};
$container[GrEduLabs\Authorization\Listener\RoleProvider::class] = function ($c) {
return new GrEduLabs\Authorization\Listener\RoleProvider(
$c['authentication_storage'],
$c[GrEduLabs\Authorization\Acl::class]
);
};
});
$events('on', 'app.services', function ($stop, $container) {
$container->extend('identity_class_resolver', function () {
return function () {
return 'GrEduLabs\\Authorization\\Identity';
......@@ -73,7 +75,9 @@ return function (Slim\App $app) {
->setAcl($c[GrEduLabs\Authorization\Acl::class])
->setCurrentRole(call_user_func($c['current_role']));
});
}, -10);
$events('on', 'app.bootstrap', function ($stop, $app, $container) {
foreach ($container['router']->getRoutes() as $route) {
if ('user.login' === $route->getName()) {
$route->add(GrEduLabs\Authorization\Middleware\RoleProvider::class);
......
......@@ -11,24 +11,29 @@
return function (Slim\App $app) {
$container = $app->getContainer();
$events = $container['events'];
$container['settings']->set('displayErrorDetails', true);
$events('on', 'app.services', function ($stop, $container) {
$container['settings']->set('displayErrorDetails', true);
});
$container->extend('view', function ($view) {
$view->addExtension(new Twig_Extension_Debug());
$view->getEnvironment()->enableDebug();
$events('on', 'app.services', function ($stop, $container) {
$container->extend('view', function ($view) {
$view->addExtension(new Twig_Extension_Debug());
$view->getEnvironment()->enableDebug();
return $view;
});
return $view;
});
$container->extend('logger', function ($logger, $c) {
$settings = $c['settings'];
$container->extend('logger', function ($logger, $c) {
$settings = $c['settings'];
$logger->pushHandler(new Monolog\Handler\StreamHandler(
$settings['logger']['debug_path'],
Monolog\Logger::DEBUG
));
$logger->pushHandler(new Monolog\Handler\StreamHandler(
$settings['logger']['debug_path'],
Monolog\Logger::DEBUG
));
return $logger;
});
return $logger;
});
}, -10);
};
......@@ -11,14 +11,19 @@
return function (Slim\App $app) {
$container = $app->getContainer();
$events = $container['events'];
$container['autoloader']->addPsr4('SchInventory\\', __DIR__ . '/src/');
$events('on', 'app.autoload', function ($stop, $autoloader) {
$autoloader->addPsr4('SchInventory\\', __DIR__ . '/src/');
});
$container['SchInventory\\Service'] = function ($c) {
$settings = $c['settings'];
$events('on', 'app.services', function ($stop, $container) {
$container['SchInventory\\Service'] = function ($c) {
$settings = $c['settings'];
return new SchInventory\GuzzleHttpService(
new GuzzleHttp\Client($settings['inventory'])
);
};
return new SchInventory\GuzzleHttpService(
new GuzzleHttp\Client($settings['inventory'])
);
};
});
};
......@@ -11,10 +11,13 @@
return function (Slim\App $app) {
$container = $app->getContainer();
$events = $container['events'];
$container['ldap'] = function ($c) {
$settings = $c['settings']['ldap'];
$events('on', 'app.services', function ($stop, $container) {
$container['ldap'] = function ($c) {
$settings = $c['settings']['ldap'];
return new Zend\Ldap\Ldap($settings);
};
return new Zend\Ldap\Ldap($settings);
};
});
};
......@@ -11,18 +11,24 @@
return function (Slim\App $app) {
$container = $app->getContainer();
$events = $container['events'];
$container['autoloader']->addPsr4('SchMM\\', __DIR__ . '/src/');
$events('on', 'app.autoload', function ($stop, $autoloader) {
$autoloader->addPsr4('SchMM\\', __DIR__ . '/src/');
});
$container[SchMM\FetchUnit::class] = function ($c) {
$settings = $c['settings'];
$events('on', 'app.services', function ($stop, $container) {
$container[SchMM\FetchUnit::class] = function ($c) {
$settings = $c['settings'];
return new SchMM\FetchUnit(new GuzzleHttp\Client([
'base_uri' => $settings['sch_mm']['api_url'],
'auth' => [
$settings['sch_mm']['api_user'],
$settings['sch_mm']['api_pass'],
],
]));
};
});
return new SchMM\FetchUnit(new GuzzleHttp\Client([
'base_uri' => $settings['sch_mm']['api_url'],
'auth' => [
$settings['sch_mm']['api_user'],
$settings['sch_mm']['api_pass'],
],
]));
};
};
......@@ -12,89 +12,107 @@ return function (Slim\App $app) {
$container = $app->getContainer();
$container['autoloader']->addPsr4('SchSSO\\', __DIR__ . '/src/');
$container['init_cas'] = $container->protect(function () use ($container) {
$settings = $container['settings']['sso']['phpcas'];
phpCAS::client(
$settings['serverVersion'],
$settings['serverHostname'],
$settings['serverPort'],
$settings['serverUri'],
$settings['changeSessionId']
);
if (($casServerCaCert = $settings['casServerCaCert'])) {
if ($settings['casServerCnValidate']) {
phpCAS::setCasServerCACert($casServerCaCert, true);
} else {
phpCAS::setCasServerCACert($casServerCaCert, false);
}
}
$events = $container['events'];
if ($settings['noCasServerValidation']) {
phpCAS::setNoCasServerValidation();
}
phpCAS::handleLogoutRequests();
$events('on', 'app.autoload', function ($stop, $autoloader) {
$autoloader->addPsr4('SchSSO\\', __DIR__ . '/src/');
});
$container['is_allowed'] = $container->protect(function ($attributes) use ($container) {
$allowed = isset($container['settings']['sso']['allowed'])
? $container['settings']['sso']['allowed'] : [];
foreach ($allowed as $index => $ruleset) {
$isAllowed[$index] = true;
foreach ($ruleset as $attribute => $rule) {
if (!isset($attributes[$attribute])) {
$isAllowed[$index] = false;
break;
}
if (!is_array($attributes[$attribute])) {
$attributes[$attribute] = [$attributes[$attribute]];
$events('on', 'app.services', function ($stop, $container) {
$container['init_cas'] = $container->protect(function () use ($container) {
$settings = $container['settings']['sso']['phpcas'];
phpCAS::client(
$settings['serverVersion'],
$settings['serverHostname'],
$settings['serverPort'],
$settings['serverUri'],
$settings['changeSessionId']
);
if (($casServerCaCert = $settings['casServerCaCert'])) {
if ($settings['casServerCnValidate']) {
phpCAS::setCasServerCACert($casServerCaCert, true);
} else {
phpCAS::setCasServerCACert($casServerCaCert, false);
}
foreach ($attributes[$attribute] as $value) {
$isAllowed[$index] &= (1 === preg_match($rule, $value));
}
if ($settings['noCasServerValidation']) {
phpCAS::setNoCasServerValidation();
}
phpCAS::handleLogoutRequests();
});
$container['is_allowed'] = $container->protect(function ($attributes) use ($container) {
$allowed = isset($container['settings']['sso']['allowed'])
? $container['settings']['sso']['allowed'] : [];
foreach ($allowed as $index => $ruleset) {
$isAllowed[$index] = true;
foreach ($ruleset as $attribute => $rule) {
if (!isset($attributes[$attribute])) {
$isAllowed[$index] = false;
break;
}
if (!is_array($attributes[$attribute])) {
$attributes[$attribute] = [$attributes[$attribute]];
}
foreach ($attributes[$attribute] as $value) {
$isAllowed[$index] &= (1 === preg_match($rule, $value));
}
}
}
}