]> Raphaël G. Git Repositories - userbundle/blob - Repository/UserRepository.php
Add password upgrade feature
[userbundle] / Repository / UserRepository.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys UserBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\UserBundle\Repository;
13
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;
18
19 /**
20 * UserRepository
21 */
22 class UserRepository extends EntityRepository implements PasswordUpgraderInterface {
23 /**
24 * Find user count as int
25 *
26 * @return integer The keywords count
27 */
28 public function findCountAsInt(): int {
29 //Set the request
30 $req = <<<SQL
31 SELECT COUNT(u.id) AS count
32 FROM RapsysUserBundle:User AS u
33 SQL;
34
35 //Get result set mapping instance
36 $req = $this->replace($req);
37
38 //Get result set mapping instance
39 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
40 $rsm = new ResultSetMapping();
41
42 //Declare all fields
43 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
44 //addScalarResult($sqlColName, $resColName, $type = 'string');
45 $rsm->addScalarResult('count', 'count', 'integer');
46
47 //Get result
48 return $this->_em
49 ->createNativeQuery($req, $rsm)
50 ->getSingleScalarResult();
51 }
52
53 /**
54 * Find all users as array
55 *
56 * @param integer $page The page
57 * @param integer $count The count
58 * @return array The users sorted by id
59 */
60 public function findAllAsArray(int $page, int $count): array {
61 //Set the request
62 $req = <<<SQL
63 SELECT
64 u.id,
65 u.mail,
66 u.forename,
67 u.surname,
68 CONCAT_WS(" ", u.forename, u.surname) AS pseudonym,
69 c.id AS c_id,
70 c.title AS c_title,
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)
77 GROUP BY u.id
78 ORDER BY u.id ASC
79 LIMIT :offset, :count
80 SQL;
81
82 //Replace bundle entity name by table name
83 $req = $this->replace($req);
84
85 //Get result set mapping instance
86 //XXX: DEBUG: see ../blog.orig/src/Rapsys/UserBundle/Repository/ArticleRepository.php
87 $rsm = new ResultSetMapping();
88
89 //Declare all fields
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');
103
104 //Fetch result
105 $res = $this->_em
106 ->createNativeQuery($req, $rsm)
107 ->setParameter('offset', $page * $count)
108 ->setParameter('count', $count)
109 ->getResult();
110
111 //Init return
112 $ret = [];
113
114 //Process result
115 foreach($res as $data) {
116 //Set data
117 $ret[$data['id']] = [
118 'mail' => $data['mail'],
119 'forename' => $data['forename'],
120 'surname' => $data['surname'],
121 'pseudonym' => $data['pseudonym'],
122 'groups' => [],
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)])
125 ];
126
127 //With groups
128 if (!empty($data['g_ids'])) {
129 //Set titles
130 $titles = explode("\n", $data['g_titles']);
131
132 //Iterate on each group
133 foreach(explode("\n", $data['g_ids']) as $k => $id) {
134 //Add group
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)])
139 ];
140 }
141 }
142 }
143
144 //Send result
145 return $ret;
146 }
147
148 /**
149 * {@inheritdoc}
150 */
151 public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $hash): void {
152 //Set new hashed password
153 $user->setPassword($hash);
154
155 //Flush data to database
156 $this->getEntityManager()->flush();
157 }
158 }