Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Chrysweel committed Mar 18, 2015
2 parents 810e8f1 + 07db374 commit f2557db
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 17 deletions.
24 changes: 24 additions & 0 deletions Controller/SecuredController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}
}
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
13 changes: 13 additions & 0 deletions Resources/translations/Login.en.yml
Original file line number Diff line number Diff line change
@@ -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.'
6 changes: 6 additions & 0 deletions Resources/translations/Login.es.yml
Original file line number Diff line number Diff line change
@@ -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'
12 changes: 7 additions & 5 deletions Resources/views/Secured/login.html.twig
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
{% extends "ChateaSecureBundle::layout.html.twig" %}

{% trans_default_domain 'Login' %}

{% block body %}
<h1 class="title">Login</h1>
<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">
<div>
<label for="username">Username</label>
<label for="username">{{ "login.username" | trans }}</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" />
</div>

<div>
<label for="password">Password</label>
<label for="password">{{ "login.password" | trans }}</label>
<input type="password" id="password" name="_password" />
</div>

<button type="submit" class="sf-button">
<span class="border-l">
<span class="border-r">
<span class="btn-bg">Login</span>
<span class="btn-bg">{{ "login.submit" | trans }}</span>
</span>
</span>
</button>
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 f2557db

Please sign in to comment.