我在我的Symfony2项目中使用FosUserBundle,我有登录工作,我现在想进一步自定义登录,包括检查id以及用户名,电子邮件和密码。我在Entity
文件夹中有一个User实体,我在Repository
文件夹中有一个UserRepository
-thankyou
用户实体:
<?php
namespace Example\Bundle\ExampleBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* User
*
* @ORM\Table(name="Example_user")
* @ORM\Entity(repositoryClass="Example\Bundle\ExampleBundle\Repository\UserRepository")
*/
class User extends BaseUser {
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/*
* some Class properties here
*
*/
/**
* Get id
*
* @return integer
*/
public function getId() {
return $this->id;
}
/*
* some setters and getters here
*/
}
用户存储库
<?php
namespace Example\Bundle\ExampleBundle\Repository;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Example\Bundle\ExampleBundle\siteConfig;
class UserRepository extends EntityRepository implements UserProviderInterface{
public $university = siteConfig::university_id;
/**
*
* @param type $username
*/
public function FindUsernameOrEmailInUniversity($username, $universityId) {
return $this->createQueryBuilder('user')
->where('user.university_id = :universityId')
->andWhere('user.username = :username OR user.email = :email')
->setParameter('universityId', $universityId)
->setParameter('username', $username)
->setParameter('email', $username)
->getQuery()
->getOneOrNullResult();
}
/**
*
* @param type $username
*/
public function loadUserByUsername($username) {
$user = $this->FindUsernameOrEmailInUniversity($username, $this->university); //check order of parameters use type hinting
if(!$user){
throw new \Symfony\Component\Security\Core\Exception\UsernameNotFoundException('User Name '.$username.' Not Found');
}
return $user;
}
/**
*
* @param \Symfony\Component\Security\Core\User\UserInterface $user
*/
public function refreshUser(\Symfony\Component\Security\Core\User\UserInterface $user) {
return $user;
}
/**
*
* @param type $class
*/
public function supportsClass($class) {
return ;
}
}
Security.yml
# app/config/security.yml
security:
encoders:
FOS\UserBundle\Model\UserInterface: bcrypt
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
providers:
fos_userbundle:
entity: { class: Example\Bundle\ExampleBundle\Repository\UserRepository }
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_provider: security.csrf.token_manager # Use form.csrf_provider instead for Symfony <2.4
logout: true
anonymous: true
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/, role: ROLE_ADMIN }
以下是错误
The class 'Example\Bundle\ExampleBundle\Repository\UserRepository' was not found in the chain configured namespaces Example\Bundle\ExampleBundle\Entity, FOS\UserBundle\Entity
答案 0 :(得分:1)
在扩展FOS\UserBundle\Doctrine\UserManager
的实体文件夹中创建一个类(我假设有一个名为siteConfig.php的类,其中包含由db查询更新的静态University_id字段)
<?php
namespace Example\Bundle\ExampleBundle\Entity
use FOS\UserBundle\Doctrine\UserManager as BaseUserManager;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
use FOS\UserBundle\Util\CanonicalizerInterface;
use FOS\UserBundle\Model\UserInterface;
use Example\Bundle\ExampleBundle\siteConfig;
class UserManager extends BaseUserManager {
public function __construct(EncoderFactoryInterface $encoderFactory, CanonicalizerInterface $usernameCanonicalizer,
CanonicalizerInterface $emailCanonicalizer, EntityManager $em, $class) {
parent::__construct($encoderFactory, $usernameCanonicalizer, $emailCanonicalizer, $em, $class);
}
/**
* this overides the findUserByUsernameOrEmail in FOS\UserBundle\Doctrine\UserManager
**/
public function findUserByUsernameOrEmail($usernameOrEmail) {
if (filter_var($usernameOrEmail, FILTER_VALIDATE_EMAIL)) {
return $this->findUserBy(array('emailCanonical' => $this->canonicalizeEmail($usernameOrEmail), 'university' => siteConfig::$university_id));
}
return $this->findUserBy(array('usernameCanonical' => $this->canonicalizeUsername($usernameOrEmail), 'university' => siteConfig::$university_id));
}
}
用户管理器的依赖注入,在services.yml内部
ExampleUserManager:
class: namespace Example\Bundle\ExampleBundle\Entity\UserManager
arguments: [@security.encoder_factory, @fos_user.util.username_canonicalizer, @fos_user.util.email_canonicalizer, @fos_user.entity_manager, namespace Example\Bundle\ExampleBundle\Entity\User]
在config.yml内部,在fos_user
下添加以下配置service:
user_manager: ExampleUserManager
在提供商下的security.yml内添加以下内容
providers:
fos_userbundle:
id: fos_user.user_provider.username_email
答案 1 :(得分:0)
首先是错误类型 - 在为实体行定义存储库类时需要额外的paranthese:
/**
* User
*
* @ORM\Table(name="Example_user")
* @ORM\Entity(repositoryClass="Example\Bundle\ExampleBundle\Repository\UserRepository")
*/
class User extends BaseUser {
试试这个,我对你的方法做了一些修改:
/**
* @param type $username
*/
public function FindUsernameOrEmailInUniversity($username, University $university) {
return $this->createQueryBuilder('user') //It should be alias for your entity name, and it could be anything you write, like 'user' or just 'u'
->where('user.university = :university')
->andWhere('user.username = :username OR user.email = :email')
->setParameter('university', $university)
->setParameter('username', $username)
->setParameter('email', $username)
->getQuery()
->getOneOrNullResult();
}
你也有大学,电子邮件和用户名的getter,setter和属性吗?我假设您希望将大学作为与用户的关系,因此您需要设置正确的学说关系。