1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys UserBundle package.
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Rapsys\UserBundle\Repository
;
14 use Doctrine\ORM\Query\ResultSetMapping
;
16 use Rapsys\UserBundle\Repository
;
18 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
19 use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface
;
20 use Symfony\Component\Security\Core\User\PasswordUpgraderInterface
;
25 class UserRepository
extends Repository
implements PasswordUpgraderInterface
{
27 * Find user count as int
29 * @return integer The keywords count
31 public function findCountAsInt(): int {
34 SELECT COUNT(u.id) AS count
35 FROM RapsysUserBundle:User AS u
38 //Get result set mapping instance
39 $req = $this->replace($req);
41 //Get result set mapping instance
42 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
43 $rsm = new ResultSetMapping();
46 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
47 //addScalarResult($sqlColName, $resColName, $type = 'string');
48 $rsm->addScalarResult('count', 'count', 'integer');
52 ->createNativeQuery($req, $rsm)
53 ->getSingleScalarResult();
57 * Find all users grouped by translated group
59 * @param integer $page The page
60 * @param integer $count The count
61 * @return array The user keyed by group and id
63 public function findIndexByGroupId(int $page, int $count): array {
79 IFNULL(g.id, 0) AS g_id,
80 IFNULL(g.title, :defaultgroup) AS g_title
81 FROM RapsysUserBundle:User AS u
82 LEFT JOIN RapsysUserBundle:UserGroup AS gu ON (gu.user_id = u.id)
83 LEFT JOIN RapsysUserBundle:Group AS g ON (g.id = gu.group_id)
88 ORDER BY t.g_id DESC, t.id ASC
92 //Replace bundle entity name by table name
93 $req = $this->replace($req);
95 //Get result set mapping instance
96 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
97 $rsm = new ResultSetMapping();
100 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
101 //addScalarResult($sqlColName, $resColName, $type = 'string');
102 $rsm->addScalarResult('id', 'id', 'integer')
103 ->addScalarResult('mail', 'mail', 'string')
104 ->addScalarResult('forename', 'forename', 'string')
105 ->addScalarResult('surname', 'surname', 'string')
106 ->addScalarResult('g_title', 'g_title', 'string');
110 ->createNativeQuery($req, $rsm)
111 ->setParameter('offset', $page * $count)
112 ->setParameter('count', $count)
119 foreach($res as $data) {
120 //Get translated group
121 $group = $this->translator
->trans($data['g_title'], [], $this->alias
);
123 //Init group subarray
124 if (!isset($ret[$group])) {
129 $ret[$group][$data['id']] = [
130 'mail' => $data['mail'],
131 'forename' => $data['forename'],
132 'surname' => $data['surname'],
133 //Milonga Raphaël exception
134 'edit' => $this->router
->generate('rapsysuser_edit', ['mail' => $short = $this->slugger
->short($data['mail']), 'hash' => $this->slugger
->hash($short)])
145 public function upgradePassword(PasswordAuthenticatedUserInterface
$user, string $hash): void {
146 //Set new hashed password
147 $user->setPassword($hash);
149 //Flush data to database
150 $this->getEntityManager()->flush();