All checks were successful
Cadoles/nineskeletor/pipeline/pr-master This commit looks good
100 lines
3.3 KiB
PHP
100 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Repository;
|
|
|
|
use App\Entity\Group;
|
|
use App\Entity\UserGroup;
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
|
use Doctrine\Persistence\ManagerRegistry;
|
|
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;
|
|
}
|
|
}
|