Skip to content

Commit

Permalink
Added XmlEntityUserProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
igorgolovanov committed Aug 1, 2011
1 parent 6150a68 commit bbfef67
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Resources/config/oxm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
<parameter key="doctrine.oxm.yml_mapping_dirs">%doctrine.oxm.mapping_dirs%</parameter>
<parameter key="doctrine.oxm.xml_entity_dirs" type="collection"></parameter>

<!-- security/user -->
<parameter key="doctrine.oxm.security.user.provider.class">Doctrine\Bundle\OXMBundle\Security\XmlEntityUserProvider</parameter>

<!-- proxy cache warmer -->
<parameter key="doctrine.oxm.proxy_cache_warmer.class">Doctrine\Bundle\OXMBundle\CacheWarmer\ProxyCacheWarmer</parameter>

Expand Down Expand Up @@ -76,5 +79,10 @@
<argument type="service" id="service_container" />
</service>

<!-- Security -->
<service id="doctrine.oxm.security.user.provider" class="%doctrine.oxm.security.user.provider.class%" public="false" abstract="true">
<argument type="service" id="doctrine.oxm.security.user.xml_entity_manager" />
</service>
<service id="doctrine.oxm.security.user.xml_entity_manager" alias="doctrine.oxm.default_xml_entity_manager" public="false" />
</services>
</container>
73 changes: 73 additions & 0 deletions Security/XmlEntityUserProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Doctrine\Bundle\OXMBundle\Security;

use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Doctrine\Common\Persistence\ObjectManager;

/**
* @author Igor Golovanov <igor.golovanov@gmail.com>
*/
class XmlEntityUserProvider implements UserProviderInterface
{
protected $class;
protected $repository;
protected $property;

public function __construct(ObjectManager $om, $class, $property = null)
{
$this->class = $class;

if (false !== strpos($this->class, ':')) {
$this->class = $om->getClassMetadata($class)->getName();
}

$this->repository = $om->getRepository($class);
$this->property = $property;
}

/**
* {@inheritdoc}
*/
public function loadUserByUsername($username)
{
if (null !== $this->property) {
$user = $this->repository->findOneBy(array($this->property => $username));
} else {
if (!$this->repository instanceof UserProviderInterface) {
throw new \InvalidArgumentException(sprintf('The Doctrine repository "%s" must implement UserProviderInterface.', get_class($this->repository)));
}

$user = $this->repository->loadUserByUsername($username);
}

if (null === $user) {
throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
}

return $user;
}

/**
* {@inheritDoc}
*/
public function refreshUser(UserInterface $user)
{
if (!$user instanceof $this->class) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
}

return $this->loadUserByUsername($user->getUsername());
}

/**
* {@inheritDoc}
*/
public function supportsClass($class)
{
return $class === $this->class;
}
}

0 comments on commit bbfef67

Please sign in to comment.