多个用户类

时间:2015-06-22 14:06:19

标签: symfony

我在系统中有4种不同的用户类型(在Symfony 2之上)。每种类型都有一些特定的字段和行为,但它们都有一个共同的基础。因此,为扩展同一个超类的每个用户实现单个类似乎是个好主意。

如何实现?我在这个主题上找到的只是一些RollerworksMultiUserBundle。

1 个答案:

答案 0 :(得分:1)

在ORM级别和OOP继承中使用表继承。如果表现很关键(没有加入),请转到Single Table Inheritance;如果您是纯粹主义者,请转到Class Table Inheritance

E.g。

通用基类:

use Symfony\Component\Security\Core\User\AdvancedUserInterface;

/**
  * @ORM\Entity(repositoryClass="Some\Bundle\Repository\UserRepository")
  * @ORM\InheritanceType("SINGLE_TABLE")
  * @ORM\DiscriminatorColumn(name="userType", type="string")
  * @ORM\DiscriminatorMap({
  *         "userType1" = "UserType1",
  *         "userType2" = "UserType2",
  *         "userType3" = "UserType3",
  *         "userType4" = "UserType4"
  * })
  */
abstract class User implements AdvancedUserInterface
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=250, unique=true)
     */
    protected $email;

    /**
     * @ORM\Column(type="string", length=128, nullable=true)
     */
    protected $password;

    // other fields

    public function getSalt()
    {
       return "some salt number";
    }

    public function getUsername()
    {
       return $this->email;
    }

    public function getPassword()
    {
       return $this->password;
    }

    public function getRoles()
    {
       return array('ROLE_USER');
    }

    public function eraseCredentials() {}

    public function isCredentialsNonExpired() 
    {
       return true;
    }

    public function isAccountNonLocked() 
    {
       return true;
    }

    public function isAccountNonExpired() 
    {
        return true;
    }

    public function isEnabled()
    {
       return true;
    }

    public function equals(UserInterface $user)
    {
         return $user->getUsername() === $this->getUsername() || $user->getEmail() === $this->getEmail();
    }
}

子类很简单(仅限于UserType1类的示例):

/**
 * @ORM\Entity
 */
class UserType1 extends User 
{
    // fields of UserType1 class go here

    public function getRoles()
    {
       return array('ROLE_USER_TYPE_1', 'ROLE_USER');
    }
}

其余部分与示例非常相似。在security.yml

security:
    encoders:
        Some\Bundle\Repository\User:
            algorithm: sha512
            encode_as_base64: false
            iterations: 1000

    providers:
        provider1:
            entity: { class: "SomeBundle:User" }

    role_hierarchy:
        ROLE_USER_TYPE_1:  ROLE_USER
        ROLE_USER_TYPE_2:  ROLE_USER
        ROLE_USER_TYPE_3:  ROLE_USER
        ROLE_USER_TYPE_4:  ROLE_USER

    firewalls:
        firewall1:
            pattern: ^/
            provider: provider1
            form_login:
                login_path: /login
                check_path: /auth
                post_only: true
                username_parameter: email
                password_parameter: password
                always_use_default_target_path: true
                default_target_path: /
            logout:
                path: /logout
                target: /login
            anonymous: ~

存储库类:

use Doctrine\ORM\EntityRepository;
use Symfony\Component\Security\Core\User\UserInterface;

class UserRepository extends EntityRepository implements UserProviderInterface
{
    public function loadUserByUsername($username)
    {
        $qb = $this->createQueryBuilder('u');

        $query = $qb->where('LOWER(u.email) = :email')
                    ->setParameter('email', strtolower($username))
                    ->getQuery();

        try {
            $user = $query->getSingleResult();
        }
        catch (NoResultException $e) {
            throw new UsernameNotFoundException('User not found.', null, $e);
        }
        return $user;
    }

    public function supportsClass($class)
    {
        return $this->getEntityName() === $class || 
                    is_subclass_of($class, $this->getEntityName());
    }

    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->find($user->getId());
    }
}