Update controllers

This commit is contained in:
Ryan Prather 2024-11-29 21:46:33 -05:00
parent 9a0b4db460
commit eac41ac9fc
4 changed files with 223 additions and 33 deletions

View File

@ -0,0 +1,169 @@
<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\UserFormType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\CurrentUser;
use Symfony\Component\Security\Http\Attribute\IsGranted;
#[IsGranted('ROLE_ADMIN')]
class AdminController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly UserPasswordHasherInterface $userPasswordHasher,
private array $navLinks = []
) {
$this->navLinks = [
'admin_dashboard' => 'nav-link text-dark',
'user_dashboard' => 'nav-link text-dark',
'profile' => 'nav-link text-dark',
'user_list' => 'nav-link text-dark',
];
}
#[Route('/admin-dashboard', name: 'app_admin_dashboard')]
public function adminDashboard(#[CurrentUser()] User $user): Response
{
$this->denyAccessUnlessGranted('ROLE_ADMIN');
$this->navLinks['admin_dashboard'] = 'nav-link text-white active bg-gradient-dark';
return $this->render(
'internal/admin-dashboard.html.twig',
array_merge(
$this->navLinks,
[
'breadcrumbs' => [
'Admin Dashboard'
],
'notifications' => $user->retrieveUnreadNotifications(),
]
)
);
}
#[Route('/list-users', name: 'app_list_users')]
public function listUsers(#[CurrentUser()] User $user): Response
{
$this->denyAccessUnlessGranted('ROLE_ADMIN');
$users = $user->getCompany()->getUsers();
$this->navLinks['user_list'] = 'nav-link text-white active bg-gradient-dark';
return $this->render(
'internal/admin/list-users.html.twig',
array_merge(
$this->navLinks,
[
'breadcrumbs' => [
'User List'
],
'users' => $users,
'notifications' => $user->retrieveUnreadNotifications(),
]
)
);
}
#[Route('/add-user', name: 'app_add_user')]
public function addUser(Request $request, #[CurrentUser()] User $admin): Response
{
$this->denyAccessUnlessGranted('ROLE_ADMIN');
$user = new User();
$form = $this->createForm(UserFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$plainPassword = $form->get('password')->getData();
$user->setUsername($form->get('username')->getData());
$user->setPassword(
$this->userPasswordHasher->hashPassword(
$user,
$plainPassword
)
);
$user->setRoles(['ROLE_USER']);
$user->setName($form->get('name')->getData());
$user->setEmail($form->get('email')->getData());
$user->setJob($form->get('job')->getData());
$user->setRateType($form->get('rateType')->getData());
$user->setRate($form->get('rate')->getData());
$user->setLevel($form->get('level')->getData());
$user->setCompany($admin->getCompany());
$this->entityManager->persist($user);
$this->entityManager->flush();
return $this->redirectToRoute('app_list_users');
}
$this->navLinks['user_list'] = 'nav-link text-white active bg-gradient-dark';
return $this->render(
'internal/admin/add-user.html.twig',
array_merge(
$this->navLinks,
[
'breadcrumbs' => [
'Add User'
],
'form' => $form,
'notifications' => $admin->retrieveUnreadNotifications(),
]
)
);
}
#[Route('/edit-user/{id}', name: 'app_edit_user')]
public function editUser(string $id, Request $request, #[CurrentUser()] User $admin): Response
{
/** @var User $user */
$user = $this->entityManager->getRepository(User::class)->find($id);
$form = $this->createForm(UserFormType::class, $user);
$form->handleRequest($request);
$this->navLinks['user_list'] = 'nav-link text-white active bg-gradient-dark';
if ($form->isSubmitted() && $form->isValid()) {
$user->setName($form->get('name')->getData())
->setEmail($form->get('email')->getData())
->setJob($form->get('job')->getData())
->setRateType($form->get('rateType')->getData())
->setRate($form->get('rate')->getData())
->setLevel($form->get('level')->getData());
$this->entityManager->persist($user);
$this->entityManager->flush();
return $this->redirectToRoute('app_list_users');
}
return $this->render(
'internal/admin/edit-user.html.twig',
array_merge(
$this->navLinks,
[
'breadcrumbs' => [
'Edit User'
],
'data' => $user,
'form' => $form,
'notifications' => $admin->retrieveUnreadNotifications(),
]
)
);
}
}

View File

@ -3,14 +3,31 @@
namespace App\Controller; namespace App\Controller;
use App\Entity\User; use App\Entity\User;
use App\Form\UserFormType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\CurrentUser; use Symfony\Component\Security\Http\Attribute\CurrentUser;
class DefaultController extends AbstractController class DefaultController extends AbstractController
{ {
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly UserPasswordHasherInterface $userPasswordHasher,
private array $navLinks = []
) {
$this->navLinks = [
'admin_dashboard' => 'nav-link text-dark',
'user_dashboard' => 'nav-link text-dark',
'profile' => 'nav-link text-dark',
'user_list' => 'nav-link text-dark',
];
}
#[Route('/dashboard', name: 'app_dashboard')] #[Route('/dashboard', name: 'app_dashboard')]
public function dashboard(Request $request, #[CurrentUser()] ?User $user): Response public function dashboard(Request $request, #[CurrentUser()] ?User $user): Response
{ {
@ -20,11 +37,41 @@ class DefaultController extends AbstractController
return $this->redirectToRoute('app_register_step', ['step' => RegistrationController::REGISTER_STEP_TWO]); return $this->redirectToRoute('app_register_step', ['step' => RegistrationController::REGISTER_STEP_TWO]);
} }
$this->navLinks['user_dashboard'] = 'nav-link text-white active bg-gradient-dark';
return $this->render( return $this->render(
'internal/dashboard.html.twig', 'internal/dashboard.html.twig',
array_merge(
$this->navLinks,
[ [
'user' => $user 'breadcrumbs' => [
'Dashboard'
],
'notifications' => $user->retrieveUnreadNotifications(),
] ]
)
); );
} }
#[Route('/profile', name: 'app_profile')]
public function profile(#[CurrentUser()] User $user): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$this->navLinks['profile'] = 'nav-link text-white active bg-gradient-dark';
return $this->render(
'internal/profile.html.twig',
array_merge(
$this->navLinks,
[
'breadcrumbs' => [
'Profile'
],
'notifications' => $user->retrieveUnreadNotifications(),
]
)
);
}
} }

View File

@ -69,7 +69,7 @@ class RegistrationController extends AbstractController
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
// @var string $plainPassword /** @var string $plainPassword */
$plainPassword = $form->get('plainPassword')->getData(); $plainPassword = $form->get('plainPassword')->getData();
// encode the plain password // encode the plain password
@ -119,35 +119,6 @@ class RegistrationController extends AbstractController
return $this->redirectToRoute('app_dashboard'); return $this->redirectToRoute('app_dashboard');
} }
#[Route('/new-user', name: 'app_new_user')]
public function newUser(Request $request): Response
{
return $this->render('registration/new-user.html.twig');
}
#[Route('/add-user', name: 'app_add_user')]
public function addUser(Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager): Response
{
$user = new User();
$user->setUsername('new-user');
$user->setEmail('g6eK1@example.com');
$user->setName('New User');
$user->setPassword(
$this->userPasswordHasher->hashPassword(
$user,
'password'
)
);
$user->setJob(JobType::ADMIN);
$user->setRateType(RateType::FIXED);
$user->setRate('0.00');
$entityManager->persist($user);
$entityManager->flush();
return $this->redirectToRoute('dashboard');
}
#[Route('/register', name: 'app_register')] #[Route('/register', name: 'app_register')]
public function register(): Response public function register(): Response
{ {

View File

@ -12,6 +12,9 @@ class SecurityController extends AbstractController
#[Route(path: '/', name: 'app_login')] #[Route(path: '/', name: 'app_login')]
public function login(AuthenticationUtils $authenticationUtils): Response public function login(AuthenticationUtils $authenticationUtils): Response
{ {
if ($this->isGranted('ROLE_ADMIN')) {
return $this->redirectToRoute('app_admin_dashboard');
}
if ($this->isGranted('IS_AUTHENTICATED_FULLY')) { if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
return $this->redirectToRoute('app_dashboard'); return $this->redirectToRoute('app_dashboard');
} }