0

I have already existing user table with password, etc. I'm currently doing migration to FOSUserBundle Entity. Everything is ok, here is migration:

namespace Application\Migrations;

use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Auto-generated Migration: Please modify to your needs!
 */
class Version20140909172327 extends AbstractMigration implements ContainerAwareInterface
{
    public function up(Schema $schema)
    {
        // this up() migration is auto-generated, please modify it to your needs
        $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');

        $this->addSql('ALTER TABLE User ADD username VARCHAR(255) NOT NULL, ADD username_canonical VARCHAR(255) NOT NULL, ADD email_canonical VARCHAR(255) NOT NULL, ADD enabled TINYINT(1) NOT NULL, ADD salt VARCHAR(255) NOT NULL, ADD last_login DATETIME DEFAULT NULL, ADD locked TINYINT(1) NOT NULL, ADD expired TINYINT(1) NOT NULL, ADD expires_at DATETIME DEFAULT NULL, ADD confirmation_token VARCHAR(255) DEFAULT NULL, ADD password_requested_at DATETIME DEFAULT NULL, ADD roles LONGTEXT NOT NULL COMMENT \'(DC2Type:array)\', ADD credentials_expired TINYINT(1) NOT NULL, ADD credentials_expire_at DATETIME DEFAULT NULL, CHANGE password password VARCHAR(255) NOT NULL, CHANGE email email VARCHAR(255) NOT NULL');
        $this->addSql('CREATE UNIQUE INDEX UNIQ_2DA1797792FC23A8 ON User (username_canonical)');
        $this->addSql('CREATE UNIQUE INDEX UNIQ_2DA17977A0D96FBF ON User (email_canonical)');
        $this->addSql('UPDATE User SET roles="a:0:{}", enabled=1');
    }

    public function down(Schema $schema)
    {
        // this down() migration is auto-generated, please modify it to your needs
        $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');

        $this->addSql('DROP INDEX UNIQ_2DA1797792FC23A8 ON User');
        $this->addSql('DROP INDEX UNIQ_2DA17977A0D96FBF ON User');
        $this->addSql('ALTER TABLE User DROP username, DROP username_canonical, DROP email_canonical, DROP enabled, DROP salt, DROP last_login, DROP locked, DROP expired, DROP expires_at, DROP confirmation_token, DROP password_requested_at, DROP roles, DROP credentials_expired, DROP credentials_expire_at, CHANGE email email LONGTEXT DEFAULT NULL, CHANGE password password LONGTEXT DEFAULT NULL');
    }

    public function postUp(Schema $schema)
    {
        $em = $this->container->get('doctrine.orm.entity_manager');

        $users = $em->getRepository('AcmeWhanToBundle:User')->findAll();

        foreach($users as $user)
        {
            if($user->getAdmin()) {
                $user->addRole('ROLE_ADMIN');
            }
        }

        $em->flush();
    }

    /**
     * @var Container
     */
    private $container;

    /**
     * {@inheritDoc}
     */
    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }
}

but the problem is when I try to login with current password. Current passwords were created with sha1 algorythm. This is my security.yml:

security:
    encoders:
        Symfony\Component\Security\Core\User\User: sha1
        FOS\UserBundle\Model\UserInterface: sha1

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username_email

    firewalls:
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false

        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_provider: form.csrf_provider
            logout:       true
            anonymous:    true

    access_control:
        - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/, roles: ROLE_USER }

I'm getting error "Bad credentials". What am I missing?

mmmm
  • 3,768
  • 9
  • 36
  • 63
  • Are you encoders the same ? And are you doing the migration on the same server as the original users were generated ? – Oliver Bayes-Shelton Sep 09 '14 at 16:12
  • You can see encoders in security... As I said passwords were coded with sha1 algo. I'm currently doing this on localhost. – mmmm Sep 09 '14 at 16:13
  • yes but are the encoders the same as when you generated the user and the same now ? also did you generate all the users locally ? – Oliver Bayes-Shelton Sep 09 '14 at 16:14
  • yep, I have exactly the same user in my localhost database. And sha1 was used to generate password for this user. – mmmm Sep 09 '14 at 16:15
  • but was the user generated locally or on another machine ? and was sha1 used for all users ? – Oliver Bayes-Shelton Sep 09 '14 at 16:16
  • No, I've taken data from remote database and saved it on localhost. And yes, sha1 was used for all users. – mmmm Sep 09 '14 at 16:17
  • 1
    then maybe the way the two machines are hashing/encoding the passwords and tokens are different. also do you own research first look at - http://stackoverflow.com/questions/12005004/migrating-legacy-users-to-symfony2 – Oliver Bayes-Shelton Sep 09 '14 at 16:19

0 Answers0