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 as array
59 * @param integer $page The page
60 * @param integer $count The count
61 * @return array The users sorted by id
63 public function findAllAsArray(int $page, int $count): array {
71 CONCAT_WS(" ", u.forename, u.surname) AS pseudonym,
74 GROUP_CONCAT(g.id ORDER BY g.id SEPARATOR "\\n") AS g_ids,
75 GROUP_CONCAT(g.title ORDER BY g.id SEPARATOR "\\n") AS g_titles
76 FROM RapsysUserBundle:User AS u
77 JOIN RapsysUserBundle:UserGroup AS gu ON (gu.user_id = u.id)
78 JOIN RapsysUserBundle:Group AS g ON (g.id = gu.group_id)
79 JOIN RapsysUserBundle:Civility AS c ON (c.id = u.civility_id)
85 //Replace bundle entity name by table name
86 $req = $this->replace($req);
88 //Get result set mapping instance
89 //XXX: DEBUG: see ../blog.orig/src/Rapsys/UserBundle/Repository/ArticleRepository.php
90 $rsm = new ResultSetMapping();
93 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
94 //addScalarResult($sqlColName, $resColName, $type = 'string');
95 $rsm->addScalarResult('id', 'id', 'integer')
96 ->addScalarResult('mail', 'mail', 'string')
97 ->addScalarResult('forename', 'forename', 'string')
98 ->addScalarResult('surname', 'surname', 'string')
99 ->addScalarResult('pseudonym', 'pseudonym', 'string')
100 ->addScalarResult('c_id', 'c_id', 'integer')
101 ->addScalarResult('c_title', 'c_title', 'string')
102 //XXX: is a string because of \n separator
103 ->addScalarResult('g_ids', 'g_ids', 'string')
104 //XXX: is a string because of \n separator
105 ->addScalarResult('g_titles', 'g_titles', 'string');
109 ->createNativeQuery($req, $rsm)
110 ->setParameter('offset', $page * $count)
111 ->setParameter('count', $count)
118 foreach($res as $data) {
120 $ret[$data['id']] = [
121 'mail' => $data['mail'],
122 'forename' => $data['forename'],
123 'surname' => $data['surname'],
124 'pseudonym' => $data['pseudonym'],
126 'slug' => $this->slugger
->slug($data['pseudonym']),
127 'link' => $this->router
->generate('rapsysuser_edit', ['mail' => $short = $this->slugger
->short($data['mail']), 'hash' => $this->slugger
->hash($short)])
131 if (!empty($data['g_ids'])) {
133 $titles = explode("\n", $data['g_titles']);
135 //Iterate on each group
136 foreach(explode("\n", $data['g_ids']) as $k => $id) {
138 $ret[$data['id']]['groups'][$id] = [
139 'title' => $group = $this->translator
->trans($titles[$k]),
140 #'slug' => $this->slugger->slug($group)
141 #'link' => $this->router->generate('rapsysuser_group_view', ['id' => $id, 'slug' => $this->slugger->short($group)])
154 public function upgradePassword(PasswordAuthenticatedUserInterface
$user, string $hash): void {
155 //Set new hashed password
156 $user->setPassword($hash);
158 //Flush data to database
159 $this->getEntityManager()->flush();