UpVs
UpVs

Reputation: 1958

Multiple user classes

I have 4 different user types in a system (on top of Symfony 2). Each type have some specific fields and behaviour, but all of them have a common base. So it seems it would be good idea to implement single class for each user extending the same superclass.

How can it be achieved? All I have found on the topic is some RollerworksMultiUserBundle.

Upvotes: 0

Views: 2031

Answers (1)

Genti Saliu
Genti Saliu

Reputation: 2723

Using table inheritance in the ORM level and OOP inheritance. Go for Single Table Inheritance if performance is critical (no JOINs) or Class Table Inheritance if you are a purist.

E.g.

Common base class:

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();
    }
}

The children classes are straightforward (below an example for class UserType1 only):

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

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

The rest is pretty much like in the examples. In 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: ~

The repository class:

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());
    }
}

Upvotes: 2

Related Questions