Created
March 16, 2017 15:04
-
-
Save RaphaelBlehoue/fd444e162f9d428836994e12f9a8b7de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: raphael | |
* Date: 07/03/2017 | |
* Time: 16:19 | |
*/ | |
namespace Labs\AuthBundle\Form; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\Extension\Core\Type\EmailType; | |
use Symfony\Component\Form\Extension\Core\Type\PasswordType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
class LoginFormEmail extends AbstractType | |
{ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder | |
->add('_username',EmailType::class, | |
[ | |
'label' => false, | |
'attr' => [ | |
'class' => 'form-control', | |
'placeholder' =>'Adresse Email', | |
'required' => true | |
] | |
] | |
) | |
->add('_password',PasswordType::class, | |
[ | |
'label' => false, | |
'attr' => [ | |
'class' => 'form-control', | |
'placeholder' =>'Mot de passe', | |
'required' => false | |
] | |
] | |
) | |
; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: raphael | |
* Date: 07/03/2017 | |
* Time: 16:19 | |
*/ | |
namespace Labs\AuthBundle\Form; | |
use libphonenumber\PhoneNumberFormat; | |
use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\Extension\Core\Type\PasswordType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\Form\FormEvent; | |
use Symfony\Component\Form\FormEvents; | |
class LoginFormPhone extends AbstractType | |
{ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder | |
->add('phone', | |
PhoneNumberType::class, [ | |
'label' => false, | |
'format' => PhoneNumberFormat::NATIONAL, | |
'widget' => PhoneNumberType::WIDGET_COUNTRY_CHOICE, | |
'preferred_country_choices' => array('FR','US'), | |
] | |
) | |
->add('_password',PasswordType::class, | |
[ | |
'label' => false, | |
'attr' => [ | |
'class' => 'form-control', | |
'placeholder' =>'Mot de passe', | |
'required' => false | |
] | |
] | |
) | |
->addEventListener(FormEvents::SUBMIT, function (FormEvent $event){ | |
$user = $event->getData(); | |
$form = $event->getForm(); | |
if (!$user) { | |
return; | |
} | |
if(null === $user['_username']){ | |
$user['_username'] = "raphael"; | |
$event->setData($user); | |
} | |
}) | |
; | |
} | |
/** | |
* @return string | |
*/ | |
public function getName() | |
{ | |
return $this->getBlockPrefix(); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getBlockPrefix() | |
{ | |
return 'login_form_email'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Labs\AuthBundle\Repository; | |
use Doctrine\ORM\EntityRepository; | |
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface; | |
use Symfony\Component\Security\Core\User\UserInterface; | |
/************************************************************ | |
* | |
* Mon provider | |
* | |
********************/ | |
/** | |
* UserRepository | |
* | |
* This class was generated by the Doctrine ORM. Add your own custom | |
* repository methods below. | |
*/ | |
class UserRepository extends EntityRepository implements UserLoaderInterface | |
{ | |
/** | |
* Loads the user for the given username. | |
* | |
* This method must return null if the user is not found. | |
* | |
* @param string $username The username | |
* | |
* @return UserInterface|null | |
* @throws \Exception | |
*/ | |
public function loadUserByUsername($username) | |
{ | |
return $this->createQueryBuilder('u') | |
->where('u.phone = :phone OR u.email = :email') | |
->setParameter('phone', $username) | |
->setParameter('email', $username) | |
->getQuery() | |
->getOneOrNullResult(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fos_user: | |
resource: "@FOSUserBundle/Resources/config/routing/all.xml" | |
#main provider | |
fos_user_security_login: | |
path: /accounts/authService | |
defaults: { _controller: LabsAuthBundle:Security:login, _method: POST } | |
fos_user_registration_register: | |
path: /accounts/signUp | |
defaults: { _controller: FOSUserBundle:Registration:register, _method: POST|GET } | |
fos_user_security_check: | |
path: /accounts/login_check | |
defaults: { _controller: FOSUserBundle:Security:check, _method: POST } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
security: | |
encoders: | |
FOS\UserBundle\Model\UserInterface: bcrypt | |
role_hierarchy: | |
ROLE_AUTOR: ROLE_USER | |
ROLE_ADMIN: ROLE_USER | |
ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_AUTOR, ROLE_ALLOWED_TO_SWITCH] | |
providers: | |
toudeal_provider: | |
id: labs_auth.login_event | |
firewalls: | |
dev: | |
pattern: ^/(_(profiler|wdt)|css|images|js)/ | |
security: false | |
main: | |
pattern: ^/ | |
form_login: | |
provider: toudeal_provider | |
csrf_token_generator: security.csrf.token_manager | |
login_path: fos_user_security_login | |
check_path: fos_user_security_check | |
# if you are using Symfony < 2.8, use the following config instead: | |
# csrf_provider: form.csrf_provider | |
username_parameter: login_form_email[_username] | |
password_parameter: login_form_email[_password] | |
logout: true | |
anonymous: true | |
context: apps | |
# defaut login area for standard users | |
#main: | |
# pattern: ^/ | |
# form_login: | |
# provider: fos_userbundle | |
# login_path: /login | |
# use_forward: false | |
# check_path: /login_check | |
# failure_path: null | |
# logout: true | |
# anonymous: true | |
access_control: | |
- { path: ^/accounts/authService$, role: IS_AUTHENTICATED_ANONYMOUSLY } | |
#- { path: ^/nm/accounts/authService$, role: IS_AUTHENTICATED_ANONYMOUSLY } | |
- { path: ^/accounts/signUp, role: IS_AUTHENTICATED_ANONYMOUSLY } | |
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY } | |
- { path: ^/, role: ROLE_ADMIN } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: raphael | |
* Date: 07/03/2017 | |
* Time: 15:42 | |
*/ | |
namespace Labs\AuthBundle\Controller; | |
use FOS\UserBundle\Controller\SecurityController as BaseController; | |
use Symfony\Component\HttpFoundation\Request; | |
use Labs\AuthBundle\Form\LoginFormEmail; | |
use Labs\AuthBundle\Form\LoginFormPhone; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\Security\Core\Exception\AuthenticationException; | |
use Symfony\Component\Security\Core\Security; | |
class SecurityController extends BaseController | |
{ | |
public function loginAction(Request $request) | |
{ | |
/** @var $session \Symfony\Component\HttpFoundation\Session\Session */ | |
$session = $request->getSession(); | |
$authErrorKey = Security::AUTHENTICATION_ERROR; | |
$lastUsernameKey = Security::LAST_USERNAME; | |
// get the error if any (works with forward and redirect -- see below) | |
if ($request->attributes->has($authErrorKey)) { | |
$error = $request->attributes->get($authErrorKey); | |
} elseif (null !== $session && $session->has($authErrorKey)) { | |
$error = $session->get($authErrorKey); | |
$session->remove($authErrorKey); | |
} else { | |
$error = null; | |
} | |
if (!$error instanceof AuthenticationException) { | |
$error = null; // The value does not come from the security component. | |
} | |
// last username entered by the user | |
$lastUsername = (null === $session) ? '' : $session->get($lastUsernameKey); | |
$csrfToken = $this->has('security.csrf.token_manager') | |
? $this->get('security.csrf.token_manager')->getToken('authenticate')->getValue() | |
: null; | |
$formChoice = $this->createForm(LoginFormEmail::class); | |
if($this->CheckRouteAddFormView()){ | |
$formChoice = $this->createForm(LoginFormPhone::class); | |
} | |
$form = $formChoice; | |
return $this->renderLogin(array( | |
'error' => $error, | |
'last_username' => $lastUsername, | |
'csrf_token' => $csrfToken, | |
'form' => $form->createView() | |
)); | |
} | |
protected function renderLogin(array $data) | |
{ | |
if($this->CheckRouteAddFormView()){ | |
return $this->render('LabsAuthBundle:Security:login_phone.html.twig', $data); | |
} | |
return $this->render('LabsAuthBundle:Security:login.html.twig', $data); | |
} | |
public function checkAction() | |
{ | |
parent::checkAction(); | |
} | |
public function logoutAction() | |
{ | |
parent::logoutAction(); | |
} | |
private function CheckRouteAddFormView() | |
{ | |
$query_route = $this->get('request_stack')->getCurrentRequest()->query->get('ref'); | |
if(null !== $query_route){ | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment