diff --git a/Controller/SecuredController.php b/Controller/SecuredController.php index 5d38383..8a652b8 100755 --- a/Controller/SecuredController.php +++ b/Controller/SecuredController.php @@ -14,12 +14,21 @@ class SecuredController extends Controller */ public function loginAction(Request $request) { + $securityContext = $this->container->get('security.context'); + if ($securityContext->isGranted('IS_AUTHENTICATED_FULLY') && $this->get('security.context')->isGranted('IS_AUTHENTICATED_REMEMBERED')) { + 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); } + if ($error){ + $error = $this->extractAuthErrorI18N($error); + } + return $this->render('ChateaSecureBundle:Secured:login.html.twig',array( 'last_username' => $request->getSession()->get(SecurityContext::LAST_USERNAME), 'error' => $error, @@ -41,4 +50,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 new file mode 100644 index 0000000..c7e3a44 --- /dev/null +++ b/Resources/translations/Login.en.yml @@ -0,0 +1,13 @@ +login.login: "Login" +login.username: "Username" +login.password: "Password" +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 new file mode 100644 index 0000000..eb95790 --- /dev/null +++ b/Resources/translations/Login.es.yml @@ -0,0 +1,6 @@ +login.login: "Autenticar" +login.username: "Nombre de usuario" +login.password: "Contraseña" +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 0592ac3..4319d55 100755 --- a/Resources/views/Secured/login.html.twig +++ b/Resources/views/Secured/login.html.twig @@ -1,27 +1,29 @@ {% extends "ChateaSecureBundle::layout.html.twig" %} +{% trans_default_domain 'Login' %} + {% block body %} -

Login

+

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