-1

I have integrated in my Symfony project the FOSUserBundle with the HWIOAUTHBundle. I have a facebook and a google login/registration next to the simple login form from FOSUserBundle. However, Facebook doesn't necessarily give me an email back, since it's not needed for a Facebook account (i.e when the user registers on Facebook via phone). In this case, when the user registers I set her/his email address to be the same as the facebook/google ID (because I can't leave email field empty).

When somebody on my website orders an item, I need to send him an email containing a QR code that she/he will use to authenticate himself, thus I need a way to get a real e-mail address from the user.

I have thought that when the user registers via Facebook and tries to purchase a product I'd redirect her/him to the profile edit page, show a notification that he/she should provide a correct email address and then she/he can move on with the purchase.

However, I need to validate if their current email is a real (or at least real-looking) email address in the controller that handles the purchases.

How can I use Symfony's validator in a controller to check if the user's email from $user = $this->get('security.token_storage')->getToken()->getUser(); $user->getEmail(); really looks like an email?

For now this is what I have:

if (!$this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
    throw $this->createAccessDeniedException('some message');
}

$user = $this->get('security.token_storage')->getToken()->getUser();

if ($user->isEnabled() == false) {
    throw $this->createAccessDeniedException('some message');
}

if (null == $user->getEmail() || $user->getFacebookId() == $user->getEmail() || $user->getGoogleId() === $user->getEmail()) {
    $session = new Session();
    $session->getFlashBag()->add('warning', 'Please provide a real email address, yata yata, etc.');
    return $this->redirectToRoute('fos_user_profile_edit');
}

Thanks in advance!

mywebstuff.hq
  • 43
  • 1
  • 6

2 Answers2

3

Try this in your controller

    ...
use Symfony\Component\Validator\Validator\ValidatorInterface;

// ...
public function addEmailAction($email, ValidatorInterface $validator)
{
    $emailConstraint = new Assert\Email();
    // all constraint "options" can be set this way
    $emailConstraint->message = 'Invalid email address';

    // use the validator to validate the value
    $errorList = $validator->validate(
        $email,
        $emailConstraint
    );

See the doc here https://symfony.com/doc/current/validation/raw_values.html

Mz1907
  • 625
  • 4
  • 10
  • 1
    You should active `checkMX` and `checkHost` on the Symfony Email assert if you want to be sure that the DNS exist and that a MX server is running under it (necessary to receive an email). With that you could accept the email :) – Mcsky Nov 09 '17 at 10:44
  • Mcsky answer is totally right. You just need to know that activating CheckMX and CheckHost may improve a little bit performances. But not that much. Check here doc for checkMX and checHost. https://symfony.com/doc/current/reference/constraints/Email.html – Mz1907 Nov 09 '17 at 10:51
  • 1
    Activating these options won't improve performances, that will decrease them because the application will do some calls to check domain and mx. But if you want more than just check the email string and ensure that the email is "real" you have to do that. Chech the `vendor/symfony/symfony/src/Symfony/Component/Validator/Constraints/EmailValidator.php` class – Mcsky Nov 09 '17 at 11:22
  • @Mcsky yes that's what I meant but do to my "bad" english I wrote improve instead of decrease thinking the verb improve meant "decrease". Thx for your comment. – Mz1907 Nov 09 '17 at 11:27
  • @Mz1907 Thank you very much, I do not know how I could miss that :) Also thanks Mcsky I have not known that, it truly looks like an awesome feature, though I need some tests with that first :) – mywebstuff.hq Nov 09 '17 at 18:32
0

You should be able to just use the validator service and feed it with the value and the constraint (or a list of combined constraints)

This simple example should work (for sf 3.3+ depending on your service definition strategy you may have to inject it through the constructor)

public function testAction()
{
    $constraint = new \Symfony\Component\Validator\Constraints\Email();
    $stringToTest = 'lorem@ipsum.com';

    $errors = $this->get('validator')->validate($stringToTest, $constraint);

    if(count($errors) > 0) {
        //no valid email
    }
}
Joe
  • 2,356
  • 10
  • 15