You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jul 26, 2018. It is now read-only.
<?phpnamespaceBaseBundle\Services;
useDoctrine\ORM\Proxy\Proxy;
useEasyCorp\Bundle\EasySecurityBundle\Security\SecurityasBaseSecurity;
useHWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\OAuthToken;
useSymfony\Component\DependencyInjection\ContainerAwareTrait;
useSymfony\Component\Security\Core\User\UserInterface;
classSecurityextendsBaseSecurity
{
useContainerAwareTrait;
/** * This method should only be used to enforce login on development * environment (when you don't have an internet connection for example) * or on demo websites where visitors can try features requiring authentication. * * @param int $id * * @return Security */publicfunctionloginById($id)
{
$user = $this->container
->get('doctrine')
->getManager()
->getRepository('BaseBundle:User')
->findOneById($id);
$this->login(
$this->container
->get('base.oauth_user_provider')
->loadUserByUsername($user->getUsername())
);
return$this;
}
/** * {@inheritdoc} */publicfunctionlogin(UserInterface$user, $firewallName = 'main')
{
$token = newOAuthToken(null, $user->getRoles());
$token->setUser($this->getRealEntity($user));
$token->setAuthenticated(true);
$this->container->get('security.token_storage')->setToken($token);
$this->container->get('session')->set("_security_{$firewallName}", serialize($token));
$this->container->get('session')->save();
return$this;
}
/** * This method is used to store a real entity and not a doctrine proxy * on the tokenstorage (they internally do a get_class and if the entity * was lazily loaded, it will be an instance of a proxy). * * @param mixed $proxy * * @return mixed */publicfunctiongetRealEntity($proxy)
{
if ($proxy instanceof Proxy) {
$metadata = $this->getManager()->getMetadataFactory()->getMetadataFor(get_class($proxy));
$class = $metadata->getName();
$entity = new$class();
$reflectionSourceClass = new \ReflectionClass($proxy);
$reflectionTargetClass = new \ReflectionClass($entity);
foreach ($metadata->getFieldNames() as$fieldName) {
$reflectionPropertySource = $reflectionSourceClass->getProperty($fieldName);
$reflectionPropertySource->setAccessible(true);
$reflectionPropertyTarget = $reflectionTargetClass->getProperty($fieldName);
$reflectionPropertyTarget->setAccessible(true);
$reflectionPropertyTarget->setValue($entity, $reflectionPropertySource->getValue($proxy));
}
return$entity;
}
return$proxy;
}
}
He says that they are useful when:
On many dev purposes (notably when there are only hwi ways to login on disconnected envs), I required those.
Thoughts? Thanks!
The text was updated successfully, but these errors were encountered:
@ninsuo in this comment is proposing to add new methods:
He says that they are useful when:
Thoughts? Thanks!
The text was updated successfully, but these errors were encountered: