nineskeletor/src/Repository/GroupRepository.php

99 lines
3.2 KiB
PHP
Raw Normal View History

2022-07-21 16:15:47 +02:00
<?php
namespace App\Repository;
use App\Entity\Group;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Common\Collections\ArrayCollection;
use App\Entity\UserGroup;
use Ramsey\Uuid\Uuid;
class GroupRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Group::class);
}
public function add(Group $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function remove(Group $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
/* Déterminer les groupes d'un user SSO en fonction de ses attributs */
public function calculateSSOGroup($user,$attruser)
{
$groups = $this->_em->getRepository('App\Entity\Group')->findAll();
$retgroups= new ArrayCollection();
foreach($groups as $group) {
if($group->getAttributes()) {
$attgroup=json_decode($group->getAttributes(),true);
foreach($attgroup as $key => $value) {
if(array_key_exists($key,$attruser)) {
if(is_array($attruser[$key])) {
foreach($attruser[$key] as $val) {
if($value=="*")
$retgroups->add($group);
elseif($val==$value)
$retgroups->add($group);
}
}
else {
if($value=="*")
$retgroups->add($group);
elseif($value==$attruser[$key])
$retgroups->add($group);
}
}
}
}
}
// Pour chaque groupe de l'utilisateur
$usergroups=$user->getGroups();
// On le détache des groupes auxquelles il n'appartient plus
if($usergroups) {
foreach($usergroups as $usergroup) {
if($usergroup->getGroup()->getAttributes()!="") {
if(!$retgroups->contains($usergroup->getGroup())) {
$user->removeGroup($usergroup);
}
}
}
}
// On attache le user aux groupes
foreach($retgroups as $retgroup) {
$usergroup=$this->_em->getRepository('App\Entity\UserGroup')->findBy(["user"=>$user,"group"=>$retgroup]);
if(!$usergroup) {
$usergroup=new UserGroup();
$usergroup->setUser($user);
$usergroup->setGroup($retgroup);
$usergroup->setApikey(Uuid::uuid4());
$usergroup->setRolegroup(0);
$this->_em->persist($usergroup);
$this->_em->flush();
}
}
return $user;
}
}