diff --git a/lib/Alchemy/Phrasea/Authentication/ACLProvider.php b/lib/Alchemy/Phrasea/Authentication/ACLProvider.php new file mode 100644 index 0000000000..e3aa66ebef --- /dev/null +++ b/lib/Alchemy/Phrasea/Authentication/ACLProvider.php @@ -0,0 +1,92 @@ +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); + } +} diff --git a/lib/Alchemy/Phrasea/Core/Provider/PhraseanetServiceProvider.php b/lib/Alchemy/Phrasea/Core/Provider/PhraseanetServiceProvider.php index 9ac3896522..9f607969eb 100644 --- a/lib/Alchemy/Phrasea/Core/Provider/PhraseanetServiceProvider.php +++ b/lib/Alchemy/Phrasea/Core/Provider/PhraseanetServiceProvider.php @@ -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) diff --git a/lib/Alchemy/Phrasea/Model/Entities/User.php b/lib/Alchemy/Phrasea/Model/Entities/User.php index 42d724c54f..322e22908e 100644 --- a/lib/Alchemy/Phrasea/Model/Entities/User.php +++ b/lib/Alchemy/Phrasea/Model/Entities/User.php @@ -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 */ diff --git a/tests/Alchemy/Tests/Phrasea/Authentication/ACLProviderTest.php b/tests/Alchemy/Tests/Phrasea/Authentication/ACLProviderTest.php new file mode 100644 index 0000000000..2c2fdc4310 --- /dev/null +++ b/tests/Alchemy/Tests/Phrasea/Authentication/ACLProviderTest.php @@ -0,0 +1,16 @@ +get(self::$DI['user']); + + $this->assertInstanceOf('\ACL', $acl); + } +} diff --git a/tests/Alchemy/Tests/Phrasea/Core/Provider/PhraseanetServiceProviderTest.php b/tests/Alchemy/Tests/Phrasea/Core/Provider/PhraseanetServiceProviderTest.php new file mode 100644 index 0000000000..b88a0b1813 --- /dev/null +++ b/tests/Alchemy/Tests/Phrasea/Core/Provider/PhraseanetServiceProviderTest.php @@ -0,0 +1,40 @@ +