Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added XmlEntityUserProvider

  • Loading branch information...
commit bbfef678686a85bcdb62680303209e8c95e0afee 1 parent 6150a68
@golovanov golovanov authored
Showing with 81 additions and 0 deletions.
  1. +8 −0 Resources/config/oxm.xml
  2. +73 −0 Security/XmlEntityUserProvider.php
View
8 Resources/config/oxm.xml
@@ -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>
@@ -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>
View
73 Security/XmlEntityUserProvider.php
@@ -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;
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.