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, :guestid) AS g_id,
80 IFNULL(g.title, :guesttitle) 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_id', 'g_id', 'integer')
107 ->addScalarResult('g_title', 'g_title', 'string');
111 ->createNativeQuery($req, $rsm)
112 ->setParameter('offset', $page * $count)
113 ->setParameter('count', $count)
120 foreach($res as $data) {
121 //Get translated group
122 $group = $this->translator
->trans($data['g_title'], [], $this->alias
);
124 //Init group subarray
125 if (!isset($ret[$group])) {
130 $ret[$group][$data['id']] = [
131 'mail' => $data['mail'],
132 'forename' => $data['forename'],
133 'surname' => $data['surname'],
134 //Milonga Raphaël exception
135 'edit' => $this->router
->generate('rapsysuser_edit', ['mail' => $short = $this->slugger
->short($data['mail']), 'hash' => $this->slugger
->hash($short)])
146 public function upgradePassword(PasswordAuthenticatedUserInterface
$user, string $hash): void {
147 //Set new hashed password
148 $user->setPassword($hash);
150 //Flush data to database
151 $this->getEntityManager()->flush();