Symfony 3通过手机阵列登录

Nik*_*_ua 2 php symfony symfony-security

美好的一天!我想登录我的用户,这是一项微不足道的任务.但问题是按值数组检查用户!

例如,我有实体用户和电话.用户有很多手机.所以我需要通过它拥有的所有手机登录用户.如何使用安全捆绑包的默认工具来完成?

我没有找到像我这样的问题,并阅读有关Symfony安全性的所有文档.我认为唯一要做的就是创建自定义提供程序.但我认为它不能解决我的问题.

任何想法,亲爱的symfoners?:)

Gen*_*ire 5

您必须在security.yml中设置安全提供程序

security:

    # http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
...

    providers:
        my_db_provider:
            entity:
                class: AppBundle:User
...
Run Code Online (Sandbox Code Playgroud)

然后,您的实体AppBundle:User应实现该接口,Symfony\Component\Security\Core\User\UserInterface并具有UserRepository实现该接口的Example的自定义存储库Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface.你的UserRepository课应该是这样的:

<?php

use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use AppBundle\Entity\User;
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;

class UserRepository extends EntityRepository implements UserLoaderInterface
{

 /**
  * Loads the user for the given username.
  *
  * This method must return null if the user is not found.
  *
  * @param string $username The username
  * @return null|Utilisateur
  * @throws \Exception
  */
  public function loadUserByUsername($username)
  {
    //Here you write a custom query to retrive the user base on the fields you require. 
    // Here I have used username, email and phone number
    $q = $this
        ->createQueryBuilder('u')
        ->select('u')
        ->leftJoin('u.phones', 'p')
        ->where('u.username = :username or u.email= :email or p.phoneNumber= :phone')
        ->setParameter('username', $username)
        ->setParameter('email', $username)
        ->setParameter('phone ', $username)
        ->getQuery();
    try {
        $user = $q->getSingleResult();
    } catch (NoResultException $e) {
        throw new UsernameNotFoundException(sprintf('Unable to find an active user AppBundle:User object identified by "%s".', $username), 0, $e);
    } catch (NonUniqueResultException $ex) {
        throw new \Exception("The user you provided is not unique");
    }
    return $user;
  }
}
Run Code Online (Sandbox Code Playgroud)

您的AppByndle:User实体类应如下所示:

<?php


  use Doctrine\Common\Collections\ArrayCollection;
  use Doctrine\Common\Collections\Collection;
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\Security\Core\User\UserInterface;

  /**
   * User
   *
   * @ORM\Table(name="user")
   * @ORM\Entity(repositoryClass="AppBundle\Dao\UserRepository")
 */
 class User implements UserInterface
 {
  /**
   * @var integer
   *
   * @ORM\Column(name="id", type="integer", nullable=false)
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="IDENTITY")
   */
   private $id;

   /**
    * @var string
    *
    * @ORM\Column(name="username", type="string", length=254, nullable=false, unique=true)
    */
    private $username;

    ....
    ....

  }
Run Code Online (Sandbox Code Playgroud)