mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Add a list of user groups in User menu, and implements the feature to add / remove users
This commit is contained in:
parent
7bedf1b5b8
commit
479651b31e
177
src/Bundle/ChillMainBundle/Controller/UserGroupController.php
Normal file
177
src/Bundle/ChillMainBundle/Controller/UserGroupController.php
Normal file
@ -0,0 +1,177 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Controller;
|
||||
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Entity\UserGroup;
|
||||
use Chill\MainBundle\Form\Type\PickUserDynamicType;
|
||||
use Chill\MainBundle\Pagination\PaginatorFactoryInterface;
|
||||
use Chill\MainBundle\Repository\UserGroupRepositoryInterface;
|
||||
use Chill\MainBundle\Routing\ChillUrlGeneratorInterface;
|
||||
use Chill\MainBundle\Security\Authorization\UserGroupVoter;
|
||||
use Chill\MainBundle\Templating\Entity\ChillEntityRenderManagerInterface;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
||||
use Symfony\Component\Form\FormFactoryInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use Symfony\Component\Translation\TranslatableMessage;
|
||||
use Twig\Environment;
|
||||
|
||||
/**
|
||||
* Controller to see and manage user groups.
|
||||
*/
|
||||
final readonly class UserGroupController
|
||||
{
|
||||
public function __construct(
|
||||
private UserGroupRepositoryInterface $userGroupRepository,
|
||||
private Security $security,
|
||||
private PaginatorFactoryInterface $paginatorFactory,
|
||||
private Environment $twig,
|
||||
private FormFactoryInterface $formFactory,
|
||||
private ChillUrlGeneratorInterface $chillUrlGenerator,
|
||||
private EntityManagerInterface $objectManager,
|
||||
private ChillEntityRenderManagerInterface $chillEntityRenderManager,
|
||||
) {}
|
||||
|
||||
#[Route('/{_locale}/main/user-groups/my', name: 'chill_main_user_groups_my')]
|
||||
public function myUserGroups(): Response
|
||||
{
|
||||
if (!$this->security->isGranted('ROLE_USER')) {
|
||||
throw new AccessDeniedHttpException();
|
||||
}
|
||||
|
||||
$user = $this->security->getUser();
|
||||
|
||||
if (!$user instanceof User) {
|
||||
throw new AccessDeniedHttpException();
|
||||
}
|
||||
|
||||
$nb = $this->userGroupRepository->countByUser($user);
|
||||
$paginator = $this->paginatorFactory->create($nb);
|
||||
|
||||
$groups = $this->userGroupRepository->findByUser($user, true, $paginator->getItemsPerPage(), $paginator->getCurrentPageFirstItemNumber());
|
||||
$forms = new \SplObjectStorage();
|
||||
|
||||
foreach ($groups as $group) {
|
||||
$forms->attach($group, $this->createFormAppendUserForGroup($group)?->createView());
|
||||
}
|
||||
|
||||
return new Response($this->twig->render('@ChillMain/UserGroup/my_user_groups.html.twig', [
|
||||
'groups' => $groups,
|
||||
'paginator' => $paginator,
|
||||
'forms' => $forms,
|
||||
]));
|
||||
}
|
||||
|
||||
#[Route('/{_locale}/main/user-groups/{id}/append', name: 'chill_main_user_groups_append_users')]
|
||||
public function appendUsersToGroup(UserGroup $userGroup, Request $request, Session $session): Response
|
||||
{
|
||||
if (!$this->security->isGranted(UserGroupVoter::APPEND_TO_GROUP, $userGroup)) {
|
||||
throw new AccessDeniedHttpException();
|
||||
}
|
||||
|
||||
$form = $this->createFormAppendUserForGroup($userGroup);
|
||||
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
foreach ($form['users']->getData() as $user) {
|
||||
$userGroup->addUser($user);
|
||||
|
||||
$session->getFlashBag()->add(
|
||||
'success',
|
||||
new TranslatableMessage(
|
||||
'user_group.user_added',
|
||||
[
|
||||
'user_group' => $this->chillEntityRenderManager->renderString($userGroup, []),
|
||||
'user' => $this->chillEntityRenderManager->renderString($user, []),
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$this->objectManager->flush();
|
||||
|
||||
return new RedirectResponse(
|
||||
$this->chillUrlGenerator->returnPathOr('chill_main_user_groups_my')
|
||||
);
|
||||
}
|
||||
if ($form->isSubmitted()) {
|
||||
$errors = [];
|
||||
foreach ($form->getErrors() as $error) {
|
||||
$errors[] = $error->getMessage();
|
||||
}
|
||||
|
||||
return new Response(implode(', ', $errors));
|
||||
}
|
||||
|
||||
return new RedirectResponse(
|
||||
$this->chillUrlGenerator->returnPathOr('chill_main_user_groups_my')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ParamConverter("user", class=User::class, options={"id" = "userId"})
|
||||
*/
|
||||
#[Route('/{_locale}/main/user-group/{id}/user/{userId}/remove', name: 'chill_main_user_groups_remove_user')]
|
||||
public function removeUserToGroup(UserGroup $userGroup, User $user, Session $session): Response
|
||||
{
|
||||
if (!$this->security->isGranted(UserGroupVoter::APPEND_TO_GROUP, $userGroup)) {
|
||||
throw new AccessDeniedHttpException();
|
||||
}
|
||||
|
||||
$userGroup->removeUser($user);
|
||||
$this->objectManager->flush();
|
||||
|
||||
$session->getFlashBag()->add(
|
||||
'success',
|
||||
new TranslatableMessage(
|
||||
'user_group.user_removed',
|
||||
[
|
||||
'user_group' => $this->chillEntityRenderManager->renderString($userGroup, []),
|
||||
'user' => $this->chillEntityRenderManager->renderString($user, []),
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
return new RedirectResponse(
|
||||
$this->chillUrlGenerator->returnPathOr('chill_main_user_groups_my')
|
||||
);
|
||||
}
|
||||
|
||||
private function createFormAppendUserForGroup(UserGroup $group): ?FormInterface
|
||||
{
|
||||
if (!$this->security->isGranted(UserGroupVoter::APPEND_TO_GROUP, $group)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$builder = $this->formFactory->createBuilder(FormType::class, ['users' => []], [
|
||||
'action' => $this->chillUrlGenerator->generateWithReturnPath('chill_main_user_groups_append_users', ['id' => $group->getId()]),
|
||||
]);
|
||||
$builder->add('users', PickUserDynamicType::class, [
|
||||
'submit_on_adding_new_entity' => true,
|
||||
'label' => 'user_group.append_users',
|
||||
'mapped' => false,
|
||||
'multiple' => true,
|
||||
]);
|
||||
|
||||
return $builder->getForm();
|
||||
}
|
||||
}
|
@ -11,20 +11,34 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\MainBundle\Repository;
|
||||
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Entity\UserGroup;
|
||||
use Chill\MainBundle\Search\SearchApiQuery;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Symfony\Contracts\Translation\LocaleAwareInterface;
|
||||
|
||||
final readonly class UserGroupRepository implements UserGroupRepositoryInterface
|
||||
final class UserGroupRepository implements UserGroupRepositoryInterface, LocaleAwareInterface
|
||||
{
|
||||
private EntityRepository $repository;
|
||||
private readonly EntityRepository $repository;
|
||||
|
||||
private string $locale;
|
||||
|
||||
public function __construct(EntityManagerInterface $em)
|
||||
{
|
||||
$this->repository = $em->getRepository(UserGroup::class);
|
||||
}
|
||||
|
||||
public function setLocale(string $locale): void
|
||||
{
|
||||
$this->locale = $locale;
|
||||
}
|
||||
|
||||
public function getLocale(): string
|
||||
{
|
||||
return $this->locale;
|
||||
}
|
||||
|
||||
public function find($id): ?UserGroup
|
||||
{
|
||||
return $this->repository->find($id);
|
||||
@ -66,4 +80,54 @@ final readonly class UserGroupRepository implements UserGroupRepositoryInterface
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
public function findByUser(User $user, bool $onlyActive = true, ?int $limit = null, ?int $offset = null): array
|
||||
{
|
||||
$qb = $this->buildQueryByUser($user, $onlyActive);
|
||||
|
||||
if (null !== $limit) {
|
||||
$qb->setMaxResults($limit);
|
||||
}
|
||||
|
||||
if (null !== $offset) {
|
||||
$qb->setFirstResult($offset);
|
||||
}
|
||||
|
||||
// ordering thing
|
||||
$qb->addSelect('JSON_EXTRACT(ug.label, :lang) AS HIDDEN label_ordering')
|
||||
->addOrderBy('label_ordering', 'ASC')
|
||||
->setParameter('lang', $this->getLocale());
|
||||
|
||||
return $qb->getQuery()->getResult();
|
||||
}
|
||||
|
||||
public function countByUser(User $user, bool $onlyActive = true): int
|
||||
{
|
||||
$qb = $this->buildQueryByUser($user, $onlyActive);
|
||||
$qb->select('count(ug)');
|
||||
|
||||
return $qb->getQuery()->getSingleScalarResult();
|
||||
}
|
||||
|
||||
private function buildQueryByUser(User $user, bool $onlyActive): \Doctrine\ORM\QueryBuilder
|
||||
{
|
||||
$qb = $this->repository->createQueryBuilder('ug');
|
||||
$qb->where(
|
||||
$qb->expr()->orX(
|
||||
$qb->expr()->isMemberOf(':user', 'ug.users'),
|
||||
$qb->expr()->isMemberOf(':user', 'ug.adminUsers')
|
||||
)
|
||||
);
|
||||
|
||||
$qb->setParameter('user', $user);
|
||||
|
||||
if ($onlyActive) {
|
||||
$qb->andWhere(
|
||||
$qb->expr()->eq('ug.active', ':active')
|
||||
);
|
||||
$qb->setParameter('active', true);
|
||||
}
|
||||
|
||||
return $qb;
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\MainBundle\Repository;
|
||||
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Entity\UserGroup;
|
||||
use Chill\MainBundle\Search\SearchApiQuery;
|
||||
use Doctrine\Persistence\ObjectRepository;
|
||||
@ -24,4 +25,8 @@ interface UserGroupRepositoryInterface extends ObjectRepository
|
||||
* Provide a SearchApiQuery for searching amongst user groups.
|
||||
*/
|
||||
public function provideSearchApiQuery(string $pattern, string $lang, string $selectKey = 'user-group'): SearchApiQuery;
|
||||
|
||||
public function findByUser(User $user, bool $onlyActive = true, ?int $limit = null, ?int $offset = null): array;
|
||||
|
||||
public function countByUser(User $user, bool $onlyActive = true): int;
|
||||
}
|
||||
|
@ -0,0 +1,160 @@
|
||||
{% extends '@ChillMain/layout.html.twig' %}
|
||||
|
||||
{% block css %}
|
||||
{{ parent() }}
|
||||
{{ encore_entry_link_tags('mod_pickentity_type') }}
|
||||
|
||||
<style type="text/css">
|
||||
form.remove {
|
||||
display: inline-block;
|
||||
padding: 1px;
|
||||
border: 1px solid transparent;
|
||||
border-radius: 4px;
|
||||
}
|
||||
form:hover {
|
||||
animation-duration: 0.5s;
|
||||
animation-name: onHover;
|
||||
animation-iteration-count: 1;
|
||||
border: 1px solid #dee2e6;
|
||||
border-radius: 4px;
|
||||
}
|
||||
form.remove button.remove {
|
||||
display: inline;
|
||||
background-color: unset;
|
||||
border: none;
|
||||
color: var(--bs-chill-red);
|
||||
}
|
||||
|
||||
@keyframes onHover {
|
||||
from {
|
||||
border: 1px solid transparent;
|
||||
}
|
||||
to {
|
||||
border: 1px solid #dee2e6;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
{% endblock %}
|
||||
|
||||
{% block js %}
|
||||
{{ parent() }}
|
||||
{{ encore_entry_script_tags('mod_pickentity_type') }}
|
||||
{% endblock %}
|
||||
|
||||
{% block title 'user_group.my_groups'|trans %}
|
||||
|
||||
{% block content %}
|
||||
<h1>{{ block('title') }}</h1>
|
||||
|
||||
{% if paginator.totalItems == 0 %}
|
||||
<p>{{ 'user_group.no_user_groups'|trans }}</p>
|
||||
{% else %}
|
||||
<div class="flex-table">
|
||||
{% for entity in groups %}
|
||||
<div class="item-bloc">
|
||||
<div class="item-row">
|
||||
<div class="wrap-header">
|
||||
<div class="wh-row">
|
||||
<div class="wh-col">
|
||||
{{ entity|chill_entity_render_box }}
|
||||
</div>
|
||||
<div class="wh-col">
|
||||
{%- if not entity.active -%}
|
||||
<div>
|
||||
<span class="badge bg-danger">{{ 'user_group.inactive'|trans }}</span>
|
||||
</div>
|
||||
{%- endif -%}
|
||||
<div>{{ 'user_group.with_count_users'|trans({'count': entity.users|length}) }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="item-row separator">
|
||||
<div class="wrap-list">
|
||||
<div class="wl-row">
|
||||
<div class="wl-col title">
|
||||
<strong>{{ 'user_group.with_users'|trans }}</strong>
|
||||
</div>
|
||||
<div class="wl-col list">
|
||||
{% if entity.users.contains(app.user) %}
|
||||
{% if is_granted('CHILL_MAIN_USER_GROUP_APPEND_TO_GROUP', entity) %}
|
||||
<form class="remove" method="POST" action="{{ chill_path_add_return_path('chill_main_user_groups_remove_user', {'id': entity.id, 'userId': app.user.id}) }}">
|
||||
<p class="wl-item">
|
||||
{{ 'user_group.me'|trans }}
|
||||
<button class="remove" type="submit"><i class="fa fa-times"></i></button>
|
||||
</p>
|
||||
</form>
|
||||
{% else %}
|
||||
<p class="wl-item">
|
||||
{% if entity.users|length > 1 %}{{ 'user_group.me_and'|trans }}{% else %}{{ 'user_group.me_only'|trans }}{% endif %}
|
||||
</p>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
{% for user in entity.userListByLabelAscending %}
|
||||
{% if user is not same as app.user %}
|
||||
{% if is_granted('CHILL_MAIN_USER_GROUP_APPEND_TO_GROUP', entity) %}
|
||||
<form class="remove" method="POST" action="{{ chill_path_add_return_path('chill_main_user_groups_remove_user', {'id': entity.id, 'userId': user.id}) }}">
|
||||
<p class="wl-item">
|
||||
<span class="badge-user">
|
||||
{{ user|chill_entity_render_box }}
|
||||
</span>
|
||||
<button class="remove" type="submit"><i class="fa fa-times"></i></button>
|
||||
</p>
|
||||
</form>
|
||||
{% else %}
|
||||
<p class="wl-item">
|
||||
<span class="badge-user">
|
||||
{{ user|chill_entity_render_box }}
|
||||
</span>
|
||||
</p>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% if entity.adminUsers|length > 0 %}
|
||||
<div class="item-row separator">
|
||||
<div class="wrap-list">
|
||||
<div class="wl-row">
|
||||
<div class="wl-col title">
|
||||
<strong>{{ 'user_group.adminUsers'|trans }}</strong>
|
||||
</div>
|
||||
<div class="wl-col list">
|
||||
{% if entity.adminUsers.contains(app.user) %}
|
||||
<p class="wl-item">{% if entity.adminUsers|length > 1 %}{{ 'user_group.me_and'|trans }}{% else %}{{ 'user_group.me_only'|trans }}{% endif %}</p>
|
||||
{% endif %}
|
||||
{% for user in entity.adminUserListByLabelAscending %}
|
||||
{% if user is not same as app.user %}
|
||||
<p class="wl-item">
|
||||
<span class="badge-user">
|
||||
{{ user|chill_entity_render_box }}
|
||||
</span>
|
||||
</p>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endif -%}
|
||||
{%- set form = forms.offsetGet(entity) %}
|
||||
{%- if form is not null -%}
|
||||
<div class="item-row separator">
|
||||
<ul class="record_actions slim">
|
||||
<li>
|
||||
{{- form_start(form) -}}
|
||||
{{- form_widget(form.users) -}}
|
||||
{{- form_end(form) -}}
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
{%- endif %}
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
||||
{{ chill_pagination(paginator) }}
|
||||
{% endif %}
|
||||
{% endblock %}
|
@ -49,19 +49,19 @@
|
||||
|
||||
{% for flashMessage in app.session.flashbag.get('success') %}
|
||||
<div class="alert alert-success flash_message">
|
||||
<span>{{ flashMessage|raw }}</span>
|
||||
<span>{{ flashMessage|trans }}</span>
|
||||
</div>
|
||||
{% endfor %}
|
||||
|
||||
{% for flashMessage in app.session.flashbag.get('error') %}
|
||||
<div class="alert alert-danger flash_message">
|
||||
<span>{{ flashMessage|raw }}</span>
|
||||
<span>{{ flashMessage|trans }}</span>
|
||||
</div>
|
||||
{% endfor %}
|
||||
|
||||
{% for flashMessage in app.session.flashbag.get('notice') %}
|
||||
<div class="alert alert-warning flash_message">
|
||||
<span>{{ flashMessage|raw }}</span>
|
||||
<span>{{ flashMessage|trans }}</span>
|
||||
</div>
|
||||
{% endfor %}
|
||||
|
||||
|
@ -60,7 +60,6 @@ class UserMenuBuilder implements LocalMenuBuilderInterface
|
||||
|
||||
$nbNotifications = $this->notificationByUserCounter->countUnreadByUser($user);
|
||||
|
||||
// TODO add an icon? How exactly? For example a clock icon...
|
||||
$menu
|
||||
->addChild($this->translator->trans('absence.Set absence date'), [
|
||||
'route' => 'chill_main_user_absence_index',
|
||||
@ -69,6 +68,14 @@ class UserMenuBuilder implements LocalMenuBuilderInterface
|
||||
'order' => -8_888_888,
|
||||
]);
|
||||
|
||||
$menu
|
||||
->addChild($this->translator->trans('user_group.my_groups'), [
|
||||
'route' => 'chill_main_user_groups_my',
|
||||
])
|
||||
->setExtras([
|
||||
'order' => -7_777_777,
|
||||
]);
|
||||
|
||||
$menu
|
||||
->addChild(
|
||||
$this->translator->trans('notification.My notifications with counter', ['nb' => $nbNotifications]),
|
||||
|
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Security\Authorization;
|
||||
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Entity\UserGroup;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
|
||||
final class UserGroupVoter extends Voter
|
||||
{
|
||||
public const APPEND_TO_GROUP = 'CHILL_MAIN_USER_GROUP_APPEND_TO_GROUP';
|
||||
|
||||
public function __construct(private readonly Security $security) {}
|
||||
|
||||
protected function supports(string $attribute, $subject)
|
||||
{
|
||||
return self::APPEND_TO_GROUP === $attribute && $subject instanceof UserGroup;
|
||||
}
|
||||
|
||||
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token)
|
||||
{
|
||||
/* @var UserGroup $subject */
|
||||
if ($this->security->isGranted('ROLE_ADMIN')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$user = $this->security->getUser();
|
||||
|
||||
if (!$user instanceof User) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $subject->getAdminUsers()->contains($user);
|
||||
}
|
||||
}
|
@ -13,6 +13,8 @@ user_group:
|
||||
many {# utilisateurs}
|
||||
other {# utilisateurs}
|
||||
}
|
||||
user_removed: L'utilisateur {user} est enlevé du groupe {user_group} avec succès
|
||||
user_added: L'utilisateur {user} est ajouté groupe {user_group} avec succès
|
||||
|
||||
notification:
|
||||
My notifications with counter: >-
|
||||
|
@ -53,7 +53,7 @@ user:
|
||||
|
||||
user_group:
|
||||
inactive: Inactif
|
||||
with_users: Associé aux utilisateurs
|
||||
with_users: Membres
|
||||
no_users: Aucun utilisateur associé
|
||||
no_admin_users: Aucun administrateur
|
||||
Label: Nom du groupe
|
||||
@ -64,6 +64,11 @@ user_group:
|
||||
Users: Membres du groupe
|
||||
adminUsers: Administrateurs du groupe
|
||||
adminUsersHelp: Les administrateurs du groupe peuvent ajouter ou retirer des membres dans le groupe.
|
||||
my_groups: Mes groupes
|
||||
me_and: Moi et
|
||||
me_only: Uniquement moi
|
||||
me: Moi
|
||||
append_users: Ajouter des utilisateurs
|
||||
|
||||
inactive: inactif
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user