M Khalid Junaid
M Khalid Junaid

Reputation: 64466

Create user using FOS user bundle for custom defined user type

Here is what i am working on i have defined the custom user types using the FOS user bundle also using sonata admin bundle ,i have successfully created services for admin config.yml, Also generated the fos user entity in my custom bundle

sonata.admin.hrmanagement:
    class: Namespace\Mybundlename\Admin\MyAdminClass
    tags:
        - { name: sonata.admin, manager_type: orm, group: "Content", label: "My user type" }
    arguments: [null, Namespace\Mybundlename\Entity\FosUser, ~]
    calls:
        - [ setTranslationDomain, [NamespaceMybundlenameBundle]]

I have imported above yml in main congig.yml under app folder ,While creating the user i want the my security encoder to hash the received plain password (creating/editing user ),successfully defined the security encoder in main security.yml like

security:
    encoders:
       Namespace\Mybundlename\Entity\FosUser: sha512

Now in MyAdminClass i have filters for before update and after update how can i access above defined security encoder of my entity

public function preUpdate($object)
{
   $salt = md5(time());
   $encoderservice = $this->get('security.encoder_factory');// here is the problem i can't access
   $encoder = $encoderservice->getEncoder($object);
   $encoded_pass = $encoder->encodePassword($object->getPassword(),$salt );             
   $object->setSalt($salt);

}

Upvotes: 3

Views: 1329

Answers (1)

Shaheer
Shaheer

Reputation: 2147

you need to use

$this->getConfigurationPool()->getContainer()->get('security.encoder_factory') as container is not directly accessible in the Admin class.

EDIT

If you want to have the object available directly to your code (like $this->container ) then you can do the following:

add a protected $container in your admin class

add a configure method in your admin class:

fetch the container, and assign it to the $container

public function configure() {
    $this->container = $this->getConfigurationPool()->getContainer();
}

Profit!

Upvotes: 2

Related Questions