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
;
22 class UserRepository
extends EntityRepository
implements PasswordUpgraderInterface
{
24 * Find user count as int
26 * @return integer The keywords count
28 public function findCountAsInt(): int {
31 SELECT COUNT(u.id) AS count
32 FROM RapsysUserBundle:User AS u
35 //Get result set mapping instance
36 $req = $this->replace($req);
38 //Get result set mapping instance
39 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
40 $rsm = new ResultSetMapping();
43 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
44 //addScalarResult($sqlColName, $resColName, $type = 'string');
45 $rsm->addScalarResult('count', 'count', 'integer');
49 ->createNativeQuery($req, $rsm)
50 ->getSingleScalarResult();
54 * Find all users as array
56 * @param integer $page The page
57 * @param integer $count The count
58 * @return array The users sorted by id
60 public function findAllAsArray(int $page, int $count): array {
68 CONCAT_WS(" ", u.forename, u.surname) AS pseudonym,
71 GROUP_CONCAT(g.id ORDER BY g.id SEPARATOR "\\n") AS g_ids,
72 GROUP_CONCAT(g.title ORDER BY g.id SEPARATOR "\\n") AS g_titles
73 FROM RapsysUserBundle:User AS u
74 JOIN RapsysUserBundle:UserGroup AS gu ON (gu.user_id = u.id)
75 JOIN RapsysUserBundle:Group AS g ON (g.id = gu.group_id)
76 JOIN RapsysUserBundle:Civility AS c ON (c.id = u.civility_id)
82 //Replace bundle entity name by table name
83 $req = $this->replace($req);
85 //Get result set mapping instance
86 //XXX: DEBUG: see ../blog.orig/src/Rapsys/UserBundle/Repository/ArticleRepository.php
87 $rsm = new ResultSetMapping();
90 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
91 //addScalarResult($sqlColName, $resColName, $type = 'string');
92 $rsm->addScalarResult('id', 'id', 'integer')
93 ->addScalarResult('mail', 'mail', 'string')
94 ->addScalarResult('forename', 'forename', 'string')
95 ->addScalarResult('surname', 'surname', 'string')
96 ->addScalarResult('pseudonym', 'pseudonym', 'string')
97 ->addScalarResult('c_id', 'c_id', 'integer')
98 ->addScalarResult('c_title', 'c_title', 'string')
99 //XXX: is a string because of \n separator
100 ->addScalarResult('g_ids', 'g_ids', 'string')
101 //XXX: is a string because of \n separator
102 ->addScalarResult('g_titles', 'g_titles', 'string');
106 ->createNativeQuery($req, $rsm)
107 ->setParameter('offset', $page * $count)
108 ->setParameter('count', $count)
115 foreach($res as $data) {
117 $ret[$data['id']] = [
118 'mail' => $data['mail'],
119 'forename' => $data['forename'],
120 'surname' => $data['surname'],
121 'pseudonym' => $data['pseudonym'],
123 'slug' => $this->slugger
->slug($data['pseudonym']),
124 'link' => $this->router
->generate('rapsys_user_edit', ['mail' => $short = $this->slugger
->short($data['mail']), 'hash' => $this->slugger
->hash($short)])
128 if (!empty($data['g_ids'])) {
130 $titles = explode("\n", $data['g_titles']);
132 //Iterate on each group
133 foreach(explode("\n", $data['g_ids']) as $k => $id) {
135 $ret[$data['id']]['groups'][$id] = [
136 'title' => $group = $this->translator
->trans($titles[$k]),
137 #'slug' => $this->slugger->slug($group)
138 #'link' => $this->router->generate('rapsys_user_group_view', ['id' => $id, 'slug' => $this->slugger->short($group)])
151 public function upgradePassword(PasswordAuthenticatedUserInterface
$user, string $hash): void {
152 //Set new hashed password
153 $user->setPassword($hash);
155 //Flush data to database
156 $this->getEntityManager()->flush();