Skip to content

Commit

Permalink
Added translations to auth errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeveloper committed Mar 17, 2015
1 parent 0c1f9cc commit 618f50a
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 15 deletions.
16 changes: 16 additions & 0 deletions Controller/SecuredController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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;
}
}
1 change: 1 addition & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

<service id="antwebs_chateasecure_user_provider" class="%antwebs_chateasecure_user_provider.class%" >
<argument type="service" id="chat_secure.adapter" />
<argument type="service" id="translator" />
</service>

<service id="security.authentication_provider.antwebs_chateasecure" class="%antwebs_chateasecure_authentication_provider.class%" abstract="true" public="false">
Expand Down
11 changes: 10 additions & 1 deletion Resources/translations/Login.en.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
login.login: "Login"
login.username: "Username"
login.password: "Password"
login.submit: "Login"
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.'
4 changes: 3 additions & 1 deletion Resources/translations/Login.es.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
login.login: "Autenticar"
login.username: "Nombre de usuario"
login.password: "Contraseña"
login.submit: "Autenticar"
login.submit: "Autenticar"
login.bad_credentials: "Usuario o contraseña incorrecto"
login.service_down: 'Servicio de autenticación no disponible'
2 changes: 1 addition & 1 deletion Resources/views/Secured/login.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<h1 class="title">{{ "login.login" | trans }}</h1>

{% if error %}
<div class="error">{{ error.message }}</div>
<div class="error">{{ error }}</div>
{% endif %}

<form action="{{ path("_security_check") }}" method="post" id="login">
Expand Down
27 changes: 15 additions & 12 deletions Security/User/UserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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'));
}


Expand All @@ -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;
}
Expand All @@ -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 {
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 618f50a

Please sign in to comment.