2

I'm building a multitenancy backend using Symfony 2.7.9 with FOSRestBundle and JMSSerializerBundle.

When returning objects over the API, I'd like to hash all the id's of the returned objects, so instead of returning { id: 5 } it should become something like { id: 6uPQF1bVzPA } so I can work with the hashed id's in the frontend (maybe by using http://hashids.org)

I was thinking about configuring JMSSerializer to set a virtual property (e.g. '_id') on my entities with a custom getter-method that calculates the hash for the id, but I don't have access to the container / to any service.

How could I properly handle this?

bac
  • 226
  • 3
  • 10

2 Answers2

1

You could use a Doctrine postLoad listener to generate a hash and set a hashId property in your class. Then you could call expose the property in the serializer but set the serialized_name as id (or you could just leave it at hash_id).

Due to the hashing taking place int the postLoad you would need to refresh your object if you have just created it using $manager->refresh($entity) for it take effect.

AppBundle\Doctrine\Listener\HashIdListener

class HashIdListsner
{
    private $hashIdService;

    public function postLoad(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();
        $reflectionClass = new \ReflectionClass($entity);

        // Only hash the id if the class has a "hashId" property
        if (!$reflectionClass->hasProperty('hashId')) {
            return;
        }

        // Hash the id
        $hashId = $this->hashIdService->encode($entity->getId());

        // Set the property through reflection so no need for a setter
        // that could be used incorrectly in future 
        $property = $reflectionClass->getProperty('hashId');
        $property->setAccessible(true);
        $property->setValue($entity, $hashId);
    }
}

services.yml

services:
    app.doctrine_listsner.hash_id:
        class: AppBundle\Doctrine\Listener\HashIdListener
        arguments:
            # assuming your are using cayetanosoriano/hashids-bundle
            - "@hashids"
        tags:
            - { name: doctrine.event_listener, event: postLoad }

AppBundle\Resources\config\serializer\Entity.User.yml

AppBundle\Entity\User:
    exclusion_policy: ALL
    properties:
        # ...
        hashId:
            expose: true
            serialized_name: id
        # ...
qooplmao
  • 17,622
  • 2
  • 44
  • 69
1

Thanks a lot for your detailed answer qooplmao.

However, I don't particularly like this approach because I don't intend to store the hashed in the entity. I now ended up subscribing to the serializer's onPostSerialize event in which I can add the hashed id as follows:

use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\ObjectEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;

class MySubscriber implements EventSubscriberInterface
{
    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public static function getSubscribedEvents()
    {
        return array(
            array('event' => 'serializer.post_serialize', 'method' => 'onPostSerialize'),
        );
    }

    /**
     * @param ObjectEvent $event
     */    
    public function onPostSerialize(ObjectEvent $event)
    {
        $service = $this->container->get('myservice');
        $event->getVisitor()->addData('_id', $service->hash($event->getObject()->getId()));
    }
}
bac
  • 226
  • 3
  • 10