wip: synchro

This commit is contained in:
2022-05-07 02:22:28 +02:00
parent 9935af0497
commit 811798e23f
14 changed files with 315 additions and 43 deletions

View File

@@ -43,9 +43,9 @@ class User implements AdvancedUserInterface
/**
* Array where SAML attributes's data are stored.
*
* @ORM\Column(type="json", nullable=true)
* @ORM\Column(type="json", nullable=false)
*/
private array $attributes;
private array $attributes = [];
/**
* @ORM\ManyToOne(targetEntity=Location::class)

View File

@@ -15,6 +15,7 @@ use Chill\MainBundle\Entity\GroupCenter;
use Chill\MainBundle\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\ORM\Query\ResultSetMappingBuilder;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ObjectRepository;
@@ -168,19 +169,29 @@ final class UserRepository implements ObjectRepository
$rsm = new ResultSetMappingBuilder($this->entityManager);
$rsm->addRootEntityFromClassMetadata(User::class, 'u');
$sql = "SELECT ".$rsm->generateSelectClause()." FROM users u WHERE NOT attributes ? :key OR attributes IS NULL AND enabled IS TRUE";
$sql = "SELECT ".$rsm->generateSelectClause()." FROM users u WHERE NOT attributes ?? :key OR attributes IS NULL AND enabled IS TRUE";
if (null !== $limit) {
$sql .= " LIMIT $limit";
}
if (null !== $offset) {
$sql .= " OFFET $offset";
$sql .= " OFFSET $offset";
}
return $this->entityManager->createNativeQuery($sql, $rsm)->setParameter(':key', $key)->getResult();
}
public function countByNotHavingAttribute(string $key): int
{
$rsm = new ResultSetMapping();
$rsm->addScalarResult('count', 'count');
$sql = "SELECT count(*) FROM users u WHERE NOT attributes ?? :key OR attributes IS NULL AND enabled IS TRUE";
return $this->entityManager->createNativeQuery($sql, $rsm)->setParameter(':key', $key)->getSingleScalarResult();
}
public function getClassName()
{
return User::class;

View File

@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Chill\Migrations\Main;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20220506223243 extends AbstractMigration
{
public function getDescription(): string
{
return 'Force user attribute to be an array';
}
public function up(Schema $schema): void
{
$this->addSql('UPDATE users SET attributes = \'{}\'::jsonb WHERE attributes IS NULL');
$this->addSql('ALTER TABLE users ALTER attributes SET NOT NULL');
$this->addSql('ALTER TABLE users ALTER attributes SET DEFAULT \'{}\'::jsonb');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE users ALTER attributes DROP NOT NULL');
}
}