Search code examples
symfonysulu

Access to the Security services in Sulu Frontend: Hashing a Users password


I'm trying to make a user registration for the Sulu CMF frontend. I want to hash the passwords. But for any reason i can't get the Service.

You have requested a non-existent service "security.encoder_factory".

Or

You have requested a non-existent service "security.password_encoder"

If i ask for

app/console container:debug | grep encode

Both services are there

$ app/console container:debug | grep encode                    
263: security.encoder_factory                                                      Symfony\Component\Security\Core\Encoder\EncoderFactory                                     
266: security.password_encoder                                                     Symfony\Component\Security\Core\Encoder\UserPasswordEncoder                                

Do you have any tip for me? My Controller is aware of the Container and my code is the following:

$this->container->get('security.password_encoder')

Heres some output from conainer:debug

$ app/console container:debug security.password_encoder
[container] Information for service security.password_encoder
Service Id       security.password_encoder
Class            Symfony\Component\Security\Core\Encoder\UserPasswordEncoder
Tags             -
Scope            container
Public           yes
Synthetic        no
Lazy             no
Synchronized     no
Abstract         no


$ app/console container:debug security.encoder_factory 
[container] Information for service security.encoder_factory
Service Id       security.encoder_factory
Class            Symfony\Component\Security\Core\Encoder\EncoderFactory
Tags             -
Scope            container
Public           yes
Synthetic        no
Lazy             no
Synchronized     no
Abstract         no

My currently installed version of symfony is v2.6.12

the command provided by sulu makes use of this method. It works in both environments, dev and prod.

/**
 * Encodes the given password, for the given password, with he given salt and returns the result.
 *
 * @param $user
 * @param $password
 * @param $salt
 *
 * @return mixed
 */
private function encodePassword($user, $password, $salt)
{
    /** @var PasswordEncoderInterface $encoder */
    $encoder = $this->getContainer()->get('security.encoder_factory')->getEncoder($user);

    return $encoder->encodePassword($password, $salt);
}

Solution

  • As xabbuh has already pointed out in the comments, we have two different kernels in Sulu. If you use the app/webconsole command, you are using the Kernel for the Website.

    In the standard installation we don't include the SecurityBundle, because it adds some overhead, and is not necessary for simple websites. However, if you are building a more complicated application, you might also want to secure your website. In this case you can simply add a new instance of Symfony\Bundle\SecurityBundle\SecurityBundle to the $bundles array in app/WebsiteKernel. Afterwards there will also be the service available you have been asking for.