Skip to content
This repository was archived by the owner on May 31, 2024. It is now read-only.

Commit 1bd6c22

Browse files
committed
[Security][Acl] enforce string identifiers
1 parent b3d0326 commit 1bd6c22

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

Acl/Domain/ObjectIdentity.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ final class ObjectIdentity implements ObjectIdentityInterface
3636
*/
3737
public function __construct($identifier, $type)
3838
{
39-
if (empty($identifier)) {
39+
if ('' === $identifier) {
4040
throw new \InvalidArgumentException('$identifier cannot be empty.');
4141
}
4242
if (empty($type)) {
@@ -66,7 +66,7 @@ public static function fromDomainObject($domainObject)
6666
if ($domainObject instanceof DomainObjectInterface) {
6767
return new self($domainObject->getObjectIdentifier(), ClassUtils::getRealClass($domainObject));
6868
} elseif (method_exists($domainObject, 'getId')) {
69-
return new self($domainObject->getId(), ClassUtils::getRealClass($domainObject));
69+
return new self((string) $domainObject->getId(), ClassUtils::getRealClass($domainObject));
7070
}
7171
} catch (\InvalidArgumentException $invalid) {
7272
throw new InvalidDomainObjectException($invalid->getMessage(), 0, $invalid);

Tests/Acl/Domain/ObjectIdentityTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,26 @@ public function testFromDomainObjectWithProxy()
6464
$this->assertEquals('Symfony\Component\Security\Tests\Acl\Domain\TestDomainObject', $id->getType());
6565
}
6666

67+
public function testFromDomainObjectWithoutInterfaceEnforcesStringIdentifier()
68+
{
69+
$domainObject = new TestDomainObject();
70+
$domainObject->id = 1;
71+
$id = ObjectIdentity::fromDomainObject($domainObject);
72+
73+
$this->assertSame('1', $id->getIdentifier());
74+
$this->assertEquals('Symfony\Component\Security\Tests\Acl\Domain\TestDomainObject', $id->getType());
75+
}
76+
77+
public function testFromDomainObjectWithoutInterfaceAllowsZeroAsIdentifier()
78+
{
79+
$domainObject = new TestDomainObject();
80+
$domainObject->id = '0';
81+
$id = ObjectIdentity::fromDomainObject($domainObject);
82+
83+
$this->assertSame('0', $id->getIdentifier());
84+
$this->assertEquals('Symfony\Component\Security\Tests\Acl\Domain\TestDomainObject', $id->getType());
85+
}
86+
6787
/**
6888
* @dataProvider getCompareData
6989
*/
@@ -89,14 +109,16 @@ public function getCompareData()
89109

90110
class TestDomainObject
91111
{
112+
public $id = 'getId()';
113+
92114
public function getObjectIdentifier()
93115
{
94116
return 'getObjectIdentifier()';
95117
}
96118

97119
public function getId()
98120
{
99-
return 'getId()';
121+
return $this->id;
100122
}
101123
}
102124
}

0 commit comments

Comments
 (0)