public function postAction(Request $request)
    {
        if (!$this->isGranted('IS_AUTHENTICATED_FULLY')) {
            throw new InsufficientAuthenticationException('You must be authenticated to view users');
        }

        $userExists = $this->getApplication()
                           ->getEntityManager()
                           ->getRepository(get_class($this->getUser()))
                           ->findBy(array('_login' => $request->request->get('login')));

        if ($userExists) {
            throw new ConflictHttpException(sprintf('User with that login already exists: %s', $request->request->get('login')));
        }

        $user = new User();

        if (!$this->isGranted('CREATE', new ObjectIdentity('class', get_class($user)))) {
            throw new InsufficientAuthenticationException(sprintf('You are not authorized to create users'));
        }

        $user = $this->deserializeEntity($request->request->all(), $user);
        // handle the password
        if ($request->request->has('password')) {
            $password = $request->request->get('password');
        } elseif ($request->request->has('generate_password') && true === $request->request->get('generate_password')) {
            $password = substr(hash('sha512', rand()), 0, 6);
        } else {
            return new JsonResponse([
                'errors' => [
                    'password' => ['Password not provided.'],
                ],
            ], 400);
        }

        $user->setRawPassword($password);
        $encoderFactory = $this->getContainer()->get('security.context')->getEncoderFactory();

        if ($encoderFactory && $encoder = $encoderFactory->getEncoder($user)) {
            $password = $encoder->encodePassword($password, '');
        }

        $user->setPassword($password);

        $this->getEntityManager()->persist($user);
        $this->getEntityManager()->flush($user);

        $event = new Event($user);
        $this->getApplication()->getEventDispatcher()->dispatch('rest.user.creation', $event);

        return new Response($this->formatItem($user), 200, ['Content-Type' => 'application/json']);
    }