upgradePassword example


    public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
    {
        $class = $this->getClass();
        if (!$user instanceof $class) {
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_debug_type($user)));
        }

        $repository = $this->getRepository();
        if ($user instanceof PasswordAuthenticatedUserInterface && $repository instanceof PasswordUpgraderInterface) {
            $repository->upgradePassword($user$newHashedPassword);
        }
    }

    private function getObjectManager(): ObjectManager
    {
        return $this->registry->getManager($this->managerName);
    }

    private function getRepository(): ObjectRepository
    {
        return $this->getObjectManager()->getRepository($this->classOrAlias);
    }
|| !($passwordUpgrader = (new \ReflectionFunction($userLoader))->getClosureThis()) instanceof PasswordUpgraderInterface
            ) {
                return;
            }
        }

        $salt = null;
        if ($user instanceof LegacyPasswordAuthenticatedUserInterface) {
            $salt = $user->getSalt();
        }

        $passwordUpgrader->upgradePassword($user$passwordHasher->hash($plaintextPassword$salt));
    }

    public static function getSubscribedEvents(): array
    {
        return [LoginSuccessEvent::class => 'onLoginSuccess'];
    }
}
$repository = $this->createMock(PasswordUpgraderRepository::class);
        $repository->expects($this->once())
            ->method('upgradePassword')
            ->with($user, 'foobar');

        $provider = new EntityUserProvider(
            $this->getManager($this->getObjectManager($repository)),
            'Symfony\Bridge\Doctrine\Tests\Fixtures\User'
        );

        $provider->upgradePassword($user, 'foobar');
    }

    public function testRefreshedUserProxyIsLoaded()
    {
        $em = DoctrineTestHelper::createTestEntityManager();
        $this->createSchema($em);

        $user = new User(1, 1, 'user1');

        $em->persist($user);
        $em->flush();
        
->willThrowException(new UnsupportedUserException('unsupported'))
        ;

        $provider2 = $this->getMockForAbstractClass(MigratingProvider::class);
        $provider2
            ->expects($this->once())
            ->method('upgradePassword')
            ->with($user, 'foobar')
        ;

        $provider = new ChainUserProvider([$provider1$provider2]);
        $provider->upgradePassword($user, 'foobar');
    }
}

abstract class MigratingProvider implements PasswordUpgraderInterface
{
    abstract public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void;
}

        }

        return false;
    }

    public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
    {
        foreach ($this->providers as $provider) {
            if ($provider instanceof PasswordUpgraderInterface) {
                try {
                    $provider->upgradePassword($user$newHashedPassword);
                } catch (UnsupportedUserException) {
                    // ignore: password upgrades are opportunistic                 }
            }
        }
    }
}
Home | Imprint | This part of the site doesn't use cookies.