I did not want users to be able to choose their Group, and instead will be managed internally by the system. For registration, it was not much much to be done with FOS. All I needed was to override the User entity with new set of doctrine key constraints. I added the following in the User entity that extended the BaseUser:
/**
* User.
*
* @ORM\Table(name="fos_user",
* uniqueConstraints={
* @UniqueConstraint(name="group_username_unique", columns={"group_id", "username_canonical"}),
* @UniqueConstraint(name="group_email_unique", columns={"group_id", "email_canonical"})
* }
* )
* @UniqueEntity(fields={"group", "usernameCanonical"}, errorPath="username", message="fos_user.username.already_used")
* @UniqueEntity(fields={"group", "emailCanonical"}, errorPath="email", message="fos_user.email.already_used")
* @ORM\Entity
* @ORM\AttributeOverrides({
* @ORM\AttributeOverride(name="emailCanonical", column=@ORM\Column(type="string", length=180, nullable=false, unique=false)),
* @ORM\AttributeOverride(name="usernameCanonical",column=@ORM\Column(type="string", length=180, nullable=false, unique=false))
* })
*/
class User extends BaseUser
{
/* my entity contents here */
}
While for login, it was a little bit much of work. I had to override the UserProvider, UserManager, create CustomUserManager and add a set of new method with Group constraints:
public function findUserByUsernameOrEmailAndGroup($usernameOrEmail, $group)
{
if (preg_match('/^.+\@\S+\.\S+$/', $usernameOrEmail)) {
return $this->findUserByEmailAndGroup($usernameOrEmail, $group);
}
return $this->findUserByUsernameAndGroup($usernameOrEmail, $group);
}
public function findUserByEmailAndGroup($email, $group)
{
return $this->findUserBy(['group' => $group, 'emailCanonical' => $email]);
}
public function findUserByUsernameAndGroup($username, $group)
{
return $this->findUserBy(['group' => $group, 'usernameCanonical' => $username]);
}
public function findUserByConfirmationTokenAndGroup($token, $group)
{
return $this->findUserBy(['confirmationToken' => $token, 'group' => $group]);
}
Further, I made the overrides on RegistrationController and ResettingController to adopt to the new changes for login and password reset functionalities.