@@ -32,52 +32,52 @@ discard block |
||
| 32 | 32 | |
| 33 | 33 | class ProfileController extends AbstractController { |
| 34 | 34 | |
| 35 | - private EntityManagerInterface $entityManager; |
|
| 36 | - private AuthorizationCheckerInterface $authorizationChecker; |
|
| 37 | - private UserRepository $userRepository; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @param EntityManagerInterface $entityManager |
|
| 41 | - * @param AuthorizationCheckerInterface $authorizationChecker |
|
| 42 | - * @param UserRepository $userRepository |
|
| 43 | - */ |
|
| 44 | - public function __construct(EntityManagerInterface $entityManager, |
|
| 45 | - AuthorizationCheckerInterface $authorizationChecker, UserRepository $userRepository) { |
|
| 35 | + private EntityManagerInterface $entityManager; |
|
| 36 | + private AuthorizationCheckerInterface $authorizationChecker; |
|
| 37 | + private UserRepository $userRepository; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @param EntityManagerInterface $entityManager |
|
| 41 | + * @param AuthorizationCheckerInterface $authorizationChecker |
|
| 42 | + * @param UserRepository $userRepository |
|
| 43 | + */ |
|
| 44 | + public function __construct(EntityManagerInterface $entityManager, |
|
| 45 | + AuthorizationCheckerInterface $authorizationChecker, UserRepository $userRepository) { |
|
| 46 | 46 | $this->entityManager = $entityManager; |
| 47 | 47 | $this->authorizationChecker = $authorizationChecker; |
| 48 | 48 | $this->userRepository = $userRepository; |
| 49 | - } |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * @Route("/api/{version}/users/profile/all", methods={"GET"}, options={"expose"=true}, defaults={"version"="v2"}, name="swp_api_user_get_user_profiles") |
|
| 53 | - */ |
|
| 54 | - public function listProfiles() { |
|
| 51 | + /** |
|
| 52 | + * @Route("/api/{version}/users/profile/all", methods={"GET"}, options={"expose"=true}, defaults={"version"="v2"}, name="swp_api_user_get_user_profiles") |
|
| 53 | + */ |
|
| 54 | + public function listProfiles() { |
|
| 55 | 55 | $profiles = $this->userRepository->findAll(); |
| 56 | 56 | |
| 57 | 57 | return new SingleResourceResponse($profiles); |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * @Route("/api/{version}/users/profile/{id}", methods={"GET"}, options={"expose"=true}, defaults={"version"="v2"}, name="swp_api_user_get_user_profile") |
|
| 62 | - */ |
|
| 63 | - public function getAction($id) { |
|
| 60 | + /** |
|
| 61 | + * @Route("/api/{version}/users/profile/{id}", methods={"GET"}, options={"expose"=true}, defaults={"version"="v2"}, name="swp_api_user_get_user_profile") |
|
| 62 | + */ |
|
| 63 | + public function getAction($id) { |
|
| 64 | 64 | $requestedUser = $this->userRepository->find($id); |
| 65 | 65 | if (!is_object($requestedUser) || !$requestedUser instanceof UserInterface) { |
| 66 | - throw new NotFoundHttpException('Requested user don\'t exists'); |
|
| 66 | + throw new NotFoundHttpException('Requested user don\'t exists'); |
|
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | $this->checkIfCanAccess($requestedUser); |
| 70 | 70 | |
| 71 | 71 | return new SingleResourceResponse($requestedUser); |
| 72 | - } |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * @Route("/api/{version}/users/profile/{id}", methods={"PATCH"}, options={"expose"=true}, defaults={"version"="v2"}, name="swp_api_user_edit_user_profile") |
|
| 76 | - */ |
|
| 77 | - public function editAction(Request $request, $id, UserPasswordEncoderInterface $passwordEncoder) { |
|
| 74 | + /** |
|
| 75 | + * @Route("/api/{version}/users/profile/{id}", methods={"PATCH"}, options={"expose"=true}, defaults={"version"="v2"}, name="swp_api_user_edit_user_profile") |
|
| 76 | + */ |
|
| 77 | + public function editAction(Request $request, $id, UserPasswordEncoderInterface $passwordEncoder) { |
|
| 78 | 78 | $requestedUser = $this->userRepository->find($id); |
| 79 | 79 | if (!is_object($requestedUser) || !$requestedUser instanceof UserInterface) { |
| 80 | - throw new NotFoundHttpException('Requested user don\'t exists'); |
|
| 80 | + throw new NotFoundHttpException('Requested user don\'t exists'); |
|
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | $this->checkIfCanAccess($requestedUser); |
@@ -88,32 +88,32 @@ discard block |
||
| 88 | 88 | $form->handleRequest($request); |
| 89 | 89 | |
| 90 | 90 | if ($form->isSubmitted() && $form->isValid()) { |
| 91 | - if (!empty($form->get('plainPassword')->getData())) { |
|
| 91 | + if (!empty($form->get('plainPassword')->getData())) { |
|
| 92 | 92 | $requestedUser->setPassword( |
| 93 | 93 | $passwordEncoder->encodePassword( |
| 94 | 94 | $requestedUser, |
| 95 | 95 | $form->get('plainPassword')->getData() |
| 96 | 96 | ) |
| 97 | 97 | ); |
| 98 | - } |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - $entityManager = $this->entityManager; |
|
| 101 | - $entityManager->flush(); |
|
| 100 | + $entityManager = $this->entityManager; |
|
| 101 | + $entityManager->flush(); |
|
| 102 | 102 | |
| 103 | - return new SingleResourceResponse($requestedUser); |
|
| 103 | + return new SingleResourceResponse($requestedUser); |
|
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | return new SingleResourceResponse($form, new ResponseContext(400)); |
| 107 | - } |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - private function checkIfCanAccess($requestedUser) { |
|
| 109 | + private function checkIfCanAccess($requestedUser) { |
|
| 110 | 110 | /** @var UserInterface $currentUser */ |
| 111 | 111 | $currentUser = $this->getUser(); |
| 112 | 112 | if ( |
| 113 | 113 | !$this->authorizationChecker->isGranted('ROLE_ADMIN') && |
| 114 | 114 | $requestedUser->getId() !== $currentUser->getId() |
| 115 | 115 | ) { |
| 116 | - throw new AccessDeniedException('This user does not have access to this section. profile'); |
|
| 116 | + throw new AccessDeniedException('This user does not have access to this section. profile'); |
|
| 117 | + } |
|
| 117 | 118 | } |
| 118 | - } |
|
| 119 | 119 | } |