Exemplo n.º 1
0
 /**
  * Displays a form to edit an existing User entity.
  *
  * @param integer $id      The ID of the User to edit
  * @param Request $request The Request
  *
  * @return RedirectResponse|Response
  */
 public function editAction($id, Request $request)
 {
     $user = $this->getEm()->getRepository('UnifikSystemBundle:User')->find($id);
     if (!$user) {
         $user = new User();
         $user->setContainer($this->container);
     }
     $this->pushNavigationElement($user);
     $form = $this->createForm(new UserType(), $user, array('validation_groups' => $user->getId() ? 'edit' : 'new', 'self_edit' => $user == $this->getUser(), 'developer' => $this->isDeveloper));
     if ($request->getMethod() == 'POST') {
         $previousEncodedPassword = $user->getPassword();
         $form->submit($request);
         if ($form->isValid()) {
             // All Users are automatically granted the ROLE_BACKEND_ACCESS Role
             $backendAccessRole = $this->getEm()->getRepository('UnifikSystemBundle:Role')->findOneBy(array('role' => 'ROLE_BACKEND_ACCESS'));
             if (!$backendAccessRole) {
                 $backendAccessRole = new Role();
                 $backendAccessRole->setRole('ROLE_BACKEND_ACCESS');
                 $this->getEm()->persist($backendAccessRole);
             }
             $user->addRole($backendAccessRole);
             // New password set
             if ($form->get('password')->getData()) {
                 $encoder = $this->get('security.encoder_factory')->getEncoder($user);
                 $encodedPassword = $encoder->encodePassword($user->getPassword(), $user->getSalt());
             } else {
                 $encodedPassword = $previousEncodedPassword;
             }
             $user->setPassword($encodedPassword);
             // Forcing the selected locale by the user
             if ($locale = $user->getLocale()) {
                 $request->setLocale($locale);
             }
             $this->getEm()->persist($user);
             $this->getEm()->flush();
             $this->addFlashSuccess($this->get('translator')->trans('%entity% has been saved.', array('%entity%' => $user)));
             if ($request->request->has('save')) {
                 return $this->redirect($this->generateUrl('unifik_system_backend_user'));
             }
             return $this->redirect($this->generateUrl('unifik_system_backend_user_edit', array('id' => $user->getId())));
         } else {
             $this->addFlashError('Some fields are invalid.');
         }
     }
     return $this->render('UnifikSystemBundle:Backend/User/User:edit.html.twig', array('user' => $user, 'form' => $form->createView()));
 }
Exemplo n.º 2
0
 /**
  * @param InputInterface $input
  * @param OutputInterface $output
  */
 private function doAdministrationSetup(InputInterface $input, OutputInterface $output)
 {
     $output->writeln('');
     $output->writeln('<info>Administration setup.</info>');
     $output->writeln('');
     $dialog = $this->getHelperSet()->get('dialog');
     $user = new User();
     $user->setUsername($dialog->ask($output, '<question>Username:</question> '));
     $user->setFirstname($dialog->ask($output, '<question>Firstname:</question> '));
     $user->setLastname($dialog->ask($output, '<question>Lastname:</question> '));
     $user->setEmail($dialog->ask($output, '<question>Email:</question> '));
     $user->setPassword($dialog->ask($output, '<question>Password:</question> '));
     $user->setSalt(uniqid());
     $user->setActive(true);
     // password hashing
     $encoder = $this->getContainer()->get('security.encoder_factory')->getEncoder($user);
     $encodedPassword = $encoder->encodePassword($user->getPassword(), $user->getSalt());
     $user->setPassword($encodedPassword);
     // default roles
     $roleRepo = $this->getContainer()->get('doctrine')->getRepository('UnifikSystemBundle:Role');
     $roleRepo->setContainer($this->getContainer());
     $roleRepo->setCurrentAppName('backend');
     $role = $roleRepo->findOneByRole('ROLE_BACKEND_ADMIN');
     $user->addRole($role);
     $em = $this->getContainer()->get('doctrine.orm.entity_manager');
     $em->persist($user);
     $em->flush();
 }