mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-18 07:23:13 +00:00
Add ACL Service
This commit is contained in:
92
lib/Alchemy/Phrasea/Authentication/ACLProvider.php
Normal file
92
lib/Alchemy/Phrasea/Authentication/ACLProvider.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
namespace Alchemy\Phrasea\Core\Provider;
|
namespace Alchemy\Phrasea\Core\Provider;
|
||||||
|
|
||||||
|
use Alchemy\Phrasea\Authentication\ACLProvider;
|
||||||
use Alchemy\Phrasea\Security\Firewall;
|
use Alchemy\Phrasea\Security\Firewall;
|
||||||
use Silex\Application as SilexApplication;
|
use Silex\Application as SilexApplication;
|
||||||
use Silex\ServiceProviderInterface;
|
use Silex\ServiceProviderInterface;
|
||||||
@@ -37,6 +38,10 @@ class PhraseanetServiceProvider implements ServiceProviderInterface
|
|||||||
|
|
||||||
return $events;
|
return $events;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$app['acl'] = $app->share(function(SilexApplication $app) {
|
||||||
|
return new ACLProvider($app);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function boot(SilexApplication $app)
|
public function boot(SilexApplication $app)
|
||||||
|
@@ -287,11 +287,6 @@ class User
|
|||||||
**/
|
**/
|
||||||
private $notificationSettings;
|
private $notificationSettings;
|
||||||
|
|
||||||
/**
|
|
||||||
* @var \ACL
|
|
||||||
*/
|
|
||||||
private $acl;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var ArrayCollection
|
* @var ArrayCollection
|
||||||
*/
|
*/
|
||||||
@@ -1010,20 +1005,6 @@ class User
|
|||||||
return $this;
|
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
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
|
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
@@ -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'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user