easycorp / easy-security-bundle Goto Github PK
View Code? Open in Web Editor NEWEasySecurityBundle
Home Page: https://easycorp.io/EasySecurity
License: MIT License
EasySecurityBundle
Home Page: https://easycorp.io/EasySecurity
License: MIT License
@ninsuo in this comment is proposing to add new methods:
<?php
namespace BaseBundle\Services;
use Doctrine\ORM\Proxy\Proxy;
use EasyCorp\Bundle\EasySecurityBundle\Security\Security as BaseSecurity;
use HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\OAuthToken;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\Security\Core\User\UserInterface;
class Security extends BaseSecurity
{
use ContainerAwareTrait;
/**
* 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
*/
public function loginById($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}
*/
public function login(UserInterface $user, $firewallName = 'main')
{
$token = new OAuthToken(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
*/
public function getRealEntity($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!
EasyCorp\Bundle\EasySecurityBundle\Security\Security::__construct() must be an instance of Symfony\Component\Security\Core\Role\RoleHierarchy, instance of Symfony\Component\HttpFoundation\Session\Session given
symfony": "^3.3
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.