[Security][ACL] Fixed ObjectIdentity::fromDomainObject and UserSecurityIdentity::from(Account|Token) when working with proxies

Backported ClassUtils class from Doctrine Common 2.2
Fixes #2611, #2056, #2048, #2035
This commit is contained in:
Jordan Alliot 2012-04-07 22:09:48 +02:00
parent b47cb35e7b
commit 6483d88f69
6 changed files with 200 additions and 68 deletions

View File

@ -11,6 +11,7 @@
namespace Symfony\Component\Security\Acl\Domain;
use Symfony\Component\Security\Core\Util\ClassUtils;
use Symfony\Component\Security\Acl\Exception\InvalidDomainObjectException;
use Symfony\Component\Security\Acl\Model\DomainObjectInterface;
use Symfony\Component\Security\Acl\Model\ObjectIdentityInterface;
@ -59,9 +60,9 @@ final class ObjectIdentity implements ObjectIdentityInterface
try {
if ($domainObject instanceof DomainObjectInterface) {
return new self($domainObject->getObjectIdentifier(), get_class($domainObject));
return new self($domainObject->getObjectIdentifier(), ClassUtils::getRealClass($domainObject));
} elseif (method_exists($domainObject, 'getId')) {
return new self($domainObject->getId(), get_class($domainObject));
return new self($domainObject->getId(), ClassUtils::getRealClass($domainObject));
}
} catch (\InvalidArgumentException $invalid) {
throw new InvalidDomainObjectException($invalid->getMessage(), 0, $invalid);

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Security\Acl\Domain;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Util\ClassUtils;
use Symfony\Component\Security\Acl\Model\SecurityIdentityInterface;
/**
@ -52,7 +53,7 @@ final class UserSecurityIdentity implements SecurityIdentityInterface
*/
static public function fromAccount(UserInterface $user)
{
return new self($user->getUsername(), get_class($user));
return new self($user->getUsername(), ClassUtils::getRealClass($user));
}
/**
@ -69,7 +70,7 @@ final class UserSecurityIdentity implements SecurityIdentityInterface
return self::fromAccount($user);
}
return new self((string) $user, is_object($user)? get_class($user) : get_class($token));
return new self((string) $user, is_object($user) ? ClassUtils::getRealClass($user) : ClassUtils::getRealClass($token));
}
/**

View File

@ -0,0 +1,55 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Security\Core\Util;
/**
* Class related functionality for objects that
* might or might not be proxy objects at the moment.
*
* @see Doctrine\Common\Util\ClassUtils
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
* @author Johannes Schmitt <schmittjoh@gmail.com>
*/
class ClassUtils
{
/**
* Marker for Proxy class names.
*
* @var string
*/
const MARKER = '__CG__';
/**
* Length of the proxy marker
*
* @var int
*/
const MARKER_LENGTH = 6;
/**
* Gets the real class name of a class name that could be a proxy.
*
* @param string|object
* @return string
*/
public static function getRealClass($object)
{
$class = is_object($object) ? get_class($object) : $object;
if (false === $pos = strrpos($class, '\\'.self::MARKER.'\\')) {
return $class;
}
return substr($class, $pos + self::MARKER_LENGTH + 2);
}
}

View File

@ -9,12 +9,12 @@
* file that was distributed with this source code.
*/
namespace Symfony\Component\Security\Tests\Acl\Domain;
use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
class ObjectIdentityTest extends \PHPUnit_Framework_TestCase
namespace Symfony\Component\Security\Tests\Acl\Domain
{
use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
class ObjectIdentityTest extends \PHPUnit_Framework_TestCase
{
public function testConstructor()
{
$id = new ObjectIdentity('fooid', 'footype');
@ -23,6 +23,15 @@ class ObjectIdentityTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('footype', $id->getType());
}
// Test that constructor never changes passed type, even with proxies
public function testConstructorWithProxy()
{
$id = new ObjectIdentity('fooid', 'Acme\DemoBundle\Proxy\__CG__\Symfony\Component\Security\Tests\Acl\Domain\TestDomainObject');
$this->assertEquals('fooid', $id->getIdentifier());
$this->assertEquals('Acme\DemoBundle\Proxy\__CG__\Symfony\Component\Security\Tests\Acl\Domain\TestDomainObject', $id->getType());
}
public function testFromDomainObjectPrefersInterfaceOverGetId()
{
$domainObject = $this->getMock('Symfony\Component\Security\Acl\Model\DomainObjectInterface');
@ -45,6 +54,14 @@ class ObjectIdentityTest extends \PHPUnit_Framework_TestCase
{
$id = ObjectIdentity::fromDomainObject(new TestDomainObject());
$this->assertEquals('getId()', $id->getIdentifier());
$this->assertEquals('Symfony\Component\Security\Tests\Acl\Domain\TestDomainObject', $id->getType());
}
public function testFromDomainObjectWithProxy()
{
$id = ObjectIdentity::fromDomainObject(new \Acme\DemoBundle\Proxy\__CG__\Symfony\Component\Security\Tests\Acl\Domain\TestDomainObject());
$this->assertEquals('getId()', $id->getIdentifier());
$this->assertEquals('Symfony\Component\Security\Tests\Acl\Domain\TestDomainObject', $id->getType());
}
/**
@ -75,10 +92,10 @@ class ObjectIdentityTest extends \PHPUnit_Framework_TestCase
$this->markTestSkipped('The Doctrine2 DBAL is required for this test');
}
}
}
}
class TestDomainObject
{
class TestDomainObject
{
public function getObjectIdentifier()
{
return 'getObjectIdentifier()';
@ -88,4 +105,12 @@ class TestDomainObject
{
return 'getId()';
}
}
}
namespace Acme\DemoBundle\Proxy\__CG__\Symfony\Component\Security\Tests\Acl\Domain
{
class TestDomainObject extends \Symfony\Component\Security\Tests\Acl\Domain\TestDomainObject
{
}
}

View File

@ -24,6 +24,15 @@ class UserSecurityIdentityTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('Foo', $id->getClass());
}
// Test that constructor never changes the type, even for proxies
public function testContructorWithProxy()
{
$id = new UserSecurityIdentity('foo', 'Acme\DemoBundle\Proxy\__CG__\Symfony\Component\Security\Tests\Acl\Domain\Foo');
$this->assertEquals('foo', $id->getUsername());
$this->assertEquals('Acme\DemoBundle\Proxy\__CG__\Symfony\Component\Security\Tests\Acl\Domain\Foo', $id->getClass());
}
/**
* @dataProvider getCompareData
*/

View File

@ -0,0 +1,41 @@
<?php
namespace Symfony\Component\Security\Tests\Core\Util
{
use Symfony\Component\Security\Core\Util\ClassUtils;
class ClassUtilsTest extends \PHPUnit_Framework_TestCase
{
static public function dataGetClass()
{
return array(
array('stdClass', 'stdClass'),
array('Symfony\Component\Security\Core\Util\ClassUtils', 'Symfony\Component\Security\Core\Util\ClassUtils'),
array('MyProject\Proxies\__CG__\stdClass', 'stdClass'),
array('MyProject\Proxies\__CG__\OtherProject\Proxies\__CG__\stdClass', 'stdClass'),
array('MyProject\Proxies\__CG__\Symfony\Component\Security\Tests\Core\Util\ChildObject', 'Symfony\Component\Security\Tests\Core\Util\ChildObject'),
array(new TestObject(), 'Symfony\Component\Security\Tests\Core\Util\TestObject'),
array(new \Acme\DemoBundle\Proxy\__CG__\Symfony\Component\Security\Tests\Core\Util\TestObject(), 'Symfony\Component\Security\Tests\Core\Util\TestObject'),
);
}
/**
* @dataProvider dataGetClass
*/
public function testGetRealClass($object, $expectedClassName)
{
$this->assertEquals($expectedClassName, ClassUtils::getRealClass($object));
}
}
class TestObject
{
}
}
namespace Acme\DemoBundle\Proxy\__CG__\Symfony\Component\Security\Tests\Core\Util
{
class TestObject extends \Symfony\Component\Security\Tests\Core\Util\TestObject
{
}
}