<?php
namespace Acme\SudcmsBundle\Repository;
use Acme\SudcmsBundle\Entity\AuthUser;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @method AuthUser|null find($id, $lockMode = null, $lockVersion = null)
* @method AuthUser|null findOneBy(array $criteria, array $orderBy = null)
* @method AuthUser[] findAll()
* @method AuthUser[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class AuthUserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface {
public function __construct(ManagerRegistry $registry) {
parent::__construct($registry, AuthUser::class);
}
/**
* Used to upgrade (rehash) the user's password automatically over time.
*/
public function upgradePassword(UserInterface $user, string $newEncodedPassword): void {
if (!$user instanceof AuthUser) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', \get_class($user)));
}
$user->setPassword($newEncodedPassword);
$this->_em->persist($user);
$this->_em->flush();
}
public function getUserGroups($userId = 0) {
$conn = $this->getEntityManager()->getConnection();
$sql = "
SELECT *
FROM auth_user
JOIN auth_group_user
ON auth_user.id = auth_group_user.agu_user_id
JOIN auth_group_module
ON auth_group_module.agm_group_id = auth_group_user.agu_ag_id
WHERE auth_user.id = " . $userId . "
";
$stmt = $conn->prepare($sql);
$resultSet = $stmt->executeQuery();
$resultSet->fetchAllAssociative();
$roles = [];
if (is_array($resultSet) && sizeof($resultSet) > 0) {
$roles[] = $resultSet["ag_role"];
}
return $roles;
}
public function getAllGroupsUsers() {
$conn = $this->getEntityManager()->getConnection();
$sql = "
SELECT *
FROM auth_group
";
$stmt = $conn->prepare($sql);
$resultSet = $stmt->executeQuery();
return $resultSet->fetchAllAssociative();
}
public function getGroupsUser($userId = 0) {
$conn = $this->getEntityManager()->getConnection();
$sql = "
SELECT *
FROM auth_group_user
JOIN auth_group_module
ON auth_group_module.agm_group_id = auth_group_user.agu_ag_id
WHERE auth_group_user.agu_user_id = " . $userId . "
";
$stmt = $conn->prepare($sql);
$resultSet = $stmt->executeQuery();
return $resultSet->fetchAllAssociative();
}
public function getModulesUser($groupList = [], $site_uid = 0) {
$conn = $this->getEntityManager()->getConnection();
$sql = "
SELECT *
FROM auth_group_module
JOIN module ON module.id = auth_group_module.agm_module_id
JOIN site_module ON site_module.module_id = module.id
WHERE auth_group_module.agm_group_id IN (" . implode(",", $groupList) . ")
AND site_module.site_id = " . $site_uid . "
GROUP BY module.id
ORDER BY module.mod_ordre ASC
";
$stmt = $conn->prepare($sql);
$resultSet = $stmt->executeQuery();
return $resultSet->fetchAllAssociative();
}
public function findUsersByRole(?string $role) {
$query = $this->createQueryBuilder('u')
->orderBy('u.firstName', 'ASC');
if ($role) {
$query->andWhere('u.roles LIKE :val')
->setParameter('val', '%' . $role . '%');
}
return $query->getQuery()->getResult();
}
}