Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 61 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| UserController | |
0.00% |
0 / 61 |
|
0.00% |
0 / 5 |
210 | |
0.00% |
0 / 1 |
| showProfile | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| editProfile | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
56 | |||
| listUsers | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| humanFilesize | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| uploadAvatar | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Controller; |
| 4 | |
| 5 | use App\Entity\User; |
| 6 | use App\Form\EditProfileFormType; |
| 7 | use App\Repository\UserRepository; |
| 8 | use Doctrine\ORM\EntityManagerInterface; |
| 9 | use Flasher\Prime\FlasherInterface; |
| 10 | use Flasher\SweetAlert\Prime\SweetAlertFactory; |
| 11 | use Sunrise\Slugger\Slugger; |
| 12 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 13 | use Symfony\Component\HttpFoundation\File\UploadedFile; |
| 14 | use Symfony\Component\HttpFoundation\Request; |
| 15 | use Symfony\Component\HttpFoundation\Response; |
| 16 | use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; |
| 17 | use Symfony\Component\Routing\Annotation\Route; |
| 18 | use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
| 19 | use Symfony\Component\Security\Core\Exception\UserNotFoundException; |
| 20 | use Symfony\Component\Validator\Constraints\File; |
| 21 | use Symfony\Component\Validator\Constraints\NotBlank; |
| 22 | use Symfony\Component\Validator\Validator\ValidatorInterface; |
| 23 | |
| 24 | /** |
| 25 | * Class UserController. |
| 26 | */ |
| 27 | class UserController extends BaseController |
| 28 | { |
| 29 | #[Route(path: '/profile/{username}', name: 'app_profile')] |
| 30 | public function showProfile(UserRepository $userRepository, string $username = ''): Response |
| 31 | { |
| 32 | /* var User $user */ |
| 33 | if ($username === '') { |
| 34 | $user = $this->getUser(); |
| 35 | } else { |
| 36 | $user = $userRepository->findOneBy([ |
| 37 | 'username' => $username, |
| 38 | ]); |
| 39 | } |
| 40 | |
| 41 | return $this->render(view: '@default/user/show_profile.html.twig', parameters: [ |
| 42 | 'user' => $user, |
| 43 | ]); |
| 44 | } |
| 45 | |
| 46 | #[Route(path: '/profile/edit/{username}', name: 'app_profile_edit')] |
| 47 | public function editProfile(Request $request, |
| 48 | UserRepository $userRepository, |
| 49 | UserPasswordHasherInterface $userPasswordHasher, |
| 50 | EntityManagerInterface $entityManager, |
| 51 | string $username = ''): Response |
| 52 | { |
| 53 | $user = $this->getUser(); |
| 54 | |
| 55 | $editUser = $userRepository->findOneBy(['username' => $username]); |
| 56 | |
| 57 | if ($username !== $editUser->getUsername()) { |
| 58 | if (!$this->isGranted(attribute: 'ROLE_ADMIN')) { |
| 59 | $this->addFlash(type: 'error', message: 'Only admins are allowed to edit foreign profiles.'); |
| 60 | return $this->redirectToRoute(route: 'app_main'); |
| 61 | |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | $form = $this->createForm(type: EditProfileFormType::class, data: $user); |
| 66 | $form->handleRequest(request: $request); |
| 67 | |
| 68 | if ($form->isSubmitted() && $form->isValid()) { |
| 69 | $user = $form->getData(); |
| 70 | // if there's a new password, use it |
| 71 | |
| 72 | if ($form->get(name: 'newPassword')->getData()) { |
| 73 | $user->setPassword( |
| 74 | password: $userPasswordHasher->hashPassword( |
| 75 | user: $user, |
| 76 | plainPassword: $form->get(name: 'newPassword')->getData() |
| 77 | ) |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | if ($user->getTmpAvatar()) { |
| 82 | $user->setAvatar($user->getTmpAvatar()); |
| 83 | $user->setTmpAvatar(''); |
| 84 | } |
| 85 | |
| 86 | $entityManager->persist(entity: $user); |
| 87 | $entityManager->flush(); |
| 88 | |
| 89 | $this->addFlash(type: 'success', message: 'Profile successfully updated.'); |
| 90 | |
| 91 | return $this->redirectToRoute(route: 'app_main'); |
| 92 | }; |
| 93 | |
| 94 | return $this->renderForm(view: '@default/user/edit_profile.html.twig', parameters: [ |
| 95 | 'user' => $editUser, |
| 96 | 'userForm' => $form |
| 97 | ]); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | #[Route(path: '/list_users/', name: 'app_list_user')] |
| 102 | public function listUsers(UserRepository $userRepository): Response |
| 103 | { |
| 104 | $users = $userRepository->findAll(); |
| 105 | |
| 106 | return $this->render(view: '@default/user/list_users.html.twig', parameters: [ |
| 107 | 'users' => $users, |
| 108 | ]); |
| 109 | } |
| 110 | |
| 111 | // TODO move to a helper class |
| 112 | function humanFilesize($bytes, $decimals = 2): string |
| 113 | { |
| 114 | $sz = 'BKMGTP'; |
| 115 | $factor = floor((strlen($bytes) - 1) / 3); |
| 116 | return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor]; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | #[Route(path: '/user/upload/avatar/{id}', name: 'user_upload_avatar')] |
| 121 | public function uploadAvatar( |
| 122 | Request $request, |
| 123 | UserRepository $userRepository, |
| 124 | EntityManagerInterface $entityManager, |
| 125 | ValidatorInterface $validator, |
| 126 | int $id) |
| 127 | { |
| 128 | $user = $userRepository->find($id); |
| 129 | |
| 130 | if (!$user) { |
| 131 | return $this->json('User not found.', 404); |
| 132 | } |
| 133 | |
| 134 | $postMaxSize = UploadedFile::getMaxFilesize(); |
| 135 | $contentLength = $request->headers->get('Content-length'); |
| 136 | |
| 137 | if ($contentLength > $postMaxSize) { |
| 138 | return $this->json('File is bigger than the allowed ' . $this->humanFilesize($postMaxSize) . ' Bytes.', 400); |
| 139 | } |
| 140 | |
| 141 | $uploadedAvatar = $request->files->get('file'); |
| 142 | $destination = $this->getParameter(name: 'kernel.project_dir') . '/public/uploads/avatars'; |
| 143 | $originalFilename = pathinfo($uploadedAvatar->getClientOriginalName(), PATHINFO_FILENAME); |
| 144 | $slugger = new Slugger(); |
| 145 | $cleanFilename = $slugger->slugify($originalFilename); |
| 146 | $newFilename = $cleanFilename . '-' . uniqid() . '.' . $uploadedAvatar->guessExtension(); |
| 147 | $uploadedAvatar->move($destination, $newFilename); |
| 148 | // Store the tmp name, use it on real form submit. |
| 149 | $user->setTmpAvatar($newFilename); |
| 150 | $entityManager->persist(entity: $user); |
| 151 | $entityManager->flush(); |
| 152 | |
| 153 | return $this->json(data: 'OK', status: 201); |
| 154 | } |
| 155 | } |