Repository navigation
Expand file tree
/
Copy pathUserController.php
More file actions
160 lines (135 loc) · 5.42 KB
/
Copy pathUserController.php
File metadata and controls
160 lines (135 loc) · 5.42 KB
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
<?php
namespace SyncEngine\Controller\Admin;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use SyncEngine\Attribute\MenuItem;
use SyncEngine\Controller\Admin\Abstract\AbstractAdminController;
use SyncEngine\Controller\Auth\ResetPasswordController;
use SyncEngine\Entity\User;
use SyncEngine\Form\UserFormType;
use SyncEngine\Repository\UserRepository;
#[IsGranted( 'ROLE_ADMIN' )]
#[Route('/system')]
class UserController extends AbstractAdminController
{
#[Route( '/users', name: 'users_list' )]
#[MenuItem( menu: 'main', route: 'syncengine_users_list', label: 'Users', icon: 'system-users' )]
public function renderList( Request $request, UserRepository $userRepository ): Response
{
$users = $userRepository->findAll();
return $this->render(
'admin/user/index.html.twig',
[
'users' => $users,
'breadcrumbs' => [ [ 'title' => $this->trans( 'Users' ), 'current' => true ] ],
]
);
}
#[Route( '/user/create', name: 'user_create' )]
public function renderCreate(
Request $request,
UserPasswordHasherInterface $passwordHasher,
EntityManagerInterface $entityManager,
): Response {
$user = new User();
$form = $this->createForm( UserFormType::class, $user );
$form->add( 'save', SubmitType::class, [ 'label' => $this->trans( 'Create' ) ] );
$form->handleRequest( $request );
if ( $form->isSubmitted() && $form->isValid() ) {
$newPassword = $form->get( 'plainPassword' )->getData();
if ( $newPassword ) {
$user->setPassword( $passwordHasher->hashPassword( $user, $newPassword ) );
}
$entityManager->persist( $user );
$entityManager->flush();
$this->addFlash( 'success', $this->trans( 'Successfully created user!' ) );
return $this->redirectToRoute( 'syncengine_users_list' );
}
return $this->render(
'admin/user/create.html.twig',
[
'form' => $form,
'backlink' => true,
'breadcrumbs' => [
[ 'link' => $this->generateUrl( 'syncengine_users_list' ), 'title' => $this->trans( 'Users' ) ],
[ 'title' => $this->trans( 'Create' ), 'current' => true ],
],
]
);
}
#[Route( '/user/edit/{id}', name: 'user_edit' )]
public function renderEdit(
#[MapEntity( id: 'id' )]
User $user, Request $request, UserPasswordHasherInterface $passwordHasher, EntityManagerInterface $entityManager
): Response {
$form = $this->createForm( UserFormType::class, $user );
$form->add( 'save', SubmitType::class, [ 'label' => $this->trans( 'Update' ) ] );
$form->handleRequest( $request );
if ( $form->isSubmitted() && $form->isValid() ) {
$newPassword = $form->get( 'plainPassword' )->getData();
if ( $newPassword ) {
$user->setPassword( $passwordHasher->hashPassword( $user, $newPassword ) );
}
$entityManager->persist( $user );
$entityManager->flush();
$this->addFlash( 'success', $this->trans( 'Successfully edited user!' ) );
return $this->redirectToRoute( 'syncengine_users_list' );
}
return $this->render(
'admin/user/edit.html.twig',
[
'form' => $form,
'user' => $user,
'backlink' => true,
'breadcrumbs' => [
[ 'link' => $this->generateUrl( 'syncengine_users_list' ), 'title' => $this->trans( 'Users' ) ],
[ 'title' => $this->trans( 'Edit' ), 'current' => true ],
],
]
);
}
#[Route( '/user/reset-password/{id}', name: 'user_reset_password', methods: [ 'POST' ] )]
public function resetPasswordAction(
#[MapEntity( id: 'id' )]
User $user, Request $request, ResetPasswordController $resetPasswordController
): RedirectResponse {
$currentUser = $this->getUser();
if ( $currentUser instanceof User && $currentUser->getId() === $user->getId() ) {
$this->addFlash( 'warning', $this->trans( 'You cannot reset your own password.' ) );
return $this->redirectToRoute( 'syncengine_users_list' );
}
if ( $this->isCsrfTokenValid( 'reset_' . $user->getId(), $request->request->get( '_token' ) ) ) {
try {
$resetPasswordController->processSendingPasswordResetEmail( $user );
$this->addFlash( 'success', $this->trans( 'Password reset email sent successfully!' ) );
} catch ( \Exception $e ) {
$this->addFlash( 'error', $this->trans( 'An error occurred while resetting the password.' ) );
}
}
return $this->redirectToRoute( 'syncengine_users_list' );
}
#[Route( '/user/delete/{id}', name: 'user_delete', methods: [ 'POST' ] )]
public function deleteAction(
#[MapEntity( id: 'id' )]
User $user, Request $request, EntityManagerInterface $entityManager
): RedirectResponse {
$currentUser = $this->getUser();
if ( $currentUser instanceof User && $currentUser->getId() === $user->getId() ) {
$this->addFlash( 'warning', $this->trans( 'You cannot delete your own account.' ) );
return $this->redirectToRoute( 'syncengine_users_list' );
}
if ( $this->isCsrfTokenValid( 'delete_' . $user->getId(), $request->request->get( '_token' ) ) ) {
$entityManager->remove( $user );
$entityManager->flush();
$this->addFlash( 'success', $this->trans( 'Successfully deleted user!' ) );
}
return $this->redirectToRoute( 'syncengine_users_list' );
}
}