Skip to content

Commit

Permalink
[DoctrineBridge] Catch user-error when the identifier is not serializ…
Browse files Browse the repository at this point in the history
…ed with the User entity.
  • Loading branch information
beberlei committed Dec 1, 2011
1 parent 220d3d2 commit 3c83b89
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Symfony/Bridge/Doctrine/Security/User/EntityUserProvider.php
Expand Up @@ -75,12 +75,21 @@ public function refreshUser(UserInterface $user)
if (!$user instanceof $this->class) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
}


// The user must be reloaded via the primary key as all other data
// might have changed without proper persistence in the database.
// That's the case when the user has been changed by a form with
// validation errors.
return $this->repository->find($this->metadata->getIdentifierValues($user));
$id = $this->metadata->getIdentifierValues($user);
if (!$id) {
throw new \InvalidArgumentException("You cannot refresh a user ".
"from the EntityUserProvider that does not contain an identifier. ".
"The user object has to be serialized with its own identifier " .
"mapped by Doctrine."
);
}
return $this->repository->find($id);
}

/**
Expand Down
Expand Up @@ -40,6 +40,20 @@ public function testRefreshUserGetsUserByPrimaryKey()

$this->assertSame($user1, $provider->refreshUser($user1));
}

public function testRefreshUserRequiresId()
{
$em = $this->createTestEntityManager();

$user1 = new CompositeIdentEntity(null, null, 'user1');
$provider = new EntityUserProvider($em, 'Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity', 'name');

$this->setExpectedException(
'InvalidArgumentException',
'You cannot refresh a user from the EntityUserProvider that does not contain an identifier. The user object has to be serialized with its own identifier mapped by Doctrine'
);
$provider->refreshUser($user1);
}

private function createSchema($em)
{
Expand Down

0 comments on commit 3c83b89

Please sign in to comment.