|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Entity\User; |
|
6
|
|
|
use AppBundle\Form\User\EditType; |
|
7
|
|
|
use AppBundle\Form\User\RegistrationType; |
|
8
|
|
|
use AppBundle\Form\User\ActivationType; |
|
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
|
10
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
11
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
|
12
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
15
|
|
|
|
|
16
|
|
|
class UserController extends Controller |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @Route("/users", name="users_list") |
|
20
|
|
|
* @Template("@App/users.html.twig") |
|
21
|
|
|
* |
|
22
|
|
|
* @param Request $request |
|
23
|
|
|
* @return array |
|
24
|
|
|
*/ |
|
25
|
|
|
public function usersListAction(Request $request) |
|
26
|
|
|
{ |
|
27
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
28
|
|
|
$paginator = $this->get('knp_paginator'); |
|
29
|
|
|
$users = $paginator->paginate( |
|
30
|
|
|
$em->getRepository(User::class)->selectUsersByParams($request->query), |
|
31
|
|
|
$request->query->getInt('page', 1), |
|
32
|
|
|
10 |
|
33
|
|
|
); |
|
34
|
|
|
$activationForm = []; |
|
35
|
|
|
foreach ($users as $user) { |
|
36
|
|
|
$activationForm[$user->getId()] = $form = $this->createForm(ActivationType::class, $user, [ |
|
|
|
|
|
|
37
|
|
|
'method' => "PUT", |
|
38
|
|
|
'action' => $this->generateUrl('activate_user', ['id' => $user->getId()]), |
|
39
|
|
|
'validation_groups' => array('edit'), |
|
40
|
|
|
]) |
|
41
|
|
|
->createView(); |
|
42
|
|
|
} |
|
43
|
|
|
return [ |
|
44
|
|
|
"users" => $users, |
|
45
|
|
|
'activationForm' => $activationForm |
|
46
|
|
|
]; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @Route("/user/activate/{id}", name="activate_user") |
|
51
|
|
|
* @Template("@App/add.html.twig") |
|
52
|
|
|
* |
|
53
|
|
|
* @Method("PUT") |
|
54
|
|
|
* @param Request $request |
|
55
|
|
|
* @param User $user |
|
56
|
|
|
* @return RedirectResponse |
|
57
|
|
|
*/ |
|
58
|
|
|
public function activationAction(Request $request, User $user) |
|
59
|
|
|
{ |
|
60
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
61
|
|
|
$form = $this->createForm(ActivationType::class, $user, [ |
|
62
|
|
|
'method' => "PUT", |
|
63
|
|
|
'action' => $this->generateUrl('activate_user'), |
|
64
|
|
|
'validation_groups' => array('edit'), |
|
65
|
|
|
]) |
|
66
|
|
|
->createView(); |
|
67
|
|
|
$form->handleRequest($request); |
|
|
|
|
|
|
68
|
|
|
if ($form->isValid()) { |
|
|
|
|
|
|
69
|
|
|
$em->persist($user); |
|
70
|
|
|
$em->flush(); |
|
71
|
|
|
return $this->redirect($this->generateUrl("users_list")); |
|
72
|
|
|
} |
|
73
|
|
|
return $this->redirect($this->generateUrl("users_list")); |
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @Route("/user/add", name="add_user") |
|
79
|
|
|
* @Template("@App/add.html.twig") |
|
80
|
|
|
* |
|
81
|
|
|
* @param Request $request |
|
82
|
|
|
* @return array|RedirectResponse |
|
83
|
|
|
*/ |
|
84
|
|
|
public function userAddAction(Request $request) |
|
85
|
|
|
{ |
|
86
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
87
|
|
|
$user = new User(); |
|
88
|
|
|
$form = $this->createForm(RegistrationType::class, $user, [ |
|
89
|
|
|
'action' => $this->generateUrl('add_user'), |
|
90
|
|
|
'validation_groups' => array('registration'), |
|
91
|
|
|
]); |
|
92
|
|
|
$form->handleRequest($request); |
|
93
|
|
|
if ($form->isValid()) { |
|
94
|
|
|
$em->persist($user); |
|
95
|
|
|
$em->flush(); |
|
96
|
|
|
return $this->redirect($this->generateUrl("users_list")); |
|
97
|
|
|
} |
|
98
|
|
|
return ['form' => $form->createView()]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @Route("/user/edit/{id}", name="edit_user") |
|
103
|
|
|
* @Template("@App/add.html.twig") |
|
104
|
|
|
* |
|
105
|
|
|
* @param Request $request |
|
106
|
|
|
* @param User $user |
|
107
|
|
|
* @return array|RedirectResponse |
|
108
|
|
|
*/ |
|
109
|
|
|
public function userEditAction(Request $request, User $user) |
|
110
|
|
|
{ |
|
111
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
112
|
|
|
$form = $this->createForm(EditType::class, $user, [ |
|
113
|
|
|
'action' => $this->generateUrl('edit_user', array('id' => $user->getId())), |
|
114
|
|
|
'method' => 'POST', |
|
115
|
|
|
'validation_groups' => array('edit'), |
|
116
|
|
|
]); |
|
117
|
|
|
$form->handleRequest($request); |
|
118
|
|
|
if ($form->isValid()) { |
|
119
|
|
|
$em->persist($user); |
|
120
|
|
|
$em->flush(); |
|
121
|
|
|
return $this->redirect($this->generateUrl("users_list")); |
|
122
|
|
|
} |
|
123
|
|
|
return ['form' => $form->createView()]; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.