Add ACL Service

This commit is contained in:
Nicolas Le Goff
2013-10-30 14:18:37 +01:00
parent 414f6b60af
commit 21eec6dbbe
5 changed files with 153 additions and 19 deletions

View File

@@ -0,0 +1,92 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2013 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Authentication;
use Alchemy\Phrasea\Model\Entities\User;
use Silex\Application;
class ACLProvider
{
/**
* An array cache for ACL's.
*
* @var array
*/
private static $cache = array();
private $app;
public function __construct(Application $app)
{
$this->app = $app;
}
/**
* Gets ACL for user.
*
* @param User $user
*
* @return \ACL
*/
public function get(\User_Adapter $user)
{
if (null !== $acl = $this->fetchFromCache($user)) {
return $acl;
}
return $this->fetch($user);
}
/**
* Purges ACL cache
*/
public function purge()
{
self::$cache = array();
}
/**
* Fetchs ACL from cache for users.
*
* @param User $user
*
* @return null || \ACL
*/
private function fetchFromCache(\User_Adapter $user)
{
return $this->hasCache($user) ? self::$cache[$user->get_id()] : null;
}
/**
* Tells whether ACL for user is already cached.
*
* @param User $user
*
* @return boolean
*/
private function hasCache(\User_Adapter $user)
{
return array_key_exists($user->get_id(), self::$cache) && self::$cache[$user->get_id()] instanceof \ACL;
}
/**
* Saves user's ACL in cache and returns it.
*
* @param User $user
*
* @return \ACL
*/
private function fetch(\User_Adapter $user)
{
return self::$cache[$user->get_id()] = new \ACL($user, $this->app);
}
}

View File

@@ -11,6 +11,7 @@
namespace Alchemy\Phrasea\Core\Provider;
use Alchemy\Phrasea\Authentication\ACLProvider;
use Alchemy\Phrasea\Security\Firewall;
use Silex\Application as SilexApplication;
use Silex\ServiceProviderInterface;
@@ -37,6 +38,10 @@ class PhraseanetServiceProvider implements ServiceProviderInterface
return $events;
});
$app['acl'] = $app->share(function(SilexApplication $app) {
return new ACLProvider($app);
});
}
public function boot(SilexApplication $app)

View File

@@ -287,11 +287,6 @@ class User
**/
private $notificationSettings;
/**
* @var \ACL
*/
private $acl;
/**
* @var ArrayCollection
*/
@@ -1010,20 +1005,6 @@ class User
return $this;
}
/**
* @param Application $app
*
* @return \ACL
*/
public function ACL(Application $app)
{
if (!$this->acl instanceof \ACL) {
$this->acl = new \ACL($this, $app);
}
return $this->acl;
}
/**
* @return boolean
*/

View File

@@ -0,0 +1,16 @@
<?php
namespace Alchemy\Tests\Phrasea\Authentication;
use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Authentication\ACLProvider;
class ACLProviderTest extends \PhraseanetPHPUnitAbstract
{
public function testGetACL()
{
$acl = self::$DI['app']['acl']->get(self::$DI['user']);
$this->assertInstanceOf('\ACL', $acl);
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace Alchemy\Tests\Phrasea\Core\Provider;
/**
* @covers Alchemy\Phrasea\Core\Provider\FeedServiceProvider
*/
class PhraseanetServiceProviderTest extends ServiceProviderTestCase
{
public function provideServiceDescription()
{
return array(
array(
'Alchemy\Phrasea\Core\Provider\PhraseanetServiceProvider',
'phraseanet.appbox',
'\appbox'
),
array(
'Alchemy\Phrasea\Core\Provider\PhraseanetServiceProvider',
'phraseanet.registry',
'\registry'
),
array(
'Alchemy\Phrasea\Core\Provider\PhraseanetServiceProvider',
'firewall',
'Alchemy\Phrasea\Security\Firewall'
),
array(
'Alchemy\Phrasea\Core\Provider\PhraseanetServiceProvider',
'events-manager',
'\eventsmanager_broker'
),
array(
'Alchemy\Phrasea\Core\Provider\PhraseanetServiceProvider',
'acl',
'Alchemy\Phrasea\Authentication\ACLProvider'
)
);
}
}