EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

FOSUserBundle Integration (password update and deleting current user).

Open rernesto opened this issue 7 years ago • 2 comments

Bugs

Password Update

For some reason password doesn't update when you change only plainPassword field. If you update any other field together, everything works perfect.

User deletion

Default behavior allows current logged user deletion.

Bundles version

easycorp/easyadmin-bundle v2.0.6 and friendsofsymfony/user-bundle v2.1.2.

Solutions

How I am using a custom controller for my User entity I fixed this way:

Password Update

...
/**
     * @param User $entity
     */
    protected function updateEntity($entity)
    {
        $this->userManager->updateUser($entity, false);
        $plainPassword = $this->request->request->get(
            strtolower($this->entity['name']))['plainPassword'];
        if(strlen($plainPassword) !== 0) {
            $entity->setPlainPassword($plainPassword);
            $this->userManager->updatePassword($entity);
        }
        parent::updateEntity($entity);
    }
...

User Deletion

...
/**
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|void
     * @throws \Exception
     */
    protected function deleteAction()
    {
        $id = (int) $this->request->query->get('id');
        $currentUserId = $this->get('security.token_storage')
            ->getToken()->getUser()->getId();
        if ($id !== $currentUserId) {
            parent::deleteAction();
        } else {
            throw new \Exception('Current Logged-in user cannot be deleted.');
        }
    }
...

I know this is not part of EasyAdmin Bundle internals, but if this issue is wide spread I think FOSUserBundle integration help should be updated on Integrating FOSUserBundle to Manage Users.

rernesto avatar Feb 08 '19 23:02 rernesto

Enable or disable current logged user validation.

Also I added a validation to avoid current logged user being disabled overriding updateEntityProperty method on my custom controller:

protected function updateEntityProperty($entity, $property, $value)
    {
        $id = (int) $this->request->query->get('id');
        $currentUserId = $this->get('security.token_storage')
            ->getToken()->getUser()->getId();

        if ($id === $currentUserId) {
            throw new \RuntimeException(\sprintf('Cannot disable current logged user.'));
        }
        parent::updateEntityProperty($entity, $property, $value);
    }

rernesto avatar Feb 11 '19 18:02 rernesto

I'm sorry but I must close this issue. I've never used FOSUserBundle and Symfony no longer recommends to use it anymore in any app (we even deleted its docs from symfony.com recently). I'm sorry I can't be more helpful.

javiereguiluz avatar Mar 26 '19 19:03 javiereguiluz