Add BaseVoter and Authorization Service Provider

PLUG-112
This commit is contained in:
Benoît Burnichon
2015-09-14 22:49:26 +02:00
parent c808e8a923
commit d9604ae300
6 changed files with 180 additions and 9 deletions

View File

@@ -0,0 +1,52 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2015 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Authorization;
use Alchemy\Phrasea\Application as PhraseaApplication;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManager;
class AuthorizationServiceProvider implements ServiceProviderInterface
{
public function register(Application $app)
{
$app['phraseanet.security_token'] = $app->share(function (PhraseaApplication $app) {
$user = $app['authentication']->getUser();
if ($user instanceof \User_Adapter) {
return new PreAuthenticatedToken((string)$user->get_id(), null, 'fake', ['ROLE_USER']);
}
return new AnonymousToken('fake', 'anon.', []);
});
$app['phraseanet.access_manager'] = $app->share(function (PhraseaApplication $app) {
return new AccessDecisionManager($app['phraseanet.voters']);
});
$app['phraseanet.voters'] = $app->share(function () {
return [];
});
$app['phraseanet.authorization_checker'] = $app->share(function (PhraseaApplication $app) {
return new AuthorizationChecker(
$app['phraseanet.access_manager'],
$app['phraseanet.security_token']
);
});
}
public function boot(Application $app)
{
// Nothing to do
}
}