From e3329262d8d6e7dfd7173a851c6151e55ee9754d Mon Sep 17 00:00:00 2001 From: jdeveloper Date: Mon, 9 Mar 2015 13:44:24 +0100 Subject: [PATCH 1/4] Added translations --- Resources/translations/Login.en.yml | 4 ++++ Resources/translations/Login.es.yml | 4 ++++ Resources/views/Secured/login.html.twig | 10 ++++++---- 3 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 Resources/translations/Login.en.yml create mode 100644 Resources/translations/Login.es.yml diff --git a/Resources/translations/Login.en.yml b/Resources/translations/Login.en.yml new file mode 100644 index 0000000..590f4b8 --- /dev/null +++ b/Resources/translations/Login.en.yml @@ -0,0 +1,4 @@ +login.login: "Login" +login.username: "Username" +login.password: "Password" +login.submit: "Login" \ No newline at end of file diff --git a/Resources/translations/Login.es.yml b/Resources/translations/Login.es.yml new file mode 100644 index 0000000..01ace25 --- /dev/null +++ b/Resources/translations/Login.es.yml @@ -0,0 +1,4 @@ +login.login: "Autenticar" +login.username: "Nombre de usuario" +login.password: "Contraseña" +login.submit: "Autenticar" \ No newline at end of file diff --git a/Resources/views/Secured/login.html.twig b/Resources/views/Secured/login.html.twig index 0592ac3..434cc4b 100755 --- a/Resources/views/Secured/login.html.twig +++ b/Resources/views/Secured/login.html.twig @@ -1,7 +1,9 @@ {% extends "ChateaSecureBundle::layout.html.twig" %} +{% trans_default_domain 'Login' %} + {% block body %} -

Login

+

{{ "login.login" | trans }}

{% if error %}
{{ error.message }}
@@ -9,19 +11,19 @@
- +
- +
From 0c1f9ccdeafdfbb1e7c649d2b3f732e51e3557a3 Mon Sep 17 00:00:00 2001 From: Borja Date: Tue, 17 Mar 2015 11:33:45 +0100 Subject: [PATCH 2/4] redirect to home when user is autenticated in /login controller --- Controller/SecuredController.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Controller/SecuredController.php b/Controller/SecuredController.php index 5d38383..f0c928d 100755 --- a/Controller/SecuredController.php +++ b/Controller/SecuredController.php @@ -14,12 +14,18 @@ class SecuredController extends Controller */ public function loginAction(Request $request) { + $securityContext = $this->container->get('security.context'); + if ($securityContext->isGranted('IS_AUTHENTICATED_FULLY')) { + return $this->redirect("/"); + } + if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) { $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR); } else { $error = $request->getSession()->get(SecurityContext::AUTHENTICATION_ERROR); } + return $this->render('ChateaSecureBundle:Secured:login.html.twig',array( 'last_username' => $request->getSession()->get(SecurityContext::LAST_USERNAME), 'error' => $error, From 618f50a8d17a9eea144cbfba19ee4c0547b1fea1 Mon Sep 17 00:00:00 2001 From: jdeveloper Date: Tue, 17 Mar 2015 14:07:24 +0100 Subject: [PATCH 3/4] Added translations to auth errors --- Controller/SecuredController.php | 16 +++++++++++++++ Resources/config/services.xml | 1 + Resources/translations/Login.en.yml | 11 +++++++++- Resources/translations/Login.es.yml | 4 +++- Resources/views/Secured/login.html.twig | 2 +- Security/User/UserProvider.php | 27 ++++++++++++++----------- 6 files changed, 46 insertions(+), 15 deletions(-) diff --git a/Controller/SecuredController.php b/Controller/SecuredController.php index f0c928d..2264eaa 100755 --- a/Controller/SecuredController.php +++ b/Controller/SecuredController.php @@ -25,6 +25,7 @@ public function loginAction(Request $request) $error = $request->getSession()->get(SecurityContext::AUTHENTICATION_ERROR); } + $error = $this->extractAuthErrorI18N($error); return $this->render('ChateaSecureBundle:Secured:login.html.twig',array( 'last_username' => $request->getSession()->get(SecurityContext::LAST_USERNAME), @@ -47,4 +48,19 @@ public function logoutAction() { // The security layer will intercept this request } + + private function extractAuthErrorI18N($error) + { + $translator = $this->get('translator'); + $translationMap = array( + 'Bad credentials' => 'login.bad_credentials' + ); + $message = $error->getMessage(); + + if(isset($translationMap[$message])){ + return $translator->trans($translationMap[$message], array(), 'Login'); + } + + return $message; + } } \ No newline at end of file diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 6f4e511..c18b62a 100755 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -27,6 +27,7 @@ + diff --git a/Resources/translations/Login.en.yml b/Resources/translations/Login.en.yml index 590f4b8..c7e3a44 100644 --- a/Resources/translations/Login.en.yml +++ b/Resources/translations/Login.en.yml @@ -1,4 +1,13 @@ login.login: "Login" login.username: "Username" login.password: "Password" -login.submit: "Login" \ No newline at end of file +login.submit: "Login" +login.bad_credentials: "Bad credentials" +login.service_down: 'Authentication service down' +login.incorrect_credentialas: 'Incorrect username or password for %username%' +login.class_not_supported: 'Instances of "%class%" are not supported.' +login.method_not_supported: 'This method is not soported' +login.incorrect_facebookid: 'Incorrect facebookId' +login.facebookid_not_empty: 'The facebookId cannot be empty.' +login.password_not_empty: 'The password cannot be empty.' +login.username_not_empty: 'The username cannot be empty.' \ No newline at end of file diff --git a/Resources/translations/Login.es.yml b/Resources/translations/Login.es.yml index 01ace25..eb95790 100644 --- a/Resources/translations/Login.es.yml +++ b/Resources/translations/Login.es.yml @@ -1,4 +1,6 @@ login.login: "Autenticar" login.username: "Nombre de usuario" login.password: "Contraseña" -login.submit: "Autenticar" \ No newline at end of file +login.submit: "Autenticar" +login.bad_credentials: "Usuario o contraseña incorrecto" +login.service_down: 'Servicio de autenticación no disponible' \ No newline at end of file diff --git a/Resources/views/Secured/login.html.twig b/Resources/views/Secured/login.html.twig index 434cc4b..4319d55 100755 --- a/Resources/views/Secured/login.html.twig +++ b/Resources/views/Secured/login.html.twig @@ -6,7 +6,7 @@

{{ "login.login" | trans }}

{% if error %} -
{{ error.message }}
+
{{ error }}
{% endif %} diff --git a/Security/User/UserProvider.php b/Security/User/UserProvider.php index 7970709..f29047a 100755 --- a/Security/User/UserProvider.php +++ b/Security/User/UserProvider.php @@ -9,48 +9,51 @@ use Symfony\Component\Security\Core\Exception\UnsupportedUserException; use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; use Symfony\Component\Security\Core\User\UserInterface; +use Symfony\Component\Translation\TranslatorInterface; class UserProvider implements ChateaUserProviderInterface { private $authentication; + private $translator; - public function __construct(HttpAdapterInterface $authentication) + public function __construct(HttpAdapterInterface $authentication, TranslatorInterface $translator) { $this->authentication = $authentication; + $this->translator = $translator; } public function loadUser($username, $password) { if (empty($username)) { - throw new \InvalidArgumentException('The username cannot be empty.'); + throw new \InvalidArgumentException($this->translator->trans('login.username_not_empty', array(), 'Login')); } if(empty($password)) { - throw new \InvalidArgumentException('The password cannot be empty.'); + throw new \InvalidArgumentException($this->translator->trans('login.password_not_empty', array(), 'Login')); } try { $data = $this->authentication->withUserCredentials($username, $password); return $this->mapJsonToUser($data); } catch (ApiException $ae) { - throw new BadCredentialsException('Authentication service down'); + throw new BadCredentialsException($this->translator->trans('login.service_down', array(), 'Login')); } catch (AuthenticationException $e) { - throw new UsernameNotFoundException(sprintf('Incorrect username or password for %s ', $username),30,$e); + throw new UsernameNotFoundException($this->translator->trans('login.incorrect_credentialas', array('%username%' => $username), 'Login'),30,$e); } } public function loadUserByFacebookId($facebookId) { if (empty($facebookId)) { - throw new \InvalidArgumentException('The facebookId cannot be empty.'); + throw new \InvalidArgumentException($this->translator->trans('login.facebookid_not_empty', array(), 'Login')); } try { $data = $this->authentication->withFacebookId($facebookId); return $this->mapJsonToUser($data); } catch (ApiException $ae) { - throw new BadCredentialsException('Authentication service down'); + throw new BadCredentialsException($this->translator->trans('login.service_down', array(), 'Login')); } catch (AuthenticationException $e) { - throw new UsernameNotFoundException('Incorrect facebookId',30,$e); + throw new UsernameNotFoundException($this->translator->trans('login.incorrect_facebookid', array(), 'Login'),30,$e); } } @@ -71,7 +74,7 @@ public function loadUserByFacebookId($facebookId) */ public function loadUserByUsername($username) { - throw new \Exception("this method is not soported"); + throw new \Exception($this->translator->trans('login.method_not_supported', array(), 'Login')); } @@ -96,7 +99,7 @@ public function refreshUser(UserInterface $user) if($user instanceof ApiUser){ return $this->loadUser($user->getUsername(), $user->getPlainPassword()); }else if (!$user instanceof User){ - $ex = new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user))); + $ex = new UnsupportedUserException($this->translator->trans('login.class_not_supported', array('%class%' => get_class($user)))); throw $ex; } @@ -105,7 +108,7 @@ public function refreshUser(UserInterface $user) $refreshToken = $user->getRefreshToken(); if(empty($refreshToken)){ - throw new UsernameNotFoundException(sprintf('Incorrect username or password for %s ', $user->getUsername()),30,null); + throw new UsernameNotFoundException($this->translator->trans('login.incorrect_credentialas', array('%username%' => $user->getUsername())),30,null); } try { @@ -114,7 +117,7 @@ public function refreshUser(UserInterface $user) $user->setRefreshToken($data['refresh_token']); $user->setExpiresIn($data['expires_in']); } catch (AuthenticationException $e) { - throw new UsernameNotFoundException(sprintf('Incorrect username or password for %s ', $user->getUsername()),30,$e); + throw new UsernameNotFoundException($this->translator->trans('login.incorrect_credentialas', array('%username%' => $user->getUsername())),30,$e); } } From 07db3740c7c698f35c22441b101a5e2e2fc296c9 Mon Sep 17 00:00:00 2001 From: Chrysweel Date: Wed, 18 Mar 2015 09:29:37 +0100 Subject: [PATCH 4/4] [HOTFIX] get message error in page login --- Controller/SecuredController.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Controller/SecuredController.php b/Controller/SecuredController.php index 2264eaa..8a652b8 100755 --- a/Controller/SecuredController.php +++ b/Controller/SecuredController.php @@ -15,7 +15,7 @@ class SecuredController extends Controller public function loginAction(Request $request) { $securityContext = $this->container->get('security.context'); - if ($securityContext->isGranted('IS_AUTHENTICATED_FULLY')) { + if ($securityContext->isGranted('IS_AUTHENTICATED_FULLY') && $this->get('security.context')->isGranted('IS_AUTHENTICATED_REMEMBERED')) { return $this->redirect("/"); } @@ -25,7 +25,9 @@ public function loginAction(Request $request) $error = $request->getSession()->get(SecurityContext::AUTHENTICATION_ERROR); } - $error = $this->extractAuthErrorI18N($error); + if ($error){ + $error = $this->extractAuthErrorI18N($error); + } return $this->render('ChateaSecureBundle:Secured:login.html.twig',array( 'last_username' => $request->getSession()->get(SecurityContext::LAST_USERNAME),