Spookie/src/Controller/UserController.php

89 lines
2.7 KiB
PHP
Raw Normal View History

2021-06-13 13:23:56 +02:00
<?php
namespace App\Controller;
2022-04-11 16:05:12 +02:00
use App\Entity\User;
use App\Repository\UserRepository;
2021-06-13 13:23:56 +02:00
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
2022-04-11 16:05:12 +02:00
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
2021-06-13 13:23:56 +02:00
2022-04-11 16:05:12 +02:00
/**
2022-05-03 14:52:04 +02:00
* Class UserController.
2022-04-11 16:05:12 +02:00
*/
2021-06-13 13:23:56 +02:00
class UserController extends AbstractController
{
2022-05-03 14:52:04 +02:00
/**
* @param \App\Repository\UserRepository $userRepository
* @param string $userName
*
* @return \Symfony\Component\HttpFoundation\Response
*/
#[Route(path: '/profile/edit/{username}', name: 'app_profile_edit')]
public function editProfile(UserRepository $userRepository, string $username = ''): Response
{
/* var User $user */
if ($username === '') {
if ($this->isGranted(attribute: 'ROLE_USER')) {
$user = $this->getUser();
} else {
throw new AccessDeniedException(message: 'You need to be logged in.');
}
} else {
if ($this->isGranted(attribute: 'ROLE_ADMIN')) {
$user = $userRepository->findOneBy([
'username' => $username,
]);
}
}
if (isset($user)) {
return $this->render(view: 'user/edit_profile.html.twig', parameters: [
'user' => $user,
]);
} else {
throw new UserNotFoundException();
}
}
/**
* @param \App\Repository\UserRepository $userRepository
* @param string $username
*
* @return \Symfony\Component\HttpFoundation\Response
*/
#[Route(path: '/profile/{username}', name: 'app_profile')]
public function showProfile(UserRepository $userRepository, string $username = ''): Response
{
/* var User $user */
if ($username === '') {
$user = $this->getUser();
} else {
$user = $userRepository->findOneBy([
'username' => $username,
]);
}
return $this->render(view: 'user/show_profile.html.twig', parameters: [
'user' => $user,
]);
}
/**
* @param \App\Repository\UserRepository $userRepository
*
* @return \Symfony\Component\HttpFoundation\Response
*/
#[Route(path: '/list_users/', name: 'app_list_user')]
public function listUsers(UserRepository $userRepository): Response
{
$users = $userRepository->findAll();
return $this->render(view: 'user/list_users.html.twig', parameters: [
'users' => $users,
]);
}
2021-06-13 13:23:56 +02:00
}