Sonata Admin Bundle User Password
To set a user password on create/update using Sonata Admin you need to:
+ Configure a preUpdate hook
+ Configure the service
+ Use the right filed type :)
Configuring preUpdate Hook
In your admin class (ex. UserAdmin) add this methods:
public function preUpdate($user)
{
$this->getUserManager()->updateCanonicalFields($user);
$this->getUserManager()->updatePassword($user);
}
public function setUserManager(UserManagerInterface $userManager)
{
$this->userManager = $userManager;
}
public function getUserManager()
{
return $this->userManager;
}
Configuring the Service
In your UserAdmin service you have to inject the UserManager (ex. FOS).
To do this, add this lines to your service declaration:
<call method="setUserManager">
<argument type='service' id='fos_user.user_manager' />
</call>
The right field type
For password you have to use a field named plainPassword to avoid password overwriting
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
[..]
->add('plainPassword', 'password')
[...]
;
}
Written by pleone
Related protips
1 Response
Thanks for the tutorial, Just what I needed.
I add the configuration service in YAML format:
...
calls:
...
- [ setUserManager, ["@fosuser.usermanager"]]
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Php
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#