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
;
15 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
16 use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface
;
17 use Symfony\Component\Security\Core\User\PasswordUpgraderInterface
;
19 use Rapsys\UserBundle\Repository
;
24 class UserRepository
extends Repository
implements PasswordUpgraderInterface
{
26 * Find user count as int
28 * @return integer The keywords count
30 public function findCountAsInt(): int {
33 SELECT COUNT(u.id) AS count
34 FROM RapsysUserBundle:User AS u
37 //Get result set mapping instance
38 $req = $this->replace($req);
40 //Get result set mapping instance
41 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
42 $rsm = new ResultSetMapping();
45 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
46 //addScalarResult($sqlColName, $resColName, $type = 'string');
47 $rsm->addScalarResult('count', 'count', 'integer');
51 ->createNativeQuery($req, $rsm)
52 ->getSingleScalarResult();
56 * Find all users as array
58 * @param integer $page The page
59 * @param integer $count The count
60 * @return array The users sorted by id
62 public function findAllAsArray(int $page, int $count): array {
70 CONCAT_WS(" ", u.forename, u.surname) AS pseudonym,
73 GROUP_CONCAT(g.id ORDER BY g.id SEPARATOR "\\n") AS g_ids,
74 GROUP_CONCAT(g.title ORDER BY g.id SEPARATOR "\\n") AS g_titles
75 FROM RapsysUserBundle:User AS u
76 JOIN RapsysUserBundle:UserGroup AS gu ON (gu.user_id = u.id)
77 JOIN RapsysUserBundle:Group AS g ON (g.id = gu.group_id)
78 JOIN RapsysUserBundle:Civility AS c ON (c.id = u.civility_id)
84 //Replace bundle entity name by table name
85 $req = $this->replace($req);
87 //Get result set mapping instance
88 //XXX: DEBUG: see ../blog.orig/src/Rapsys/UserBundle/Repository/ArticleRepository.php
89 $rsm = new ResultSetMapping();
92 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
93 //addScalarResult($sqlColName, $resColName, $type = 'string');
94 $rsm->addScalarResult('id', 'id', 'integer')
95 ->addScalarResult('mail', 'mail', 'string')
96 ->addScalarResult('forename', 'forename', 'string')
97 ->addScalarResult('surname', 'surname', 'string')
98 ->addScalarResult('pseudonym', 'pseudonym', 'string')
99 ->addScalarResult('c_id', 'c_id', 'integer')
100 ->addScalarResult('c_title', 'c_title', 'string')
101 //XXX: is a string because of \n separator
102 ->addScalarResult('g_ids', 'g_ids', 'string')
103 //XXX: is a string because of \n separator
104 ->addScalarResult('g_titles', 'g_titles', 'string');
108 ->createNativeQuery($req, $rsm)
109 ->setParameter('offset', $page * $count)
110 ->setParameter('count', $count)
117 foreach($res as $data) {
119 $ret[$data['id']] = [
120 'mail' => $data['mail'],
121 'forename' => $data['forename'],
122 'surname' => $data['surname'],
123 'pseudonym' => $data['pseudonym'],
125 'slug' => $this->slugger
->slug($data['pseudonym']),
126 'link' => $this->router
->generate('rapsysuser_edit', ['mail' => $short = $this->slugger
->short($data['mail']), 'hash' => $this->slugger
->hash($short)])
130 if (!empty($data['g_ids'])) {
132 $titles = explode("\n", $data['g_titles']);
134 //Iterate on each group
135 foreach(explode("\n", $data['g_ids']) as $k => $id) {
137 $ret[$data['id']]['groups'][$id] = [
138 'title' => $group = $this->translator
->trans($titles[$k]),
139 #'slug' => $this->slugger->slug($group)
140 #'link' => $this->router->generate('rapsysuser_group_view', ['id' => $id, 'slug' => $this->slugger->short($group)])
153 public function upgradePassword(PasswordAuthenticatedUserInterface
$user, string $hash): void {
154 //Set new hashed password
155 $user->setPassword($hash);
157 //Flush data to database
158 $this->getEntityManager()->flush();