remove old stuff

parent e7a3ecae
<?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
*/
$container = $app->getContainer();
// Twig
$container['view'] = function ($c) {
$settings = $c->get('settings');
$view = new \Slim\Views\Twig(
$settings['view']['template_path'],
$settings['view']['twig']
);
$view->addExtension(new Slim\Views\TwigExtension(
$c->get('router'),
$c->get('request')->getUri()
));
$view->addExtension(new Twig_Extension_Debug());
$view->addExtension(new Knlv\Slim\Views\TwigMessages(
$c->get('flash')
));
$view->addExtension(new GrEduLabs\Twig\Extension\Identity(
$c->get('authentication_service')
));
if (isset($settings['navigation']) && is_array($settings['navigation'])) {
$authService = $c->get('authentication_service');
$role = $settings['acl']['default_role'];
if ($authService->hasIdentity()) {
$identity = $authService->getIdentity();
if ($identity instanceof GrEduLabs\Authorization\RoleAwareInterface) {
$role = $identity->getRole();
}
}
$view->addExtension(new GrEduLabs\Twig\Extension\Navigation(
$settings['navigation'],
$c->get('router'),
$c->get('request'),
$c->get('acl_service'),
$role
));
}
return $view;
};
// Flash messages
$container['flash'] = function ($c) {
return new \Slim\Flash\Messages;
};
// Monolog
$container['logger'] = function ($c) {
$settings = $c->get('settings');
$logger = new \Monolog\Logger($settings['logger']['name']);
$logger->pushProcessor(new \Monolog\Processor\UidProcessor());
$logger->pushHandler(new \Monolog\Handler\StreamHandler(
$settings['logger']['path'],
\Monolog\Logger::DEBUG
));
return $logger;
};
// Event manager
$container['events'] = function ($c) {
return new \Zend\EventManager\EventManager(
new \Zend\EventManager\SharedEventManager(),
['events']
);
};
// Csrf guard
$container['csrf'] = function ($c) {
return new \Slim\Csrf\Guard;
};
// Database
$container['db'] = function ($c) {
$settings = $c->get('settings');
try {
$pdo = new \PDO(
$settings['db']['dsn'],
$settings['db']['user'],
$settings['db']['pass'],
$settings['db']['options']
);
return $pdo;
} catch (\PDOException $e) {
$c->get('logger')->error($e->getMessage());
return;
}
};
// Authentication service
$container['authentication_db_adapter'] = function ($c) {
return new \GrEduLabs\Authentication\Adapter\Pdo($c->get('db'));
};
$container['authentication_cas_adapter'] = function ($c) {
$settings = $c->get('settings');
$adapter = new GrEduLabs\Authentication\Adapter\Cas($settings['phpcas']);
$adapter->setIdentityPrototype(GrEduLabs\Authorization\Identity::class);
return $adapter;
};
$container['authentication_storage'] = function ($c) {
return new \GrEduLabs\Authentication\Storage\PhpSession();
};
$container['authentication_service'] = function ($c) {
return new \Zend\Authentication\AuthenticationService(
$c->get('authentication_storage')
);
};
$container['authentication_cas_logout_middleware'] = function ($c) {
return new GrEduLabs\Middleware\CasLogout(
$c->get('authentication_cas_adapter')
);
};
$container['set_identity_in_request'] = function ($c) {
return new GrEduLabs\Middleware\SetIdentityInRequest(
$c->get('authentication_service')
);
};
// Acl
$container['acl_service'] = function ($c) {
$settings = $c->get('settings');
return new GrEduLabs\Authorization\Acl($settings['acl'], $c);
};
$container['acl_guard_middleware'] = function ($c) {
$settings = $c->get('settings');
$authService = $c->get('authentication_service');
$role = $settings['acl']['default_role'];
if ($authService->hasIdentity()) {
$identity = $authService->getIdentity();
if ($identity instanceof GrEduLabs\Authorization\RoleAwareInterface) {
$role = $identity->getRole();
}
}
return new GrEduLabs\Authorization\GuardMiddleware($c->get('acl_service'), $role);
};
$container['provide_role_middleware'] = function ($c) {
return new GrEduLabs\Authorization\ProvideRoleMiddleware($c->get('authentication_service'));
};
// Inventory service
$container['inventory_service'] = function ($c) {
$settings = $c->get('settings');
return new GrEduLabs\Inventory\GuzzleHttpService(
new GuzzleHttp\Client($settings['inventory'])
);
};
// Actions
$container['GrEduLabs\\Action\\Index'] = function ($c) {
return new GrEduLabs\Action\Index($c->get('view'));
};
$container['GrEduLabs\\Action\\User\\Login'] = function ($c) {
$service = $service = $c->get('authentication_service');
$adapter = $c->get('authentication_db_adapter');
$service->setAdapter($adapter);
return new GrEduLabs\Action\User\Login(
$c->get('view'),
$service,
$adapter,
$c->get('flash'),
$c->get('csrf'),
$c->get('router')->pathFor('index')
);
};
$container['GrEduLabs\\Action\\User\\LoginSso'] = function ($c) {
$service = $c->get('authentication_service');
$adapter = $c->get('authentication_cas_adapter');
$service->setAdapter($adapter);
return new GrEduLabs\Action\User\LoginSso(
$service,
$c->get('flash'),
$c->get('router')->pathFor('index'),
$c->get('router')->pathFor('user.login')
);
};
$container['GrEduLabs\\Action\\User\\Logout'] = function ($c) {
return new GrEduLabs\Action\User\Logout(
$c->get('authentication_service'),
$c->get('router')->pathFor('index')
);
};
<?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
*/
// Application middleware
$app->add('acl_guard_middleware');
$app->add('set_identity_in_request');
<?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
*/
$app->get('/', 'GrEduLabs\\Action\\Index')->setName('index');
$app->get('/faq', function () {})->setName('faq');
// authentication
$app->group('/user', function () {
$this->map(['GET', 'POST'], '/login', 'GrEduLabs\\Action\\User\\Login')
->setName('user.login')
->add('provide_role_middleware')
->add('csrf');
$this->get('/login-sso', 'GrEduLabs\\Action\\User\\LoginSso')
->setName('user.loginSso')
->add('provide_role_middleware');
$this->get('/logout', 'GrEduLabs\\Action\\User\\Logout')
->setName('user.logout')
->add('authentication_cas_logout_middleware');
$this->get('/profile', 'GrEduLabs\\Action\\User\\Profile')
->setName('user.profile');
});
<?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\Action;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Views\Twig;
class Index
{
/**
* @var Twig
*/
protected $view;
/**
* Constructor
* @param Twig $view
*/
public function __construct(Twig $view)
{
$this->view = $view;
}
public function __invoke(Request $req, Response $res)
{
return $this->view->render($res, 'index.twig');
}
}
<?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\Action\User;
use Slim\Csrf\Guard;
use Slim\Flash\Messages;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Views\Twig;
use Zend\Authentication\Adapter\AdapterInterface;
use Zend\Authentication\Adapter\ValidatableAdapterInterface;
use Zend\Authentication\AuthenticationServiceInterface;
class Login
{
/**
* @var Twig
*/
protected $view;
/**
* @var AuthenticationServiceInterface
*/
protected $authService;
/**
* @var AdapterInterface
*/
protected $authAdapter;
/**
* @var Messages
*/
protected $flash;
/**
* @Var Guard
*/
protected $csrf;
/**
* @var string
*/
protected $successUrl;
/**
* Constructor
* @param Twig $view
* @param AuthenticationServiceInterface $authService
* @param AdapterInterface $authAdapter
* @param Messages $flash
*/
public function __construct(
Twig $view,
AuthenticationServiceInterface $authService,
AdapterInterface $authAdapter,
Messages $flash,
Guard $csrf,
$successUrl
) {
$this->view = $view;
$this->authService = $authService;
$this->authAdapter = $authAdapter;
$this->flash = $flash;
$this->csrf = $csrf;
$this->successUrl = $successUrl;
if (method_exists($this->authService, 'setAdapter')) {
$this->authService->setAdapter($this->authAdapter);
}
}
public function __invoke(Request $req, Response $res)
{
if ($req->isPost()) {
if ($this->authAdapter instanceof ValidatableAdapterInterface) {
$this->authAdapter->setIdentity($req->getParam('identity'));
$this->authAdapter->setCredential($req->getParam('credential'));
}
$result = $this->authService->authenticate($this->authAdapter);
if (!$result->isValid()) {
$this->flash->addMessage('danger', reset($result->getMessages()));
return $res->withRedirect($req->getUri());
}
return $res->withRedirect($this->successUrl);
}
return $this->view->render($res, 'user/login.twig', $this->getCsrfData($req));
}
private function getCsrfData(Request $req)
{
$nameKey = $this->csrf->getTokenNameKey();
$valueKey = $this->csrf->getTokenValueKey();
$name = $req->getAttribute($nameKey);
$value = $req->getAttribute($valueKey);
return [
'csrf_name_key' => $nameKey,
'csrf_value_key' => $valueKey,
'csrf_name' => $name,
'csrf_value' => $value,
];
}
}
<?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\Action\User;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Flash\Messages;
use Slim\Http\Response;
use Zend\Authentication\AuthenticationServiceInterface;
class LoginSso
{
/**
* @var AuthenticationServiceInterface
*/
protected $authService;
/**
* @var Messages
*/
protected $flash;
/**
* @var string
*/
protected $successUrl;
/**
* @var string
*/
protected $failureUrl;
/**
* Constructor
* @param AuthenticationServiceInterface $authService
* @param Messages $flash
*/
public function __construct(
AuthenticationServiceInterface $authService,
Messages $flash,
$successUrl,
$failureUrl
) {
$this->authService = $authService;
$this->flash = $flash;
$this->successUrl = $successUrl;
$this->failureUrl = $failureUrl;
}
public function __invoke(
ServerRequestInterface $req,
Response $res
) {
$result = $this->authService->authenticate();
if (!$result->isValid()) {
$this->flash->addMessage('danger', reset($result->getMessages()));
return $res->withRedirect($this->failureUrl);
}
return $res->withRedirect($this->successUrl);
}
}
<?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\Action\User;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Http\Response;
use Zend\Authentication\AuthenticationServiceInterface;
class Logout
{
/**
* @var AuthenticationServiceInterface
*/
protected $authService;
/**
* @var string
*/
protected $redirectUrl;
public function __construct(
AuthenticationServiceInterface $authService,
$redirectUrl
) {
$this->authService = $authService;
$this->redirectUrl = $redirectUrl;
}
public function __invoke(ServerRequestInterface $req, Response $res)
{
if ($this->authService->hasIdentity()) {
$this->authService->clearIdentity();
}
return $res->withRedirect($this->redirectUrl);
}
}
<?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\Authentication\Adapter;
use Exception;
use phpCAS;
use Zend\Authentication\Adapter\AdapterInterface;
use Zend\Authentication\Result;
class Cas implements AdapterInterface
{
use IdentityPrototypeCapableTrait;
/**
* @var bool
*/
protected $__init__ = false;
/**
* @var array
*/
protected $settings;
public function __construct(array $settings = [])
{
$this->settings = $settings;
}
public function authenticate()
{
try {
$this->init();
phpCAS::handleLogoutRequests();
phpCAS::forceAuthentication();
if (!phpCAS::isAuthenticated()) {
return new Result(Result::FAILURE, null, ['Authentication failure']);
}
return new Result(
Result::SUCCESS,
$this->identityFormCasAttributes(),
['Authentication success']
);
} catch (Exception $e) {
return new Result(Result::FAILURE_UNCATEGORIZED, null, [$e->getMessage()]);
}
}
public function logout($redirect = null)
{
$this->init();
if (!phpCAS::isAuthenticated()) {
return;
}
if ($redirect) {
phpCAS::logoutWithRedirectService((string) $redirect);
}
phpCAS::logout();
}
private function identityFormCasAttributes()
{
$attributes = phpCAS::getAttributes();
$identity = phpCAS::getUser();
$filterAttribute = function ($attribute) use ($attributes) {
if (!isset($attributes[$attribute])) {
return;
}
if (is_array($attributes[$attribute])) {
return $attributes[$attribute];
}
return $attributes[$attribute];
};
$identityClass = $this->identityPrototype;
return new $identityClass(
$identity,
$filterAttribute('mail'),
$filterAttribute('cn'),
$filterAttribute('ou'),
'CAS'
);
}
private function init()