1 | <?php |
||
2 | |||
3 | namespace PiouPiou\RibsAdminBundle\Controller; |
||
4 | |||
5 | use PiouPiou\RibsAdminBundle\Entity\Account; |
||
6 | use PiouPiou\RibsAdminBundle\Entity\User; |
||
7 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
||
8 | use Symfony\Component\HttpFoundation\RedirectResponse; |
||
9 | use Symfony\Component\HttpFoundation\Request; |
||
10 | use Symfony\Component\HttpFoundation\Response; |
||
11 | use Symfony\Component\Routing\Annotation\Route; |
||
12 | |||
13 | class AccountsController extends AbstractController |
||
14 | { |
||
15 | /** |
||
16 | * @Route("/accounts/", name="ribsadmin_accounts") |
||
17 | * @return Response |
||
18 | */ |
||
19 | public function list(): Response |
||
20 | { |
||
21 | $em = $this->getDoctrine()->getManager(); |
||
22 | $current_account = $this->getUser()->getUser(); |
||
23 | |||
24 | $users = $em->getRepository("RibsAdminBundle:Account")->findAllUserArchived($current_account); |
||
25 | $users_archived = $em->getRepository("RibsAdminBundle:Account")->findAllUserArchived($current_account, true); |
||
26 | |||
27 | return $this->render('@RibsAdmin/accounts/list.html.twig', [ |
||
28 | "users" => $users, |
||
29 | "users_archived" => $users_archived |
||
30 | ]); |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * @Route("/accounts/create/", name="ribsadmin_accounts_create") |
||
35 | * @Route("/accounts/show/{guid}", name="ribsadmin_accounts_show") |
||
36 | * @Route("/accounts/edit/{guid}", name="ribsadmin_accounts_edit") |
||
37 | * @param Request $request |
||
38 | * @param string|null $guid |
||
39 | * @return Response |
||
40 | */ |
||
41 | public function edit(Request $request, string $guid = null): Response |
||
42 | { |
||
43 | $em = $this->getDoctrine()->getManager(); |
||
44 | $disabled_form = strpos($request->get("_route"), "_show") ? true : false; |
||
45 | |||
46 | if ($guid === null) { |
||
47 | $account = new Account(); |
||
48 | $old_password = null; |
||
49 | $user = null; |
||
50 | } else { |
||
51 | $user = $em->getRepository(User::class)->findOneBy(["guid" => $guid]); |
||
52 | $account = $em->getRepository(Account::class)->findOneBy(["user" => $user->getId()]); |
||
53 | $old_password = $account->getPassword(); |
||
54 | } |
||
55 | |||
56 | $form = $this->createForm("PiouPiou\RibsAdminBundle\Form\Account", $account, ["disabled" => $disabled_form]); |
||
57 | |||
58 | $form->handleRequest($request); |
||
59 | |||
60 | if ($form->isSubmitted() && $form->isValid()) { |
||
61 | /** |
||
62 | * @var Account |
||
63 | */ |
||
64 | $data = $form->getData(); |
||
65 | |||
66 | $account_exist = $em->getRepository(Account::class)->findOneBy(["username" => $data->getUsername()]); |
||
67 | |||
68 | if ($account_exist && $account_exist === $account) { |
||
69 | $account_exist = null; |
||
70 | } |
||
71 | |||
72 | if (!$account_exist) { |
||
73 | if ($guid === null) { |
||
74 | $temp_password = $this->get("security.password_encoder")->encodePassword($data, $form->get("password")->getData()); |
||
75 | $data->setPassword($temp_password); |
||
76 | } else if ($form->get("password")->getData()) { |
||
77 | $temp_password = $this->get("security.password_encoder")->encodePassword($data, $form->get("password")->getData()); |
||
78 | $data->setPassword($temp_password); |
||
79 | } else { |
||
80 | $data->setPassword($old_password); |
||
81 | } |
||
82 | |||
83 | $em->persist($data); |
||
84 | $em->flush(); |
||
85 | |||
86 | $username = $data->getUser()->getFirstName() . " " . $data->getUser()->getLastName(); |
||
87 | |||
88 | if ($guid === null) { |
||
89 | $this->addFlash("success-flash", "the account of " . $username . " was created"); |
||
90 | } else { |
||
91 | $this->addFlash("success-flash", "the account of " . $username . " was edited"); |
||
92 | } |
||
93 | |||
94 | return $this->redirectToRoute("ribsadmin_accounts"); |
||
95 | } else { |
||
96 | $this->addFlash("error-flash", "An account with username " . $data->getUsername() . " already exist"); |
||
97 | return $this->redirectToRoute($request->get("_route"), ["guid" => $guid]); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
98 | } |
||
99 | } |
||
100 | |||
101 | return $this->render("@RibsAdmin/accounts/edit.html.twig", [ |
||
102 | "form" => $form->createView(), |
||
103 | "form_errors" => $form->getErrors(), |
||
104 | "user" => $user, |
||
105 | "disabled_form" => $disabled_form |
||
106 | ]); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * method to disable or enable a user |
||
111 | * @Route("/accounts/archive/{guid}/{activate}", name="ribsadmin_accounts_archive") |
||
112 | * @param string $guid |
||
113 | * @param bool $activate |
||
114 | * @return RedirectResponse |
||
115 | */ |
||
116 | public function archive(string $guid, bool $activate = false): RedirectResponse |
||
117 | { |
||
118 | $em = $this->getDoctrine()->getManager(); |
||
119 | |||
120 | $user = $em->getRepository("RibsAdminBundle:User")->findOneBy(["guid" => $guid]); |
||
121 | |||
122 | if ($user) { |
||
123 | if ($activate === true) { |
||
124 | $user->setArchived(false); |
||
125 | $word = "activated"; |
||
126 | } else { |
||
127 | $user->setArchived(true); |
||
128 | $word = "disabled"; |
||
129 | } |
||
130 | |||
131 | $em->persist($user); |
||
132 | $em->flush(); |
||
133 | |||
134 | $this->addFlash("success-flash", "The user " . $user->getFirstname() . " " . $user->getLastname() . |
||
135 | " was " . $word . " sucessfuly"); |
||
136 | } |
||
137 | |||
138 | return $this->redirectToRoute("ribsadmin_accounts"); |
||
139 | } |
||
140 | } |
||
141 |