0

I want to access the encoded password in a controller or a service.

I can access it using Twig:

{{ app.user.password }} /* prints the encoded password. */

But when I am trying it from a controller, I'll get a null result:

$user = $this->get('security.context')->getToken()->getUser();
$user->getPassword(); /* returns null */

Why can I access it using Twig, but not from the controller?

Xover
  • 588
  • 5
  • 10
  • Do the other functions like `getId()` work with the `User` object you are getting from the `security.context` service or is it only the `getPassword()` method ? – Nicolai Fröhlich Nov 25 '13 at 11:03
  • Yes. `getId`, `getUsername`, `getCreatedAt` and also `getSalt` are working. Just `getPassword` returns NULL. – Xover Nov 25 '13 at 15:04
  • 1
    Does the `getPassword()` method actually **exist** in your `User` entity? With the information you provide this isn't for sure. if you had a public property `$password` that would explain why `{{ app.user.password }}` works but `$user->getPassword()` fails. – Nicolai Fröhlich Nov 25 '13 at 15:06
  • No. This wasn't the problem. But I found my answer here: http://stackoverflow.com/questions/9150262/symfony2-user-password-set-to-empty-after-running-this-method :) – Xover Nov 26 '13 at 14:01
  • Could you please add your findings with some description as an answer so we can have this issue marked as solved and other users can benefit aswell? – Nicolai Fröhlich Nov 26 '13 at 14:02

0 Answers0