3

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);
}
Patrick
  • 1,562
  • 1
  • 16
  • 33
  • I'm calling from a controller. There i which extends Symfony\Bundle\FrameworkBundle\Controller\Controller (which extends ContainerAware). – Patrick Jan 11 '16 at 11:38
  • Which Symfony version do you use? And can you please show the output of `php app/console container:debug security.password_encoder` and `php app/console container:debug security.encoder_factory`? – xabbuh Jan 11 '16 at 11:45
  • Thanks for your answer. I added more details above. – Patrick Jan 11 '16 at 11:54
  • Is the output different when also using the `--env=prod` option? – xabbuh Jan 11 '16 at 11:54
  • [LogicException] Debug information about the container is only available in debug mode. How can i check if I'm in debug mode? – Patrick Jan 11 '16 at 12:02
  • It may be easier to just create a simple command that makes use of one of these services and then execute it in both the `dev` and the `prod` environment. Can you do that to ensure that your issue is not related to the environment? – xabbuh Jan 11 '16 at 12:43
  • app/console sulu:security:user:create --env=dev and app/console sulu:security:user:create --env=prod bot work as expected I added the method used to generate the password in my question above. – Patrick Jan 11 '16 at 13:18
  • 1
    Are you sure that the CLI and web front controllers actually share the same kernel then? – xabbuh Jan 11 '16 at 13:20
  • You're right. Sulu CMF has an extra console app called app/console webconsole which uses a different Kernel :-) – Patrick Jan 11 '16 at 13:57
  • So now the service is not available and I know why :-) Thats a big step in the right direction. Thanks a lot. – Patrick Jan 11 '16 at 15:01
  • This is a useful Question with good Answers, but the title is not that helpful - it's not really about hashing a password, it's about gaining access to the Security services in Sulu. Any chance you could change it? – frumious Oct 09 '16 at 14:54

2 Answers2

2

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.

Daniel Rotter
  • 1,998
  • 2
  • 16
  • 33
1

Sulu CMF has an extra console app called app/console webconsole which uses a different Kernel :-)

The service i searched for is not existent there. Instead i was able to use

$userManager = $this->container->get('sulu_security.user_manager');
Patrick
  • 1,562
  • 1
  • 16
  • 33
  • That's a possible solution, depending on what you want to do. Will add another answer to achieve what you have actually asked for. – Daniel Rotter Jan 13 '16 at 12:17
  • Thanks for the answer to you (all). A already accomplished my task now. My problem was that i didn't see that SULU has different Kernels :-) – Patrick Jan 13 '16 at 14:53