Code : Sélectionner tout - Visualiser dans une fenêtre à part Serialization of 'Symfony\Component\HttpFoundation\File\File' is not allowed
Bonjour, dans mon site, mes utilisateurs peuvent télécharger des packages.
J'ai une entité User pour l'authentification en locale ( formulaire de connexion, base de données). Elle est gérée grâce à FOSUserBundle.
Et une autre entité UserCas qui fait référence aux utilisateurs qui s'authentifient grâce à l'authentification CAS. (Elle n'étend pas BaseUser de FOSUserBundle).
J'ai aussi une entité package qui contient en somme: id, titre, commentaire, URL fichier, URL notice, fichierPaquet, fichierNotice ( donc deux fichiers, qui sont gérés grâce à VichUploaderBundle).
Mes deux entités User possèdent un attribut qui est une ArrayCollection de packages ( associations ManyToMany par conséquent ).
Lorsqu'un utilisateur télécharge un package, alors celui-ci est ajouté dans la ArrayCollection de packages de l'utilisateur.
Ainsi en se rendant sur leur profil, les utilisateurs peuvent voir les packages qu'ils ont déjà téléchargé (on affiche sous forme de tableau, le titre, le nom du fichier, le nom de la notice, et le commentaire seulement ).
Ca fonctionne niquel pour les utilisateurs locaux ( entité User qui étend FOSUserBundle).
Mais pour les utilisateurs authentifiés en CAS ( entité User n'étend rien du tout ), j'ai une grosse erreur lorsque je veux me rendre sur la page de profil à partir du moment où mon User possède des packages dans son ArrayCollection :
Serialization of 'Symfony\Component\HttpFoundation\File\File' is not allowed
A noter que l'objet User lors d'une authentification CAS est passé en attribut dans un token, puis est récupéré dans le profile.html.twig, pour ensuite exploiter ses informations, dont ses packages à l'aide d'une boucle du genre :
J'ai donc tenté d'ajouter dans mon entité de package deux méthodes serialize et unserialize, en vain.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 for unPackage in user.packages { unPackage.titre unPackage.urlFichier //... }
Je vous montre donc mon code :
UserCas.php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133 <?php namespace Site\PagesBundle\Entity; use Serializable; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\Collection; use Site\PagesBundle\Security\Traits\traitUser; use Doctrine\Common\Collections\ArrayCollection; use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; /** * UserCas * * @ORM\Table(name="user_cas") * @ORM\Entity(repositoryClass="Site\PagesBundle\Repository\UserCasRepository") * @UniqueEntity("mail") */ class UserCas /*implements Serializable*/ { use traitUser; /** * @var int * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var int * * @ORM\Column(name="nbTelechargementsAuto", type="integer", nullable=true) */ private $nbTelechargementsAuto; /** * @var bool * * @ORM\Column(name="enabled", type="boolean") */ private $enabled; /** * @ORM\Column(name="mail", type="string") */ private $mail; /** * @var \Doctrine\Common\Collections\Collection * @ORM\ManyToMany(targetEntity="Paquet") * @ORM\JoinTable(name="paquetsDDLUserCas") * @ORM\JoinColumn(nullable=false) */ private $packages; /** * Constructor */ public function __construct() { $this->packages = new ArrayCollection(); $this->setEnabled(true); } /** * Get id * * @return int */ public function getId() { return $this->id; } /** * @return string */ public function getMail() { return $this->mail; } public function setMail($mail) { $this->mail = $mail; } /** * Set enabled * * @param boolean $enabled * * @return UserCas */ public function setEnabled($enabled) { $this->enabled = $enabled; return $this; } public function isEnabled() { return $this->enabled; } /* public function serialize() { $this->packages = base64_encode($this->packages); } public function unserialize($serialized) { $this->packages = base64_decode($this->packages); }*/ }
Le trait pour des méthodes communes entre User.php et UserCas.php :
Paquet.php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124 <?php namespace Site\PagesBundle\Security\Traits; use Site\PagesBundle\Entity\Paquet; trait traitUser { public function decDDL() { $this->setNbTelechargementsAuto($this->getNbTelechargementsAuto() - 1); } public function verifDDL() { if($this->getNbTelechargementsAuto() == 0) { $this->setEnabled(false); } } /** * Désactivation du compte */ public function desactiverCompte() { $this->setEnabled(false); $this->setCreatedAt(); return $this; } /** * Set nbTelechargementsAuto * * @param integer $nbTelechargementsAuto * */ public function setNbTelechargementsAuto($nbTelechargementsAuto) { $this->nbTelechargementsAuto = $nbTelechargementsAuto; return $this; } /** * Get nbTelechargementsAuto * * @return int */ public function getNbTelechargementsAuto() { return $this->nbTelechargementsAuto; } public function addPackage(Paquet $package) { $this->packages[] = $package; $this->decDDL(); } /** * {@inheritdoc} * * @return Collection */ public function getPackages() { return $this->packages->toArray(); } public function getPackagesId() { $lesPackages = $this->getPackages(); $tabId = array(); foreach ($lesPackages as $unPackage) { $id = $unPackage->getId(); array_push($tabId,$id); } return $tabId; } /** * {@inheritdoc} */ public function setPackages(array $packages) { $this->packages = array(); foreach ($packages as $package) { $this->addPackage($package); } $this->decDDL(); $this->verifDDL(); return $this; } /** * {@inheritdoc} */ public function removePackage($package) { if (false !== $key = array_search(strtoupper($package), $this->packages, true)) { unset($this->packages[$key]); $this->packages = array_values($this->packages); } return $this; } /** * {@inheritdoc} */ public function hasPackage($package) { return in_array(strtoupper($package), $this->getPackages(), true); } }
La fonction dans le contrôleur qui va renvoyer sur la page de profil :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350 <?php namespace Site\PagesBundle\Entity; use Serializable; use DateTimeImmutable; use Doctrine\ORM\Mapping as ORM; use Site\PagesBundle\Entity\Paquet; use Site\PagesBundle\Entity\TypeUser; use Symfony\Component\HttpFoundation\File\File; use Doctrine\Common\Collections\ArrayCollection; use Vich\UploaderBundle\Mapping\Annotation as Vich; use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\HttpFoundation\File\UploadedFile; /** * Paquet * * @ORM\Table(name="paquet") * @ORM\Entity(repositoryClass="Site\PagesBundle\Repository\PaquetRepository") * @Vich\Uploadable */ class Paquet implements \Serializable { /** * @var int * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var \Doctrine\Common\Collections\Collection * @ORM\ManyToMany(targetEntity="TypeUser") * @ORM\JoinTable(name="Packages_des_TypesUser") * @ORM\JoinColumn(nullable=false) */ private $typeUser; public function __construct() { $this->typeUser = new ArrayCollection(); } /** @see \Serializable::serialize() */ public function serialize() { return serialize(array( $this->id, $this->noticeFile, $this->paquetFile, )); } /** @see \Serializable::unserialize() */ public function unserialize($serialized) { list ( $this->id, $this->noticeFile, $this->paquetFile, ) = unserialize($serialized, array('allowed_classes' => false)); } /* public function serialize() { $toSerialize = new \stdClass(); $toSerialize->noticeFile = $this->noticeFile->getPath(); $toSerialize->paquetFile = $this->paquetFile->getPath(); return serialize(clone $this); } public function unserialize($serialized) { $unseriliazed = unserialize($serialized); $this->noticeFile = new File($unseriliazed->noticeFile); }*/ /** * Get TypeUser * * @return Site\PagesBundle\Entity\TypeUser */ public function getTypeUser() { return $this->typeUser; } public function deleteTypeFromTypesUser(TypeUser $type) { $this->typeUser->removeElement($type); } /** * Set typeUser * * @param Site\PagesBundle\Entity\TypeUser $typeUser * * @return Paquet */ public function setTypeUser(Site\PagesBundle\Entity\TypeUser $typeUser) { $this->typeUser = $typeUser; return $this; } /** * @var string * * @ORM\Column(name="titre", type="string", length=255) * @Assert\Length(min=5, max=255, minMessage="Le titre doit comporter au minimum 5 caractères") */ private $titre; /** * @var string * * @ORM\Column(name="urlPaquet", type="string", length=255) */ private $urlPaquet; /** * @Vich\UploadableField(mapping="paquet", fileNameProperty="urlPaquet") * @var File */ private $paquetFile; /** * @ORM\Column(type="datetime") * * @var \DateTime */ private $updatedAt; /** * @param File|UploadedFile $unPaquetFile * * @return Paquet */ public function setPaquetFile(File $unPaquetFile = null) { $this->paquetFile = $unPaquetFile; if ($unPaquetFile) { $this->updatedAt = new \DateTimeImmutable(); } return $this; } /** * Set updatedAt * * @param \DateTime $updatedAt * * @return Paquet */ public function setUpdatedAt($updatedAt) { $this->updatedAt = $updatedAt; return $this; } /** * Get updatedAt * * @return \DateTime */ public function getUpdatedAt() { return $this->updatedAt; } /** * @return File|null */ public function getPaquetFile() { return $this->paquetFile; } /** * @var string * * @ORM\Column(name="urlNotice", type="string", length=255,nullable=true) */ private $urlNotice; /** * @Vich\UploadableField(mapping="notice", fileNameProperty="urlNotice",nullable=true) * @var File */ private $noticeFile; /** * @var string * * @ORM\Column(name="commentaire", type="text") */ private $commentaire; /** * Get id * * @return int */ public function getId() { return $this->id; } /** * Set titre * * @param string $titre * * @return Paquet */ public function setTitre($titre) { $this->titre = $titre; return $this; } /** * Get titre * * @return string */ public function getTitre() { return $this->titre; } /** * Set urlPaquet * * @param string $urlPaquet * * @return Paquet */ public function setUrlPaquet($urlPaquet) { $this->urlPaquet = $urlPaquet; return $this; } /** * Get urlPaquet * * @return string|null */ public function getUrlPaquet() { return $this->urlPaquet; } /** * @return File|null */ public function getNoticeFile() { return $this->noticeFile; } /** * @param File|UploadedFile $uneNoticeFile * * @return Paquet */ public function setNoticeFile(File $uneNoticeFile = null) { $this->noticeFile = $uneNoticeFile; if ($uneNoticeFile) { $this->updatedAt = new \DateTimeImmutable(); } return $this; } /** * Set urlNotice * * @param string $urlNotice * * @return Paquet */ public function setUrlNotice($urlNotice) { $this->urlNotice = $urlNotice; return $this; } /** * Get urlNotice * * @return string */ public function getUrlNotice() { return $this->urlNotice; } /** * Set commentaire * * @param string $commentaire * * @return Paquet */ public function setCommentaire($commentaire) { $this->commentaire = $commentaire; return $this; } /** * Get commentaire * * @return string */ public function getCommentaire() { return $this->commentaire; } }
profile.html.twig :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 /** * @Route ("/profile", name="user_profile") */ public function profileAction() { $token = $this->get('security.token_storage')->getToken(); //dump($token->getAttribute('nomComplet')); $user = $token->getAttribute('user'); return $this->render('@Pages/cas/profile.html.twig',array( 'user' => $user, )); }
Voilà je suis perdu je ne sais plus quoi faire
Code Twig : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 {% extends "base.html.twig" %} {% block body %} <div class="container"> <h1><u>{{ user.mail }}</u></h1><br/> <h2>Profil</h2> <br/> <table class="table"> <tbody> <tr> <th scope="row">Nom d'utilisateur</th> <td>{{app.user.username}}</td> </tr> <tr> <th scope="row">Email</th> <td>{{user.mail}}</td> </tr> <tr> <th scope="row">Téléchargements restants</th> <td>4</td> </tr> </tbody> </table> <br/><br/><br/> <h3> Liste des packages téléchargés </h3> <br/> <table class="table table-stripped"> <thead> <tr> <th>Titre</th> <th>Package</th> <th>Notice</th> <th>Commentaire</th> </tr> </thead> <tbody> {% for unPackage in user.packages %} <tr> <td> {{unPackage.titre}} </td> <td> {{unPackage.urlPaquet}} </td> <td> {{unPackage.urlNotice}} </td> <td> {{unPackage.commentaire}} </td> </tr> {% endfor %} </tbody> </table> <a href="{{path('connexion_index')}}" class="btn btn-primary">Retour à l'accueil</a> <a href="{{ path('deconnexion') }}" class="btn btn-primary">Déconnexion</a><br/><br/><br/><br/> </div>
Partager