Add a list of user groups in User menu, and implements the feature to add / remove users

This commit is contained in:
2024-10-01 16:09:44 +02:00
parent 7bedf1b5b8
commit 479651b31e
9 changed files with 473 additions and 7 deletions

View File

@@ -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);
}
}