Merge branch 'fix-person-tests' into features/household

This commit is contained in:
2021-05-31 21:40:24 +02:00
51 changed files with 1410 additions and 589 deletions

View File

@@ -0,0 +1,91 @@
<?php
/*
* Copyright (C) 2015 Champs-Libres Coopérative <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\MainBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Chill\MainBundle\Timeline\TimelineBuilder;
use Chill\MainBundle\Pagination\PaginatorFactory;
use Chill\PersonBundle\Security\Authorization\PersonVoter;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Symfony\Component\Security\Core\Role\Role;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
class TimelineCenterController extends AbstractController
{
protected TimelineBuilder $timelineBuilder;
protected PaginatorFactory $paginatorFactory;
private Security $security;
public function __construct(
TimelineBuilder $timelineBuilder,
PaginatorFactory $paginatorFactory,
Security $security
) {
$this->timelineBuilder = $timelineBuilder;
$this->paginatorFactory = $paginatorFactory;
$this->security = $security;
}
/**
* @Route("/{_locale}/center/timeline",
* name="chill_center_timeline",
* methods={"GET"}
* )
*/
public function centerAction(Request $request)
{
// collect reachable center for each group
$user = $this->security->getUser();
$centers = [];
foreach ($user->getGroupCenters() as $group) {
$centers[] = $group->getCenter();
}
if (0 === count($centers)) {
throw $this->createNotFoundException();
}
$nbItems = $this->timelineBuilder->countItems('center',
[ 'centers' => $centers ]
);
$paginator = $this->paginatorFactory->create($nbItems);
return $this->render('@ChillMain/Timeline/index.html.twig', array
(
'timeline' => $this->timelineBuilder->getTimelineHTML(
'center',
[ 'centers' => $centers ],
$paginator->getCurrentPage()->getFirstItemNumber(),
$paginator->getItemsPerPage()
),
'nb_items' => $nbItems,
'paginator' => $paginator
)
);
}
}

View File

@@ -0,0 +1,7 @@
<div class="timeline">
{% for result in results %}
<div class="timeline-item {% if loop.index0 is even %}even{% else %}odd{% endif %}">
{% include result.template with result.template_data %}
</div>
{% endfor %}
</div>

View File

@@ -1,7 +1,15 @@
<div class="timeline">
{% for result in results %}
<div class="timeline-item {% if loop.index0 is even %}even{% else %}odd{% endif %}">
{% include result.template with result.template_data %}
</div>
{% endfor %}
</div>
{% extends "@ChillMain/layout.html.twig" %}
{% block content %}
<div id="container content">
<div class="grid-8 centered">
<h1>{{ 'Global timeline'|trans }}</h1>
{{ timeline|raw }}
{% if nb_items > paginator.getItemsPerPage %}
{{ chill_pagination(paginator) }}
{% endif %}
</div>
</div>
{% endblock content %}

View File

@@ -68,6 +68,14 @@ class SectionMenuBuilder implements LocalMenuBuilderInterface
'icons' => ['home'],
'order' => 0
]);
$menu->addChild($this->translator->trans('Global timeline'), [
'route' => 'chill_center_timeline',
])
->setExtras([
'order' => 10
]
);
if ($this->authorizationChecker->isGranted(ChillExportVoter::EXPORT)) {
$menu->addChild($this->translator->trans('Export Menu'), [

View File

@@ -110,8 +110,6 @@ class AuthorizationHelper
return false;
}
$role = ($attribute instanceof Role) ? $attribute : new Role($attribute);
foreach ($user->getGroupCenters() as $groupCenter){
//filter on center
if ($groupCenter->getCenter()->getId() === $entity->getCenter()->getId()) {
@@ -119,8 +117,7 @@ class AuthorizationHelper
//iterate on roleScopes
foreach($permissionGroup->getRoleScopes() as $roleScope) {
//check that the role allow to reach the required role
if ($this->isRoleReached($role,
new Role($roleScope->getRole()))){
if ($this->isRoleReached($attribute, $roleScope->getRole())) {
//if yes, we have a right on something...
// perform check on scope if necessary
if ($entity instanceof HasScopeInterface) {
@@ -149,12 +146,15 @@ class AuthorizationHelper
* and optionnaly Scope
*
* @param User $user
* @param Role $role
* @param string|Role $role
* @param null|Scope $scope
* @return Center[]
*/
public function getReachableCenters(User $user, Role $role, Scope $scope = null)
public function getReachableCenters(User $user, $role, Scope $scope = null)
{
if ($role instanceof Role) {
$role = $role->getRole();
}
$centers = array();
foreach ($user->getGroupCenters() as $groupCenter){
@@ -162,8 +162,7 @@ class AuthorizationHelper
//iterate on roleScopes
foreach($permissionGroup->getRoleScopes() as $roleScope) {
//check that the role is in the reachable roles
if ($this->isRoleReached($role,
new Role($roleScope->getRole()))) {
if ($this->isRoleReached($role, $roleScope->getRole())) {
if ($scope === null) {
$centers[] = $groupCenter->getCenter();
break 1;
@@ -180,6 +179,30 @@ class AuthorizationHelper
return $centers;
}
/**
* Filter an array of centers, return only center which are reachable
*
* @param User $user The user
* @param array $centers a list of centers which are going to be filtered
* @param string|Center $role
*/
public function filterReachableCenters(User $user, array $centers, $role): array
{
$results = [];
if ($role instanceof Role) {
$role = $role->getRole();
}
foreach ($centers as $center) {
if ($this->userCanReachCenter($user, $center, $role)) {
$results[] = $center;
}
}
return $results;
}
/**
* Return all reachable scope for a given user, center and role
@@ -191,8 +214,12 @@ class AuthorizationHelper
* @param Center $center
* @return Scope[]
*/
public function getReachableScopes(User $user, Role $role, Center $center)
public function getReachableScopes(User $user, $role, Center $center)
{
if ($role instanceof Role) {
$role = $role->getRole();
}
return $this->getReachableCircles($user, $role, $center);
}
@@ -200,12 +227,15 @@ class AuthorizationHelper
* Return all reachable circle for a given user, center and role
*
* @param User $user
* @param Role $role
* @param string|Role $role
* @param Center $center
* @return Scope[]
*/
public function getReachableCircles(User $user, Role $role, Center $center)
public function getReachableCircles(User $user, $role, Center $center)
{
if ($role instanceof Role) {
$role = $role->getRole();
}
$scopes = array();
foreach ($user->getGroupCenters() as $groupCenter){
@@ -215,9 +245,7 @@ class AuthorizationHelper
//iterate on roleScopes
foreach($permissionGroup->getRoleScopes() as $roleScope) {
//check that the role is in the reachable roles
if ($this->isRoleReached($role,
new Role($roleScope->getRole()))) {
if ($this->isRoleReached($role, $roleScope->getRole())) {
$scopes[] = $roleScope->getScope();
}
}
@@ -269,10 +297,10 @@ class AuthorizationHelper
* @param Role $parentRole The role which should give access to $childRole
* @return boolean true if the child role is granted by parent role
*/
protected function isRoleReached(Role $childRole, Role $parentRole)
protected function isRoleReached($childRole, $parentRole)
{
$reachableRoles = $this->roleHierarchy
->getReachableRoles([$parentRole]);
->getReachableRoleNames([$parentRole]);
return in_array($childRole, $reachableRoles);
}

View File

@@ -23,11 +23,11 @@ use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Doctrine\ORM\Query;
use Doctrine\ORM\NativeQuery;
/**
* Build timeline
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class TimelineBuilder implements ContainerAwareInterface
{
@@ -78,14 +78,14 @@ class TimelineBuilder implements ContainerAwareInterface
*/
public function getTimelineHTML($context, array $args, $firstItem = 0, $number = 20)
{
$union = $this->buildUnionQuery($context, $args);
list($union, $parameters) = $this->buildUnionQuery($context, $args);
//add ORDER BY clause and LIMIT
$query = $union . sprintf(' ORDER BY date DESC LIMIT %d OFFSET %d',
$number, $firstItem);
// run query and handle results
$fetched = $this->runUnionQuery($query);
$fetched = $this->runUnionQuery($query, $parameters);
$entitiesByKey = $this->getEntities($fetched, $context);
return $this->render($fetched, $entitiesByKey, $context, $args);
@@ -100,16 +100,18 @@ class TimelineBuilder implements ContainerAwareInterface
*/
public function countItems($context, array $args)
{
$union = $this->buildUnionQuery($context, $args);
// embed the union query inside a count query
$count = sprintf('SELECT COUNT(sq.id) AS total FROM (%s) as sq', $union);
$rsm = (new ResultSetMapping())
->addScalarResult('total', 'total', Type::INTEGER);
list($select, $parameters) = $this->buildUnionQuery($context, $args);
// embed the union query inside a count query
$countQuery = sprintf('SELECT COUNT(sq.id) AS total FROM (%s) as sq', $select);
$nq = $this->em->createNativeQuery($countQuery, $rsm);
$nq->setParameters($parameters);
return $this->em->createNativeQuery($count, $rsm)
->getSingleScalarResult();
return $nq->getSingleScalarResult();
}
/**
@@ -154,40 +156,59 @@ class TimelineBuilder implements ContainerAwareInterface
*
* @uses self::buildSelectQuery to build individual SELECT queries
*
* @param string $context
* @param mixed $args
* @param int $page
* @param int $number
* @return string
* @throws \LogicException if no builder have been defined for this context
* @return array, where first element is the query, the second one an array with the parameters
*/
private function buildUnionQuery($context, array $args)
private function buildUnionQuery(string $context, array $args): array
{
//append SELECT queries with UNION keyword between them
$union = '';
$parameters = [];
foreach($this->getProvidersByContext($context) as $provider) {
$select = $this->buildSelectQuery($provider, $context, $args);
$append = ($union === '') ? $select : ' UNION '.$select;
$data = $provider->fetchQuery($context, $args);
list($select, $selectParameters) = $this->buildSelectQuery($data);
$append = empty($union) ? $select : ' UNION '.$select;
$union .= $append;
$parameters = array_merge($parameters, $selectParameters);
}
return $union;
return [$union, $parameters];
}
/**
* Hack to replace the arbitrary "AS" statement in DQL
* into proper SQL query
* TODO remove
private function replaceASInDQL(string $dql): string
{
$pattern = '/^(SELECT\s+[a-zA-Z0-9\_\.\']{1,}\s+)(AS [a-z0-9\_]{1,})(\s{0,},\s{0,}[a-zA-Z0-9\_\.\']{1,}\s+)(AS [a-z0-9\_]{1,})(\s{0,},\s{0,}[a-zA-Z0-9\_\.\']{1,}\s+)(AS [a-z0-9\_]{1,})(\s+FROM.*)/';
$replacements = '${1} AS id ${3} AS type ${5} AS date ${7}';
$s = \preg_replace($pattern, $replacements, $dql, 1);
if (NULL === $s) {
throw new \RuntimeException('Could not replace the "AS" statement produced by '.
'DQL with normal SQL AS: '.$dql);
}
return $s;
}
*/
/**
* return the SQL SELECT query as a string,
*
* @uses TimelineProfiderInterface::fetchQuery use the fetchQuery function
* @param \Chill\MainBundle\Timeline\TimelineProviderInterface $provider
* @param string $context
* @param mixed[] $args
* @return string
* @return array: first parameter is the sql string, second an array with parameters
*/
private function buildSelectQuery(TimelineProviderInterface $provider, $context, array $args)
private function buildSelectQuery($data): array
{
$data = $provider->fetchQuery($context, $args);
return sprintf(
return [$data->buildSql(), $data->getParameters()];
// dead code
$parameters = [];
$sql = sprintf(
'SELECT %s AS id, '
. '%s AS "date", '
. "'%s' AS type "
@@ -197,16 +218,19 @@ class TimelineBuilder implements ContainerAwareInterface
$data['date'],
$data['type'],
$data['FROM'],
$data['WHERE']);
is_string($data['WHERE']) ? $data['WHERE'] : $data['WHERE'][0]
);
return [$sql, $data['WHERE'][1]];
}
/**
* run the UNION query and return result as an array
*
* @param string $query
* @return array
* @return array an array with the results
*/
private function runUnionQuery($query)
private function runUnionQuery(string $query, array $parameters): array
{
$resultSetMapping = (new ResultSetMapping())
->addScalarResult('id', 'id')
@@ -214,7 +238,8 @@ class TimelineBuilder implements ContainerAwareInterface
->addScalarResult('date', 'date');
return $this->em->createNativeQuery($query, $resultSetMapping)
->getArrayResult();
->setParameters($parameters)
->getArrayResult();
}
/**
@@ -274,7 +299,7 @@ class TimelineBuilder implements ContainerAwareInterface
}
return $this->container->get('templating')
->render('@ChillMain/Timeline/index.html.twig', array(
->render('@ChillMain/Timeline/chain_timelines.html.twig', array(
'results' => $timelineEntries
));

View File

@@ -0,0 +1,155 @@
<?php
namespace Chill\MainBundle\Timeline;
class TimelineSingleQuery
{
private ?string $id;
private ?string $date;
private ?string $key;
private ?string $from;
private ?string $where;
private array $parameters = [];
private bool $distinct = false;
public function __construct(
string $id = null,
string $date = null,
string $key = null,
string $from = null,
string $where = null,
array $parameters = []
) {
$this->id = $id;
$this->date = $date;
$this->key = $key;
$this->from = $from;
$this->where = $where;
$this->parameters = $parameters;
}
public static function fromArray(array $a)
{
return new TimelineSingleQuery(
$a['id'] ?? null,
$a['date'] ?? null,
$a['type'] ?? $a['key'] ?? null,
$a['FROM'] ?? $a['from'] ?? null,
$a['WHERE'] ?? $a['where'] ?? null,
$a['parameters'] ?? null);
}
public function getId(): string
{
return $this->id;
}
public function setId(string $id): self
{
$this->id = $id;
return $this;
}
public function getDate(): string
{
return $this->date;
}
public function setDate(string $date): self
{
$this->date = $date;
return $this;
}
public function getKey(): string
{
return $this->key;
}
public function setKey(string $key): self
{
$this->key = $key;
return $this;
}
public function getFrom(): string
{
return $this->from;
}
public function setFrom(string $from): self
{
$this->from = $from;
return $this;
}
public function getWhere(): string
{
return $this->where;
}
public function setWhere(string $where): self
{
$this->where = $where;
return $this;
}
public function getParameters(): array
{
return $this->parameters;
}
public function setParameters(array $parameters): self
{
$this->parameters = $parameters;
return $this;
}
public function setDistinct(bool $distinct): self
{
$this->distinct = $distinct;
return $this;
}
public function isDistinct(): bool
{
return $this->distinct;
}
public function buildSql(): string
{
$parameters = [];
$sql = \strtr(
'SELECT {distinct} {id} AS id, '
. '{date} AS "date", '
. "'{key}' AS type "
. 'FROM {from} '
. 'WHERE {where}',
[
'{distinct}' => $this->distinct ? 'DISTINCT' : '',
'{id}' => $this->getId(),
'{date}' => $this->getDate(),
'{key}' => $this->getKey(),
'{from}' => $this->getFrom(),
'{where}' => $this->getWhere(),
]
);
return $sql;
}
}

View File

@@ -1,3 +1,7 @@
chill_main_controllers:
resource: '../Controller/'
type: annotation
chill_main_admin_permissionsgroup:
resource: "@ChillMainBundle/config/routes/permissionsgroup.yaml"
prefix: "{_locale}/admin/permissionsgroup"

View File

@@ -4,4 +4,7 @@ services:
arguments:
- "@doctrine.orm.entity_manager"
calls:
- [ setContainer, ["@service_container"]]
- [ setContainer, ["@service_container"]]
# alias:
Chill\MainBundle\Timeline\TimelineBuilder: '@chill_main.timeline_builder'

View File

@@ -46,6 +46,11 @@ Back to the list: Retour à la liste
#interval
Years: Années
# misc date
Since %date%: Depuis le %date%
since %date%: depuis le %date%
Until %date%: Jusqu'au %date%
until %date%: jusqu'au %date%
#elements used in software
centers: centres
Centers: Centres
@@ -78,6 +83,9 @@ Results %start%-%end% of %total%: Résultats %start%-%end% sur %total%
See all results: Voir tous les résultats
Advanced search: Recherche avancée
# timeline
Global timeline: Historique global
#admin
Create: Créer
show: voir