Esempio n. 1
0
 /**
  * Edit user action.
  *
  * @param Application $app
  * @param Request $request
  * @param int $id
  * @return Response
  * @throws NotFoundHttpException if no user is found with that ID.
  */
 public function editAction(Application $app, Request $request, $id)
 {
     $errors = array();
     $user = $this->userManager->getUser($id);
     if (!$user) {
         throw new NotFoundHttpException('No user was found with that ID.');
     }
     if ($request->isMethod('POST')) {
         $user->setName($request->request->get('name'));
         $user->setEmail($request->request->get('email'));
         if ($request->request->get('password')) {
             if ($request->request->get('password') != $request->request->get('confirm_password')) {
                 $errors['password'] = '******'t match.';
             } else {
                 $this->userManager->setUserPassword($user, $request->request->get('password'));
             }
         }
         if ($app['security']->isGranted('ROLE_ADMIN') && $request->request->has('roles')) {
             $user->setRoles($request->request->get('roles'));
         }
         $errors += $this->userManager->validate($user);
         if (empty($errors)) {
             $this->userManager->update($user);
             $msg = 'Saved account information.' . ($request->request->get('password') ? ' Changed password.' : '');
             $app['session']->getFlashBag()->set('alert', $msg);
         }
     }
     return $app['twig']->render($this->editTemplate, array('layout_template' => $this->layoutTemplate, 'error' => implode("\n", $errors), 'user' => $user, 'available_roles' => array('ROLE_USER', 'ROLE_ADMIN'), 'image_url' => $this->getGravatarUrl($user->getEmail())));
 }