[英]Symfony2 Authenticating Someone with a Custom Entity Provider ERROR
I faced a problem when trying to implement the Custom Entity Provider for user authentication. 尝试实现用于用户身份验证的自定义实体提供程序时,我遇到了一个问题。 I got the Mapping Exception that my entity is not a valid or mapped super class.
我得到了映射异常,即我的实体不是有效的或映射的超类。 Here is my Actual Error: ------------- Exception/Error Start --------------
这是我的实际错误: -------------异常/错误开始--------------
1/1 MappingException: Class MunichInnovationGroup\PatentBundle\Entity\UmUsers is not a valid entity or mapped super class.
in C:\wamp\www\idp\vendor\doctrine\lib\Doctrine\ORM\Mapping\MappingException.php line 142
at MappingException::classIsNotAValidEntityOrMappedSuperClass() in C:\wamp\www\idp\vendor\doctrine\lib\Doctrine\ORM\Mapping\Driver\DriverChain.php line 80
at DriverChain->loadMetadataForClass() in C:\wamp\www\idp\vendor\doctrine\lib\Doctrine\ORM\Mapping\ClassMetadataFactory.php line 281
at ClassMetadataFactory->loadMetadata() in C:\wamp\www\idp\vendor\doctrine\lib\Doctrine\ORM\Mapping\ClassMetadataFactory.php line 170
at ClassMetadataFactory->getMetadataFor() in C:\wamp\www\idp\vendor\doctrine\lib\Doctrine\ORM\EntityManager.php line 257
at EntityManager->getClassMetadata() in C:\wamp\www\idp\vendor\doctrine\lib\Doctrine\ORM\EntityManager.php line 573
at EntityManager->getRepository() in C:\wamp\www\idp\vendor\symfony\src\Symfony\Bridge\Doctrine\Security\User\EntityUserProvider.php line 42
at EntityUserProvider->__construct() in C:\wamp\www\idp\app\cache\dev\appDevDebugProjectContainer.php line 2191
at appDevDebugProjectContainer->getSecurity_User_Provider_Concrete_UserDbService() in C:\wamp\www\idp\app\bootstrap.php.cache line 190
at Container->get() in C:\wamp\www\idp\app\cache\dev\appDevDebugProjectContainer.php line 2174
at appDevDebugProjectContainer->getSecurity_User_Provider_Concrete_ChainProviderService() in C:\wamp\www\idp\app\bootstrap.php.cache line 190
at Container->get() in C:\wamp\www\idp\app\cache\dev\appDevDebugProjectContainer.php line 2140
at appDevDebugProjectContainer->getSecurity_Authentication_ManagerService() in C:\wamp\www\idp\app\bootstrap.php.cache line 190
at Container->get() in C:\wamp\www\idp\app\cache\dev\appDevDebugProjectContainer.php line 1304
at appDevDebugProjectContainer->getSecurity_ContextService() in C:\wamp\www\idp\app\bootstrap.php.cache line 190
at Container->get() in C:\wamp\www\idp\app\cache\dev\appDevDebugProjectContainer.php line 1178
at appDevDebugProjectContainer->getProfilerService() in C:\wamp\www\idp\app\bootstrap.php.cache line 190
at Container->get() in C:\wamp\www\idp\app\cache\dev\appDevDebugProjectContainer.php line 1195
at appDevDebugProjectContainer->getProfilerListenerService() in C:\wamp\www\idp\app\bootstrap.php.cache line 190
at Container->get() in C:\wamp\www\idp\app\cache\dev\classes.php line 4779
at ContainerAwareEventDispatcher->lazyLoad() in C:\wamp\www\idp\app\cache\dev\classes.php line 4768
at ContainerAwareEventDispatcher->dispatch() in C:\wamp\www\idp\app\cache\dev\classes.php line 3916
at HttpKernel->handleException() in C:\wamp\www\idp\app\cache\dev\classes.php line 3855
at HttpKernel->handle() in C:\wamp\www\idp\app\cache\dev\classes.php line 4828
at HttpKernel->handle() in C:\wamp\www\idp\app\bootstrap.php.cache line 547
at Kernel->handle() in C:\wamp\www\idp\web\app_dev.php line 20
**------------- Exception/Error End --------------**
Lets describe my application 让我们描述一下我的应用
Here is my files 这是我的档案
------------- config.yml Start -------------- ------------- config.yml开始--------------
**Doctrine Configuration**
doctrine:
dbal:
default_connection: default
connections:
default:
driver: "%db_driver_DB-A%"
host: "%db_host_DB-A%"
port: "%db_port_DB-A%"
dbname: "%db_name_DB-A%"
user: "%db_user_DB-A%"
password: "%db_password_DB-A%"
charset: UTF8
DB_B:
driver: "%db_driver_DB-B%"
host: "%db_host_DB-B%"
port: "%db_port_DB-B%"
dbname: "%db_name_DB-B%"
user: "%db_user_DB-B%"
password: "%db_password_DB-B%"
charset: UTF8
DB_C:
driver: "%db_driver_DB-C%"
host: "%db_host_DB-C%"
port: "%db_port_DB-C%"
dbname: "%db_name_DB-C%"
user: "%db_user_DB-C%"
password: "%db_password_DB-C%"
charset: UTF8
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
MIGBundle: ~
DB_B:
connection: DB_B
mappings:
MIGPatBundle: ~
DB_C:
connection: DB_C
mappings:
MIGPatBundle: ~
------------- config.yml End -------------- ------------- config.yml结束--------------
------------- security.yml Start ------------ ------------- security.yml开始------------
security:
encoders:
MIG\PatBundle\Entity\UmUsers:
algorithm: sha512
encode-as-base64: true
iterations: 10
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
chain_provider:
providers: [user_db]
user_db:
entity: { class: MIG\PatBundle\Entity\UmUsers }
firewalls:
secured_area:
pattern: ^/
anonymous: ~
logout:
target: /
form_login:
provider: chain_provider
check_path: /security/login_check
default_target_path: /
login_path: /security/login
access_control:
- { path: ^/account, roles: ROLE_USER }
- { path: ^/management, roles: ROLE_ADMIN }
- { path: ^/portfolio, roles: ROLE_USER }
- { path: ^/portfoliogroup, roles: ROLE_USER }
- { path: ^/security, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/userpatent, roles: ROLE_USER }
- { path: ^/_internal, roles: IS_AUTHENTICATED_ANONYMOUSLY, ip: 127.0.0.1 }
access_denied_url: /security/forbidden
------------- security.yml End -------------- ------------- security.yml结束--------------
------------- User Entity Start -------------- -------------用户实体开始--------------
<?php
namespace MIG\PatBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Security\Core\User\UserInterface;
/**
*MIG\PatBundle\Entity\UmUsers
*
*@ORM\Entity(repositoryClass="MIG\PatBundle\Entity\UserRepository")
*@ORM\Table(name = "um_users")
*/
class UmUsers implements UserInterface
{
/**
@var string $id
@ORM\Column(name="id", type="string", length=36, nullable=false)
@ORM\Id
@ORM\GeneratedValue(strategy="UUID")
*/
private $id;
/**
* @ORM\Column(name="user_name", type="string", length=50, nullable=true)
*
* @var string $userName
*/
private $userName;
/**
* @var string $email
*
* @ORM\Column(name="email", type="string", length=100, nullable=false)
*/
private $email;
/**
* @ORM\Column(name="password", type="string", length=100, nullable=false)
*
* @Assert\MinLength(
* limit = 6,
* message = "The password must contain at least {{ limit }} characters."
* )
* @var string $password
*/
private $password;
/**
* @var string $salt
*
* @ORM\Column(name="salt", type="string", length=255, nullable=false)
*/
private $salt;
/**
* @var string $activationCode
*
* @ORM\Column(name="activation_code", type="string", length=255, nullable=false)
*/
private $activationCode;
/**
* @var boolean $activated
*
* @ORM\Column(name="activated", type="boolean", nullable=true)
*/
private $activated;
/**
* @var UmRoles
*
* @ORM\ManyToMany(targetEntity="UmRoles", inversedBy="user")
* @ORM\JoinTable(name="um_usersinroles",
* joinColumns={
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="role_id", referencedColumnName="id")
* }
* )
*/
private $role;
public function __construct()
{
$this->role = new \Doctrine\Common\Collections\ArrayCollection();
$this->createdAt = new \DateTime();
$this->lastLoginAt = new \DateTime();
$this->lastUpdatedDate = new \DateTime();
$this->lastLoginDate = new \DateTime();
$this->lastPasswordChangedDate = new \DateTime();
}
/**
* Get id
*
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* Set userName
*
* @param string $userName
*/
public function setUserName($userName)
{
$this->userName = $userName;
}
/**
* Get userName
*
* @return string
*/
public function getUserName()
{
return $this->userName;
}
/**
* Set email
*
* @param string $email
*/
public function setEmail($email)
{
$this->email = $email;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set password
*
* @param string $password
*/
public function setPassword($password)
{
$this->password = $password;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set salt
*
* @param string $salt
*/
public function setSalt($salt)
{
$this->salt = $salt;
}
/**
* Get salt
*
* @return string
*/
public function getSalt()
{
return $this->salt;
}
/**
* Set activationCode
*
* @param string $activationCode
*/
public function setActivationCode($activationCode)
{
$this->activationCode = $activationCode;
}
/**
* Get activationCode
*
* @return string
*/
public function getActivationCode()
{
return $this->activationCode;
}
/**
* Set activated
*
* @param boolean $activated
*/
public function setActivated($activated)
{
$this->activated = $activated;
}
/**
* Get activated
*
* @return boolean
*/
public function getActivated()
{
return $this->activated;
}
/**
* Add role
*
* @param MIG\PatBundle\Entity\UmRoles $role
*/
public function addUmRoles(\MIG\PatBundle\Entity\UmRoles $role)
{
$this->role[] = $role;
}
/**
* Get role
*
* @return Doctrine\Common\Collections\Collection
*/
public function getRole()
{
return $this->role;
}
/**
* Gets an array of roles.
*
* @return array An array of Role objects
*/
public function getRoles()
{
return $this->getRole()->toArray();
}
/**
* Erases the user credentials.
*/
public function eraseCredentials()
{
}
/**
* Compares this user to another to determine if they are the same.
*
* @param UserInterface $user The user
* @return booleanean True if equal, false othwerwise.
*/
public function equals(UserInterface $user)
{
return md5($this->getUsername()) == md5($user->getUsername());
}
} }
------------- User Entity End -------------- -------------用户实体端--------------
------------- UserRepository Entity Start-------------- ------------- UserRepository实体开始--------------
namespace MIG\PatBundle\Entity;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NoResultException;
class UserRepository extends EntityRepository implements UserProviderInterface
{
public function loadUserByUsername($username)
{
$q = $this
->createQueryBuilder('u')
->where('u.userName = :username OR u.email = :email')
->setParameter('username', $username)
->setParameter('email', $username)
->getQuery()
;
try {
// The Query::getSingleResult() method throws an exception
// if there is no record matching the criteria.
$user = $q->getSingleResult();
} catch (NoResultException $e) {
throw new UsernameNotFoundException(sprintf('Unable to find an active admin MunichInnovationGroup\PatentBundle\Entity\UmUsers object identified by "%s".', $username), null, 0, $e);
}
return $user;
}
public function refreshUser(UserInterface $user)
{
$class = get_class($user);
if (!$this->supportsClass($class)) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', $class));
}
return $this->loadUserByUsername($user->getUsername());
}
public function supportsClass($class)
{
return $this->getEntityName() === $class || is_subclass_of($class, $this->getEntityName());
}
}
------------- UserRepository Entity End-------------- ------------- UserRepository实体结束--------------
Actually i have a deadline to complete this task. 实际上,我有完成此任务的期限。 I will be very much glade to you if someone can give me the solution.
如果有人可以给我解决方案,我将非常高兴。
Regards, Zaif 问候,Zaif
If you have a look at vendor/symfony/src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml, you'll see this: 如果查看vendor / symfony / src / Symfony / Bundle / SecurityBundle / Resources / config / security.xml,您将看到以下内容:
<service id="security.user.provider.entity" class="%security.user.provider.entity.class%" abstract="true" public="false" >
<argument type="service" id="security.user.entity_manager" />
</service>
<service id="security.user.entity_manager" alias="doctrine.orm.entity_manager" public="false" />
I think you need to override the security.user.entity_manager
, but I am not sure how (maybe adding some lines in security.yml will do the trick), and since it is not public, I am not sure whether you can. 我认为您需要重写
security.user.entity_manager
,但是我不确定如何操作(也许在security.yml中添加一些行就可以了),并且由于它不是公共的,所以不确定是否可以。
If this does not work, maybe you could report an issue about this. 如果这不起作用,也许您可以报告有关此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.