1
0
Fork 0
forked from deblan/gist
gist/app/bootstrap.php.d/70-security.php

92 lines
2.9 KiB
PHP
Raw Normal View History

<?php
use Gist\Service\UserProvider;
use Silex\Provider\SecurityServiceProvider;
2016-05-31 22:20:17 +02:00
use Silex\Provider\RememberMeServiceProvider;
use Gist\Service\SaltGenerator;
use Gist\Security\AuthenticationProvider;
use Gist\Security\AuthenticationListener;
use Gist\Security\AuthenticationEntryPoint;
2015-11-24 18:57:06 +01:00
use Gist\Security\LogoutSuccessHandler;
use Silex\Provider\SessionServiceProvider;
2015-11-23 20:28:09 +01:00
use Symfony\Component\Security\Http\HttpUtils;
$app['enable_registration'] = true;
$app['enable_login'] = true;
2016-05-31 22:20:17 +02:00
$app['token'] = 'ThisTokenIsNotSoSecretChangeIt';
$app['salt_generator'] = $app->share(function($app) {
return new SaltGenerator();
});
$app['user.provider'] = $app->share(function ($app) {
return new UserProvider(
2015-11-21 18:28:48 +01:00
$app['security.encoder.digest'],
$app['salt_generator']
);
});
2015-11-23 20:28:09 +01:00
$app['security.authentication_listener.factory.form'] = $app->protect(function ($name, $options) use ($app) {
$app['security.authentication_provider.'.$name.'.form'] = $app->share(function ($app) {
return new AuthenticationProvider($app['user.provider']);
2015-11-21 18:28:48 +01:00
});
2015-11-23 20:28:09 +01:00
$app['security.authentication_listener.'.$name.'.form'] = $app->share(function ($app) use ($name) {
return new AuthenticationListener(
2015-11-21 18:28:48 +01:00
$app['security.token_storage'],
2015-11-23 20:28:09 +01:00
$app['security.authentication_provider.'.$name.'.form']
2015-11-21 18:28:48 +01:00
);
});
2015-11-24 18:57:06 +01:00
2015-11-21 18:28:48 +01:00
return [
2015-11-23 20:28:09 +01:00
'security.authentication_provider.'.$name.'.form',
'security.authentication_listener.'.$name.'.form',
null,
2015-11-21 18:28:48 +01:00
'pre_auth'
];
});
2015-11-24 18:57:06 +01:00
$app->register(
2015-11-21 18:28:48 +01:00
new SecurityServiceProvider(),
[
'security.firewalls' => [
'default' => [
2015-11-23 20:28:09 +01:00
'pattern' => '^/',
2015-11-21 18:28:48 +01:00
'anonymous' => true,
2015-11-23 20:28:09 +01:00
'form' => [
'login_path' => '_login',
2015-11-23 21:59:50 +01:00
'check_path' => '/login_check',
2015-11-24 18:57:06 +01:00
'always_use_default_target_path' => false,
2015-11-23 21:59:50 +01:00
'default_target_path' => '/',
],
'logout' => [
2015-11-23 20:28:09 +01:00
'path' => '/logout',
],
2015-11-21 18:28:48 +01:00
'users' => $app->share(function () use ($app) {
return $app['user.provider'];
}),
2016-05-31 22:20:17 +02:00
'remember_me' => [
'key' => $app['token'],
'path' => '/',
'always_remember_me' => false,
],
],
],
'security.access_rules' => [
2015-11-21 18:28:48 +01:00
['^/[a-z]{2}/my.*$', 'ROLE_USER'],
]
]
);
2015-11-24 18:57:06 +01:00
2016-05-31 22:20:17 +02:00
$app->register(new SessionServiceProvider());
$app->register(new RememberMeServiceProvider());
2015-11-24 18:57:06 +01:00
$app['security.authentication.logout_handler._proto'] = $app->protect(function ($name, $options) use ($app) {
return $app->share(function () use ($name, $options, $app) {
return new LogoutSuccessHandler(
$app['security.http_utils'],
isset($options['target_url']) ? $options['target_url'] : '/'
);
});
});