I've followed the official documentation on using the security provided by symfony 2 standard distribution to authenticate my users, persisting them on the database through Doctrine2, and it seems to be everything ok regarding the configuration, but I always get the same error: Bad Credentials
.
I've use the DoctrineFixturesBundle to load some users and roles, and it seems to complete the loading ok. So, I think the problem is somewhere in the authentication process, which is transparent to me... I don't know how to debug this and would appreciate any help...
Thanks! If you need any other code snippet, please let me know, and i'll edit the question. I've not pasted the code here, to make the question more readable....
EDIT 2: As suggested by @Dieter, I checked the logs, and it seems no data is passed to the query, which is very weird, since I followed every convention they mention in the cookbook... Here is also the definition of my login form. Sorry for the long question! Any help would be greatly appreciated!
snippet of LoginType.php:
public function buildForm(FormBuilder $builder, array $options) {
$builder->add('_username', 'text', array(
'label' => 'Email ',
'required' => true,
));
$builder->add('_password', 'password', array(
'label' => 'Password ',
'required' => true,
));
$builder->add('_remember_me', 'checkbox', array(
'label' => 'Remember me ',
'required' => false,
));
}
app/logs/dev.log output:
[2012-04-17 03:43:01] event.DEBUG: Notified event "kernel.request" to listener
"Symfony\Component\Security\Http\Firewall::onKernelRequest".
[] []
[2012-04-17 03:43:01] doctrine.DEBUG: SET NAMES UTF8 ([]) [] []
[2012-04-17 03:43:01] doctrine.DEBUG: SELECT t0.id AS id1, t0.username AS
username2, t0.salt AS salt3, t0.password AS password4,
t0.is_active AS is_active5, t0.mailer_id AS mailer_id6
FROM SfUser t0 WHERE t0.username = ? (["NONE_PROVIDED"])
[] []
[2012-04-17 03:43:01] security.INFO: Authentication request failed: Bad
credentials [] []
[2012-04-17 03:43:01] security.DEBUG: Redirecting to / [] []
EDIT: Since my pastebin links expired, and, as suggested by Peter Porfey, this is the code:
This is my security.yml
security:
encoders:
ElCuadre\AccountBundle\Entity\User: sha512
role_hierarchy:
ROLE_ADMIN: [ROLE_USER, ROLE_PROVIDER]
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_PROVIDER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
user_db:
entity: { class: ElCuadre\AccountBundle\Entity\User, property: username}
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
secured_area:
pattern: ^/
anonymous: ~
form_login:
login_path: /
check_path: /login_check
logout:
path: /logout
target: /
remember_me:
key: %secret%
lifetime: 3600
path: /
domain: ~ # Defaults to the current domain from $_SERVER
access_control:
- { path: ^/$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
and my controller:
public function loginAction() {
$request = $this->getRequest();
$session = $request->getSession();
// get the login error if there is one
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
} else {
$error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
$session->remove(SecurityContext::AUTHENTICATION_ERROR);
}
$form = $this->createForm(new LoginType());
return $this->render(
'ElCuadreAccountBundle:Auth:login.html.twig',
array(
'form' => $form->createView(),
'last_username' => $session->get(
SecurityContext::LAST_USERNAME),
'error' => $error,
)
);
}
Here is the code of the fixture file:
<?php
namespace ElCuadre\AccountBundle\DataFixtures\ORM;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\DataFixtures\AbstractFixture;
use ElCuadre\AccountBundle\Entity\User;
use ElCuadre\AccountBundle\Entity\Role;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LoadAccountData extends AbstractFixture implements FixtureInterface, ContainerAwareInterface {
private $container;
public function setContainer(ContainerInterface $container = null) {
$this->container = $container;
}
private function loadRole($manager, $name, $roleName) {
$role = new Role();
$role->setName($name);
$role->setRole($roleName);
$manager->persist($role);
$manager->flush();
return $role;
}
private function loadUser($manager, $username, $password, $roles) {
$user = new User();
$user->setUsername($username);
$encoder = $this->container->get('security.encoder_factory')->getEncoder($user);
$user->setPassword($encoder->encodePassword($password, $user->getSalt()));
foreach ($roles as $role) {
$user->addRole($role);
}
$manager->persist($user);
$manager->flush();
}
public function load(ObjectManager $manager) {
// Roles:
$roleUser = $this->loadRole($manager, 'user', 'ROLE_USER');
$roleProvider = $this->loadRole($manager, 'provider', 'ROLE_PROVIDER');
$roleAdmin = $this->loadRole($manager, 'admin', 'ROLE_ADMIN');
$roleSuperAdmin = $this->loadRole($manager, 'superadmin', 'ROLE_SUPER_ADMIN');
// Users:
$this->loadUser($manager, '[email protected]', 'userpass', $roleUser);
$this->loadUser($manager, '[email protected]', 'providerpass', $roleProvider);
$this->loadUser($manager, '[email protected]', 'adminpass', $roleAdmin);
$this->loadUser($manager, '[email protected]', 'superadminpass', $roleSuperAdmin);
}
}
If a similar error happens with my, there is always some kind of problem displayed in app/logs/dev.log, or in the system log like /var/log/apache2/error.log.
Do you see anything popping up there?
EDIT: As a reaction to your new information
Is there a reason why you build that form yourself? As stated in the docs you reference, you can just make the twig template and give the fields the names '_username' and '_password'. The magic should be done for you then.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With