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); } }