]> Raphaël G. Git Repositories - userbundle/blob - Repository/UserRepository.php
Enable register captcha
[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
16 use Rapsys\UserBundle\Repository;
17
18 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
19 use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
20 use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
21
22 /**
23 * UserRepository
24 */
25 class UserRepository extends Repository implements PasswordUpgraderInterface {
26 /**
27 * Find user count as int
28 *
29 * @return integer The keywords count
30 */
31 public function findCountAsInt(): int {
32 //Set the request
33 $req = <<<SQL
34 SELECT COUNT(u.id) AS count
35 FROM RapsysUserBundle:User AS u
36 SQL;
37
38 //Get result set mapping instance
39 $req = $this->replace($req);
40
41 //Get result set mapping instance
42 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
43 $rsm = new ResultSetMapping();
44
45 //Declare all fields
46 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
47 //addScalarResult($sqlColName, $resColName, $type = 'string');
48 $rsm->addScalarResult('count', 'count', 'integer');
49
50 //Get result
51 return $this->_em
52 ->createNativeQuery($req, $rsm)
53 ->getSingleScalarResult();
54 }
55
56 /**
57 * Find all users as array
58 *
59 * @param integer $page The page
60 * @param integer $count The count
61 * @return array The users sorted by id
62 */
63 public function findAllAsArray(int $page, int $count): array {
64 //Set the request
65 $req = <<<SQL
66 SELECT
67 u.id,
68 u.mail,
69 u.forename,
70 u.surname,
71 CONCAT_WS(" ", u.forename, u.surname) AS pseudonym,
72 c.id AS c_id,
73 c.title AS c_title,
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)
80 GROUP BY u.id
81 ORDER BY u.id ASC
82 LIMIT :offset, :count
83 SQL;
84
85 //Replace bundle entity name by table name
86 $req = $this->replace($req);
87
88 //Get result set mapping instance
89 //XXX: DEBUG: see ../blog.orig/src/Rapsys/UserBundle/Repository/ArticleRepository.php
90 $rsm = new ResultSetMapping();
91
92 //Declare all fields
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');
106
107 //Fetch result
108 $res = $this->_em
109 ->createNativeQuery($req, $rsm)
110 ->setParameter('offset', $page * $count)
111 ->setParameter('count', $count)
112 ->getResult();
113
114 //Init return
115 $ret = [];
116
117 //Process result
118 foreach($res as $data) {
119 //Set data
120 $ret[$data['id']] = [
121 'mail' => $data['mail'],
122 'forename' => $data['forename'],
123 'surname' => $data['surname'],
124 'pseudonym' => $data['pseudonym'],
125 'groups' => [],
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)])
128 ];
129
130 //With groups
131 if (!empty($data['g_ids'])) {
132 //Set titles
133 $titles = explode("\n", $data['g_titles']);
134
135 //Iterate on each group
136 foreach(explode("\n", $data['g_ids']) as $k => $id) {
137 //Add group
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)])
142 ];
143 }
144 }
145 }
146
147 //Send result
148 return $ret;
149 }
150
151 /**
152 * {@inheritdoc}
153 */
154 public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $hash): void {
155 //Set new hashed password
156 $user->setPassword($hash);
157
158 //Flush data to database
159 $this->getEntityManager()->flush();
160 }
161 }