rollerworks-graveyard / RollerworksMultiUserBundle

Multi user management for the FOSUserBundle - DISCONTINUED!!
MIT License
56 stars 21 forks source link

Unique user for all user-system type #83

Open djwatt3 opened 9 years ago

djwatt3 commented 9 years ago

Is there a feature in the bundle, that allow to register one unique user for all user-system type? Thank You

sstok commented 9 years ago

No, all the user-systems are interdependent, meaning they don't know of each others existence.

If you want something like this you need to create your own validator.

djwatt3 commented 9 years ago

very well. I have created my validator that not allow multiple user with same username or email in the system. This is the constraint class:

namespace Rollerworks\Bundle\MultiUserBundle\Validator\Constraints;
use FOS\UserBundle\Model\UserManagerInterface;
use Rollerworks\Bundle\MultiUserBundle\Model\UserConfig;
use Rollerworks\Bundle\MultiUserBundle\Model\UserDiscriminatorInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

/** Constraint Unique Email in all User-Systems. */

/** @Annotation */
class UniqueEmailSystemValidator extends ConstraintValidator {

    private $userDiscriminator;

    /** @param UserDiscriminatorInterface $userDiscriminator */
    public function __construct(UserDiscriminatorInterface $userDiscriminator)
    {
        $this->userDiscriminator = $userDiscriminator;
    }

    public function validate($value, Constraint $constraint)
    {
        $usersInSystems = $this->userDiscriminator->getUsers();
        $currentUserConfig = $this->userDiscriminator->getCurrentUserConfig();

        /** @var UserConfig $userInSystem */
        foreach($usersInSystems as $userInSystem) {
            if ($userInSystem !== $currentUserConfig) {
                /** @var UserManagerInterface $userDocumentManager */
                $userDocumentManager = $userInSystem->getUserManager();
                if ($userDocumentManager->findUserByEmail($value)) {
                    $this->buildViolation($constraint->message)
                        ->setTranslationDomain('FOSUserBundle')
                        ->addViolation();
                }
            }
        }
    }
}

I have added also new method in UserDiscriminator class, it is getUsers() that return all users present in the system. So i have added this constraint to field in my model class:

    /** @MongoDB\UniqueIndex()
     *  @RollerworksAssert\UniqueEmailSystem(message="Email già esistente nel sistema") */
    protected $email;

Done also unique login for all users. I select the correct user with requestMatcher class by Role identify. Thanks you

sstok commented 9 years ago

Please use the GitHub markdown code formatting. https://help.github.com/articles/markdown-basics/#code-formatting

I edited you reply to make it more readable.

djwatt3 commented 9 years ago

Ok. Thank You