3

Here is my code for adding admin user:

public function addAction(Request $request){
    $admin=new Admin();
    $form=$this->createForm(new AdminType(), $admin);
    if($request->getMethod()=='POST'){
        $form->bindRequest($request);
        if($form->isValid()){
            $factory = $this->get('security.encoder_factory');
            $encoder = $factory->getEncoder($admin);
            $password = $encoder->encodePassword($admin->getPassword(), $admin->getSalt());
            $admin->setPassword($password);

            $em=$this->getDoctrine()->getEntityManager();
            $em->persist($admin);
            $em->flush();
        }
    }
    return $this->render('PuzzleAdminBundle:Admin:add.html.twig', array(
        'form'=>$form->createView()
    ));
}

and here is my security.yml:

security:
encoders:
    Puzzle\AdminBundle\Entity\Admin: sha512
    Symfony\Component\Security\Core\User\User: plaintext

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

providers:
    chain_providers:
        chain:
            providers: [admin_db, in_memory]
    admin_db:
        entity: { class: Puzzle\AdminBundle\Entity\Admin, property: username }
    in_memory:
        memory:
            users:
                root: { password: 123456, roles: [ 'ROLE_SUPER_ADMIN' ] }

Everything is OK when i want to login on root, or when I set up entity encoder to plaintext. Why I always get bad credentials when I set up entity encoder to sha512?

3 Answers 3

8

Check the field length in your database. If you were following "The Book" then likely your field will be set to varchar(50). Base64-encoded SHA-512 will output an 88 character string. Adjust for this and you should be good to go.

Sign up to request clarification or add additional context in comments.

2 Comments

You sir, are a gentleman and a scholar! I was following the book and assumed I did something wrong with the YML files... sigh.
OMG!!! Thank you so much! I just spent two hours changing everything and you saved me!
1

I believe that the 'password' value defined would need to be the value post-encoding. So the value it will be looking for when set up to use a sha512 encoder will be the sha512 hash of '123456' rather than the plain text password.

Take a look here: http://symfony.com/doc/current/book/security.html#encoding-the-user-s-password . The paragraph after the first example in this section deals with this specifically.

Comments

1

I had the problem indicated and in my case it was a pretty silly thing, the length of the field in the database too small

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.