|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ApiBundle\Controller; |
| 4 | + |
| 5 | +use ApiBundle\Entity\User; |
| 6 | +use ApiBundle\Form\UserType; |
| 7 | +use ApiBundle\Form\UserProfileType; |
| 8 | + |
| 9 | +use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
| 10 | + |
| 11 | +use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
| 12 | +use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
| 13 | +use Symfony\Component\HttpFoundation\Request; |
| 14 | +use Symfony\Component\HttpFoundation\Response; |
| 15 | +use Symfony\Component\HttpFoundation\RedirectResponse; |
| 16 | + |
| 17 | +use Symfony\Component\HttpKernel\Exception\HttpException; |
| 18 | +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
| 19 | +use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
| 20 | +use Symfony\Component\Security\Core\Exception\AccountStatusException; |
| 21 | +use Symfony\Component\HttpFoundation\File\Exception\UploadException; |
| 22 | + |
| 23 | +use Symfony\Component\HttpFoundation\File\File; |
| 24 | +use Symfony\Component\HttpFoundation\File\UploadedFile; |
| 25 | + |
| 26 | +use FOS\UserBundle\Model\UserInterface; |
| 27 | + |
| 28 | +/** |
| 29 | + * Controller used to manage user contents in the backend. |
| 30 | + * |
| 31 | + * @Route("/user") |
| 32 | + * |
| 33 | + * @author Amarendra Kumar Sinha <[email protected]> |
| 34 | + */ |
| 35 | +class UserController extends Controller |
| 36 | +{ |
| 37 | + /** |
| 38 | + * Lists all User entities. |
| 39 | + * |
| 40 | + * @Route("/", name="user_index") |
| 41 | + * @Method("GET") |
| 42 | + */ |
| 43 | + public function indexAction() |
| 44 | + { |
| 45 | + return $this->redirectToRoute('homepage'); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Creates a new User entity. |
| 50 | + * |
| 51 | + * @Route("/new", name="user_new") |
| 52 | + * @Method({"GET", "POST"}) |
| 53 | + */ |
| 54 | + public function newAction(Request $request) |
| 55 | + { |
| 56 | + $confirmationEnabled = $this->container->getParameter('registration_requires_email_confirmation'); $userManager = $this->container->get('fos_user.user_manager'); |
| 57 | + |
| 58 | + $user = $userManager->createUser(); |
| 59 | + $user->setRoles(['ROLE_USER']); |
| 60 | + |
| 61 | + $form = $this->createForm(UserType::class, $user); |
| 62 | + |
| 63 | + $locale = $request->getLocale(); |
| 64 | + |
| 65 | + $form->handleRequest($request); |
| 66 | + |
| 67 | + if ($form->isSubmitted() && $form->isValid()) { |
| 68 | + // $file stores the uploaded Image file |
| 69 | + /** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */ |
| 70 | + $file = $user->getImage(); |
| 71 | + |
| 72 | + // If a file has been uploaded |
| 73 | + if ( null != $file ) { |
| 74 | + // Generate a unique name for the file before saving it |
| 75 | + $fileName = md5(uniqid()).'.'.$file->guessExtension(); |
| 76 | + |
| 77 | + // Move the file to the directory where images are stored |
| 78 | + $file->move($this->getParameter('images_profile_directory'), $fileName ); |
| 79 | + |
| 80 | + // Update the 'image' property to store the Image file name |
| 81 | + // instead of its contents |
| 82 | + $user->setImage($fileName); |
| 83 | + } |
| 84 | + |
| 85 | + $this->setUserData($user, $form); |
| 86 | + |
| 87 | + $userManager->updateUser($user); |
| 88 | + |
| 89 | + $authUser = false; |
| 90 | + if ($confirmationEnabled) { |
| 91 | + $this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail()); |
| 92 | + $route = 'fos_user_registration_check_email'; |
| 93 | + } else { |
| 94 | + $authUser = true; |
| 95 | + $route = 'fos_user_registration_confirmed'; |
| 96 | + } |
| 97 | + |
| 98 | + $this->logMessageAndFlash(200, 'success', 'User successfully created: ', $this->get('translator')->trans('flash.user_created_successfully'), $request->getLocale() ); |
| 99 | + $url = $this->container->get('router')->generate($route); |
| 100 | + $response = new RedirectResponse($url); |
| 101 | + |
| 102 | + if ($authUser) { |
| 103 | + $this->authenticateUser($user, $response); |
| 104 | + } |
| 105 | + |
| 106 | + return $response; |
| 107 | + } |
| 108 | + |
| 109 | + return $this->render('@ApiBundle/Resources/views/user/new.html.twig', [ |
| 110 | + 'form' => $form->createView(), |
| 111 | + 'attr' => array('enctype' => 'multipart/form-data'), |
| 112 | + ]); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Authenticate a user with Symfony Security |
| 117 | + * |
| 118 | + * @param \FOS\UserBundle\Model\UserInterface $user |
| 119 | + * @param \Symfony\Component\HttpFoundation\Response $response |
| 120 | + */ |
| 121 | + protected function authenticateUser(UserInterface $user, Response $response) |
| 122 | + { |
| 123 | + try { |
| 124 | + $this->container->get('fos_user.security.login_manager')->loginUser( |
| 125 | + $this->container->getParameter('fos_user.firewall_name'), |
| 126 | + $user, |
| 127 | + $response); |
| 128 | + } catch (AccountStatusException $ex) { |
| 129 | + // We simply do not authenticate users which do not pass the user |
| 130 | + // checker (not enabled, expired, etc.). |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Finds and displays a User entity. |
| 136 | + * |
| 137 | + * @Route("/profile-show/{id}", name="user_profile_show") |
| 138 | + * @Method("GET") |
| 139 | + */ |
| 140 | + public function showAction(User $user) |
| 141 | + { |
| 142 | + return $this->render('@ApiBundle/Resources/views/user/show.html.twig', [ |
| 143 | + 'user' => $user |
| 144 | + ]); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Displays a form to edit an existing User entity. |
| 149 | + * |
| 150 | + * @Route("/profile-edit/{id}", name="user_profile_edit") |
| 151 | + * @Method({"GET", "POST"}) |
| 152 | + */ |
| 153 | + public function editAction(User $user, Request $request) |
| 154 | + { |
| 155 | + $entityManager = $this->getDoctrine()->getManager(); |
| 156 | + |
| 157 | + $currentFilename = $user->getImage(); |
| 158 | + if ($user->getImage()) { |
| 159 | + $user->setImage( |
| 160 | + new File($this->getParameter('images_profile_directory').'/'.$currentFilename) |
| 161 | + ); |
| 162 | + } |
| 163 | + |
| 164 | + $editForm = $this->createForm(UserProfileType::class, $user); |
| 165 | + |
| 166 | + $locale = $request->getLocale(); |
| 167 | + |
| 168 | + $editForm->handleRequest($request); |
| 169 | + |
| 170 | + if ($editForm->isSubmitted() && $editForm->isValid()) { |
| 171 | + // $file stores the uploaded Image file |
| 172 | + /** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */ |
| 173 | + $file = $user->getImage(); |
| 174 | + |
| 175 | + // If a file has been uploaded |
| 176 | + if ( null != $file ) { |
| 177 | + // Generate a unique name for the file before saving it |
| 178 | + $fileName = md5(uniqid()).'.'.$file->guessExtension(); |
| 179 | + |
| 180 | + // Move the file to the directory where images are stored |
| 181 | + $file->move($this->getParameter('images_profile_directory'), $fileName ); |
| 182 | + |
| 183 | + // Update the 'image' property to store the Image file name |
| 184 | + // instead of its contents |
| 185 | + $user->setImage($fileName); |
| 186 | + } else { |
| 187 | + $user->setImage($currentFilename); |
| 188 | + } |
| 189 | + |
| 190 | + $this->setUserProfileData($user, $editForm); |
| 191 | + |
| 192 | + $entityManager = $this->getDoctrine()->getManager(); |
| 193 | + $entityManager->flush(); |
| 194 | + |
| 195 | + $this->logMessageAndFlash(200, 'success', 'User successfully updated: ', $this->get('translator')->trans('flash.user_updated_successfully'), $request->getLocale() ); |
| 196 | + |
| 197 | + $route = 'user_profile_show'; |
| 198 | + $url = $this->container->get('router')->generate($route, array('id' => $user->getId())); |
| 199 | + $response = new RedirectResponse($url); |
| 200 | + |
| 201 | + return $response; |
| 202 | + } |
| 203 | + |
| 204 | + return $this->render('@ApiBundle/Resources/views/user/edit.html.twig', [ |
| 205 | + 'user' => $user, |
| 206 | + 'current_image' => $currentFilename, |
| 207 | + 'edit_form' => $editForm->createView(), |
| 208 | + 'attr' => array('enctype' => 'multipart/form-data'), |
| 209 | + ]); |
| 210 | + } |
| 211 | + |
| 212 | + private function setUserData(User $user, \Symfony\Component\Form\Form $form) |
| 213 | + { |
| 214 | + $user->setFirstname($form['firstname']->getData()); |
| 215 | + $user->setLastname($form['lastname']->getData()); |
| 216 | + $user->setDob($form['dob']->getData()); |
| 217 | + $user->setEmail($form['email']->getData()); |
| 218 | + $user->setUsername($form['username']->getData()); |
| 219 | + $user->setPlainPassword($form['plainPassword']->getData()); |
| 220 | + $user->setRoles($form['roles']->getData()); |
| 221 | + $user->setConfirmationToken(null); |
| 222 | + $user->setEnabled(true); |
| 223 | + $user->setLastLogin(new \DateTime()); |
| 224 | + } |
| 225 | + |
| 226 | + private function setUserProfileData(User $user, \Symfony\Component\Form\Form $form) |
| 227 | + { |
| 228 | + $user->setFirstname($form['firstname']->getData()); |
| 229 | + $user->setLastname($form['lastname']->getData()); |
| 230 | + $user->setDob($form['dob']->getData()); |
| 231 | + $user->setConfirmationToken(null); |
| 232 | + $user->setEnabled(true); |
| 233 | + } |
| 234 | + |
| 235 | + private function logMessageAndFlash($code = 200, $type = 'success', $logMsg = '', $flashMsg = '', $locale = 'en') |
| 236 | + { |
| 237 | + $this->logMessage($code, $type, $logMsg); |
| 238 | + $this->addFlash($type, $flashMsg); |
| 239 | + } |
| 240 | + |
| 241 | + private function logMessage($code = 200, $type='success', $logMsg = '') { |
| 242 | + $logger = $this->get('logger'); |
| 243 | + |
| 244 | + if($type === 'success'){ |
| 245 | + $logger->info($code . ' ' . $logMsg); |
| 246 | + } else if($type === 'warning'){ |
| 247 | + $logger->warning($code . ' ' . $logMsg); |
| 248 | + } |
| 249 | + else if($type === 'danger'){ |
| 250 | + $logger->error($code . ' ' . $logMsg); |
| 251 | + } |
| 252 | + } |
| 253 | +} |
0 commit comments