Browse Source

Merge branch 'develop' of https://gitea.laclic.fr/Laclic/SovBundle into develop

feature/ticket
Guillaume 3 years ago
parent
commit
003f069c6e
4 changed files with 177 additions and 155 deletions
  1. +40
    -16
      Authenticator/LoginFormAuthenticator.php
  2. +18
    -38
      Controller/Admin/AbstractCrudController.php
  3. +28
    -14
      DependencyInjection/Configuration.php
  4. +91
    -87
      Repository/AbstractRepository.php

+ 40
- 16
Authenticator/LoginFormAuthenticator.php View File



use Lc\SovBundle\Model\User\UserInterface; use Lc\SovBundle\Model\User\UserInterface;
use Lc\SovBundle\Doctrine\EntityManager; use Lc\SovBundle\Doctrine\EntityManager;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\User\UserInterface as SfUserInterface; use Symfony\Component\Security\Core\User\UserInterface as SfUserInterface;
private $urlGenerator; private $urlGenerator;
private $csrfTokenManager; private $csrfTokenManager;
private $passwordEncoder; private $passwordEncoder;
protected $parameterBag;


public function __construct( public function __construct(
EntityManager $entityManager,
UrlGeneratorInterface $urlGenerator,
CsrfTokenManagerInterface $csrfTokenManager,
UserPasswordEncoderInterface $passwordEncoder
) {
EntityManager $entityManager,
UrlGeneratorInterface $urlGenerator,
CsrfTokenManagerInterface $csrfTokenManager,
UserPasswordEncoderInterface $passwordEncoder,
ParameterBagInterface $parameterBag
)
{
$this->entityManager = $entityManager; $this->entityManager = $entityManager;
$this->urlGenerator = $urlGenerator; $this->urlGenerator = $urlGenerator;
$this->csrfTokenManager = $csrfTokenManager; $this->csrfTokenManager = $csrfTokenManager;
$this->passwordEncoder = $passwordEncoder; $this->passwordEncoder = $passwordEncoder;
$this->parameterBag = $parameterBag;
} }


public function supports(Request $request) public function supports(Request $request)
{ {
return self::LOGIN_ROUTE === $request->attributes->get('_route') return self::LOGIN_ROUTE === $request->attributes->get('_route')
&& $request->isMethod('POST');
&& $request->isMethod('POST');
} }


public function getCredentials(Request $request) public function getCredentials(Request $request)
{ {
$credentials = [ $credentials = [
'email' => $request->request->get('email'),
'password' => $request->request->get('password'),
'csrf_token' => $request->request->get('_csrf_token'),
'email' => $request->request->get('email'),
'password' => $request->request->get('password'),
'csrf_token' => $request->request->get('_csrf_token'),
]; ];
$request->getSession()->set( $request->getSession()->set(
Security::LAST_USERNAME,
$credentials['email']
Security::LAST_USERNAME,
$credentials['email']
); );


return $credentials; return $credentials;
} }


$user = $this->entityManager->getRepository(UserInterface::class)->findOneBy( $user = $this->entityManager->getRepository(UserInterface::class)->findOneBy(
['email' => $credentials['email']]
['email' => $credentials['email']]
); );


if (!$user) { if (!$user) {
{ {
$routeName = 'home'; $routeName = 'home';
$email = $request->request->get('email'); $email = $request->request->get('email');
$user = $this->entityManager->getRepository(UserInterface::class)->findOneBy(['email' => $email]);
$loginRedirection = $this->parameterBag->get('lc_sov.login_redirection');
$useReferer = $loginRedirection['redirect_referer'];
$rolesRedirection = $loginRedirection['roles_redirection'];

if (isset($useReferer) && $useReferer == true) {
$url = $request->request->get('_target_path');

} else {
$user = $this->entityManager->getRepository(UserInterface::class)->findOneBy(['email' => $email]);


if ($user && ($user->hasRole('ROLE_ADMIN') || $user->hasRole('ROLE_SUPER_ADMIN'))) {
$routeName = 'admin_dashboard';
if (!empty($user)) {
$roles = $user->getRoles();

foreach ($rolesRedirection as $roleRedirect) {
if (array_search($roleRedirect['role'], $roles)) {
$routeName = $roleRedirect['redirect'];
}
}
}
} }


return new RedirectResponse($this->urlGenerator->generate($routeName));
if (isset($url) && !empty($url)) {
return new RedirectResponse($url);
} else {
return new RedirectResponse($this->urlGenerator->generate($routeName));
}
} }


protected function getLoginUrl() protected function getLoginUrl()

+ 18
- 38
Controller/Admin/AbstractCrudController.php View File

use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Twig\Environment;


abstract class AbstractCrudController extends EaAbstractCrudController abstract class AbstractCrudController extends EaAbstractCrudController
{ {
protected $session; protected $session;
protected $request; protected $request;
protected $em;
protected $translatorAdmin; protected $translatorAdmin;


public function __construct( public function __construct(
SessionInterface $session, SessionInterface $session,
RequestStack $request, RequestStack $request,
EntityManager $em, EntityManager $em,
Environment $twig,
TranslatorAdmin $translatorAdmin TranslatorAdmin $translatorAdmin
) )
{ {
$this->session = $session; $this->session = $session;
$this->request = $request; $this->request = $request;
$this->em = $em; $this->em = $em;
$this->twig = $twig;
$this->translatorAdmin = $translatorAdmin; $this->translatorAdmin = $translatorAdmin;
} }




public function configureResponseParameters(KeyValueStore $responseParameters): KeyValueStore public function configureResponseParameters(KeyValueStore $responseParameters): KeyValueStore
{ {
// fields sur la page index
if (Crud::PAGE_INDEX === $responseParameters->get('pageName')) { if (Crud::PAGE_INDEX === $responseParameters->get('pageName')) {
$responseParameters->set('fields', $this->configureFields('index')); $responseParameters->set('fields', $this->configureFields('index'));
} }


public function configureFields(string $pageName): iterable public function configureFields(string $pageName): iterable
{ {
$seoPanel = $confPanel = array();

if ($this->isInstanceOf(SortableInterface::class)) { if ($this->isInstanceOf(SortableInterface::class)) {
$seoPanel = [ $seoPanel = [
FormField::addPanel('seo')->setTemplateName('crud/field/generic'), FormField::addPanel('seo')->setTemplateName('crud/field/generic'),
} }


public function createIndexQueryBuilder( public function createIndexQueryBuilder(
SearchDto $searchDto,
EntityDto $entityDto,
FieldCollection $fields,
FilterCollection $filters
): QueryBuilder
{
SearchDto $searchDto,
EntityDto $entityDto,
FieldCollection $fields,
FilterCollection $filters
): QueryBuilder {
$queryBuilder = parent::createIndexQueryBuilder( $queryBuilder = parent::createIndexQueryBuilder(
$searchDto,
$entityDto,
$fields,
$filters
$searchDto,
$entityDto,
$fields,
$filters
); );


dump(get_defined_vars());
if ($this->isInstanceOf(TreeInterface::class)) { if ($this->isInstanceOf(TreeInterface::class)) {
$entityId = $searchDto->getRequest()->get('entityId'); $entityId = $searchDto->getRequest()->get('entityId');
if ($entityId !== null) { if ($entityId !== null) {
} }


public function createSortQueryBuilder( public function createSortQueryBuilder(
SearchDto $searchDto,
EntityDto $entityDto,
FieldCollection $fields,
FilterCollection $filters
): QueryBuilder
{
$queryBuilder = parent::createIndexQueryBuilder(
$searchDto,
$entityDto,
$fields,
$filters
);

if ($this->isInstanceOf(TreeInterface::class)) {
$entityId = $searchDto->getRequest()->get('entityId');
if ($entityId !== null) {
$queryBuilder->andWhere('entity.parent = :entityId');
$queryBuilder->setParameter('entityId', $searchDto->getRequest()->get('entityId'));
} else {
$queryBuilder->andWhere('entity.parent IS NULL');
}
}
SearchDto $searchDto,
EntityDto $entityDto,
FieldCollection $fields,
FilterCollection $filters
): QueryBuilder {


$queryBuilder = $this->createIndexQueryBuilder($searchDto,$entityDto,$fields,$filters);


return $queryBuilder; return $queryBuilder;
} }

+ 28
- 14
DependencyInjection/Configuration.php View File

*/ */
class Configuration implements ConfigurationInterface class Configuration implements ConfigurationInterface
{ {
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('lc_sov');
$rootNode = $treeBuilder->getRootNode();
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('lc_sov');
$rootNode = $treeBuilder->getRootNode();


$rootNode
->children()
//->scalarNode('dashboard_default')->defaultValue('App\Controller\Admin\DashboardController')->end()
->scalarNode('homepage_route')->end()
->end();
$rootNode
->children()
//->scalarNode('dashboard_default')->defaultValue('App\Controller\Admin\DashboardController')->end()
->scalarNode('homepage_route')->end()
->arrayNode('login_redirection')
->children()
->scalarNode('redirect_referer')
->defaultValue(true)
->end()
->arrayNode('roles_redirection')
->arrayPrototype()
->children()
->scalarNode('role')->end()
->scalarNode('redirect')->end()
->end()
->end()
->end()
->end()
->end();




return $treeBuilder;
}
return $treeBuilder;
}
} }

+ 91
- 87
Repository/AbstractRepository.php View File

use Doctrine\ORM\Query; use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Gedmo\Translatable\TranslatableListener; use Gedmo\Translatable\TranslatableListener;
use Lc\CaracoleBundle\Doctrine\Extension\FilterMerchantInterface;
use Lc\SovBundle\Doctrine\Extension\StatusInterface; use Lc\SovBundle\Doctrine\Extension\StatusInterface;


abstract class AbstractRepository extends EntityRepository implements ServiceEntityRepositoryInterface, abstract class AbstractRepository extends EntityRepository implements ServiceEntityRepositoryInterface,
AbstractRepositoryInterface AbstractRepositoryInterface
{ {


protected $defaultLocale;
protected $defaultLocale;


public function setDefaultLocale($locale)
{
$this->defaultLocale = $locale;
}
public function setDefaultLocale($locale)
{
$this->defaultLocale = $locale;
}


public function __construct(EntityManagerInterface $entityManager)
{
parent::__construct($entityManager, $entityManager->getClassMetadata($this->getInterfaceClass()));
}
public function __construct(EntityManagerInterface $entityManager)
{
parent::__construct($entityManager, $entityManager->getClassMetadata($this->getInterfaceClass()));
}


public function findOneByOldUrl($url)
{
$qb = $this->createQueryBuilder('entity')
->where('entity.oldUrls LIKE :oldUrl')
->andWhere('entity.status = 1')
->setParameter(':oldUrl', '%'.$url.'%');
public function findOneByOldUrl($url)
{
$qb = $this->createQueryBuilder('entity')
->where('entity.oldUrls LIKE :oldUrl')
->andWhere('entity.status = 1')
->setParameter(':oldUrl', '%'.$url.'%');


return $qb->getQuery()->getOneOrNullResult();
}
return $qb->getQuery()->getOneOrNullResult();
}


public function getOneOrNullResult(QueryBuilder $qb, $locale = null, $hydrationMode = null)
{
return $this->getTranslatedQuery($qb, $locale)->getOneOrNullResult($hydrationMode);
}
public function getOneOrNullResult(QueryBuilder $qb, $locale = null, $hydrationMode = null)
{
return $this->getTranslatedQuery($qb, $locale)->getOneOrNullResult($hydrationMode);
}




public function getResult(QueryBuilder $qb, $locale = null, $hydrationMode = AbstractQuery::HYDRATE_OBJECT)
{
return $this->getTranslatedQuery($qb, $locale)->getResult($hydrationMode);
}
public function getResult(QueryBuilder $qb, $locale = null, $hydrationMode = AbstractQuery::HYDRATE_OBJECT)
{
return $this->getTranslatedQuery($qb, $locale)->getResult($hydrationMode);
}




public function getArrayResult(QueryBuilder $qb, $locale = null)
{
return $this->getTranslatedQuery($qb, $locale)->getArrayResult();
}
public function getArrayResult(QueryBuilder $qb, $locale = null)
{
return $this->getTranslatedQuery($qb, $locale)->getArrayResult();
}




public function getSingleResult(QueryBuilder $qb, $locale = null, $hydrationMode = null)
{
return $this->getTranslatedQuery($qb, $locale)->getSingleResult($hydrationMode);
}
public function getSingleResult(QueryBuilder $qb, $locale = null, $hydrationMode = null)
{
return $this->getTranslatedQuery($qb, $locale)->getSingleResult($hydrationMode);
}




public function getScalarResult(QueryBuilder $qb, $locale = null)
{
return $this->getTranslatedQuery($qb, $locale)->getScalarResult();
}
public function getScalarResult(QueryBuilder $qb, $locale = null)
{
return $this->getTranslatedQuery($qb, $locale)->getScalarResult();
}




public function getSingleScalarResult(QueryBuilder $qb, $locale = null)
{
return $this->getTranslatedQuery($qb, $locale)->getSingleScalarResult();
}
public function getSingleScalarResult(QueryBuilder $qb, $locale = null)
{
return $this->getTranslatedQuery($qb, $locale)->getSingleScalarResult();
}


protected function getTranslatedQuery(QueryBuilder $qb, $locale = null)
{
$locale = null === $locale ? $this->defaultLocale : $locale;
protected function getTranslatedQuery(QueryBuilder $qb, $locale = null)
{
$locale = null === $locale ? $this->defaultLocale : $locale;


$query = $qb->getQuery();
$query = $qb->getQuery();


$query->setHint(
Query::HINT_CUSTOM_OUTPUT_WALKER,
'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'
);
$query->setHint(
Query::HINT_CUSTOM_OUTPUT_WALKER,
'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'
);


$query->setHint(TranslatableListener::HINT_TRANSLATABLE_LOCALE, $locale);
$query->setHint(TranslatableListener::HINT_TRANSLATABLE_LOCALE, $locale);


return $query;
}
return $query;
}


public function findSimilarSlug($merchant)
{
$qb = $this->createQueryBuilder('entity')
->select('entity.id, COUNT(entity.slug) as niche')
->andWhere('entity.status>=0')
->andWhere('entity.merchant= :merchant')
->setParameter('merchant', $merchant)
->groupBy('entity.slug')
->having('COUNT(entity.slug) >1');


public function findAll()
{
return $this->findBy(array());
}
return $qb->getQuery()->getResult();
}


public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
{
$className = $this->getClassMetadata()->getName();
$entity = new $className;


if($entity instanceof StatusInterface){
if (!isset($criteria['status'])) $criteria['status'] = 1;
if ($criteria['status'] === false) unset($criteria['status']);
}
public function findAll()
{
return $this->findBy(array());
}


return parent::findBy($criteria, $orderBy, $limit, $offset);
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
{
$criteria = $this->setCriteria($criteria);


}
return parent::findBy($criteria, $orderBy, $limit, $offset);
}


public function findOneBy(array $criteria, array $orderBy = null)
{
public function findOneBy(array $criteria, array $orderBy = null)
{
$criteria = $this->setCriteria($criteria);


$className = $this->getClassMetadata()->getName();
$entity = new $className;
return parent::findOneBy($criteria, $orderBy);
}


if($entity instanceof StatusInterface){
if (!isset($criteria['status'])) $criteria['status'] = 1;
if ($criteria['status'] === false) unset($criteria['status']);
}
protected function setCriteria(array $criteria) :array
{
$className = $this->getClassMetadata()->getName();
$entity = new $className;


return parent::findOneBy($criteria, $orderBy);
if ($entity instanceof StatusInterface) {
if (!isset($criteria['status'])) {
$criteria['status'] = 1;
}
if ($criteria['status'] === false) {
unset($criteria['status']);
}
} }
return $criteria;
}


public function findSimilarSlug($merchant){
$qb = $this->createQueryBuilder('entity')
->select('entity.id, COUNT(entity.slug) as niche')
->andWhere('entity.status>=0')
->andWhere('entity.merchant= :merchant')
->setParameter('merchant', $merchant)

->groupBy('entity.slug')
->having('COUNT(entity.slug) >1');

return $qb->getQuery()->getResult();
}


} }

Loading…
Cancel
Save