Ejemplo n.º 1
0
 /**
  * Registration step 2: Create user and set registration token
  *
  * @Route("/user/registration")
  * @Method("POST")
  * @Template("FOMUserBundle:Registration:form.html.twig")
  */
 public function register()
 {
     $user = new User();
     $form = $this->createForm(new UserRegistrationType(), $user);
     $form->bind($this->get('request'));
     //@TODO: Check if username and email are unique
     if ($form->isValid()) {
         $helper = new UserHelper($this->container);
         $helper->setPassword($user, $user->getPassword());
         $user->setRegistrationToken(hash("sha1", rand()));
         $user->setRegistrationTime(new \DateTime());
         $groupRepository = $this->getDoctrine()->getRepository('FOMUserBundle:Group');
         foreach ($this->container->getParameter('fom_user.self_registration_groups') as $groupTitle) {
             $group = $groupRepository->findOneByTitle($groupTitle);
             if ($group) {
                 $user->addGroups($group);
             } else {
                 $msg = sprintf('Self-registration group "%s" not found for user "%s"', $groupTitle, $user->getUsername());
                 $this->get('logger')->err($msg);
             }
         }
         $this->sendEmail($user);
         $em = $this->getDoctrine()->getManager();
         $em->persist($user);
         $em->flush();
         $helper->giveOwnRights($user);
         return $this->redirect($this->generateUrl('fom_user_registration_send'));
     }
     return array('user' => $user, 'form' => $form->createView(), 'form_name' => $form->getName());
 }
Ejemplo n.º 2
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $dialog = $this->getDialogHelper();
     $root = $this->getRoot();
     if ($root === null) {
         foreach (array('username', 'email', 'password') as $option) {
             if ($input->getOption($option) === null) {
                 throw new \RuntimeException(sprintf('The %s option must be provided.', $option));
             }
         }
     }
     $action = $root ? 'reset' : 'creation';
     if ($input->isInteractive() && !$input->getOption('silent')) {
         if (!$dialog->askConfirmation($output, $dialog->getQuestion('Do you confirm ' . $action, 'yes', '?'), true)) {
             return 1;
         }
     }
     if (!$root) {
         $root = new User();
         $root->setId(1);
     }
     if ($input->getOption('username') !== null) {
         //TODO: Validate, use same validator as in the askAndValidate below
         $root->setUsername($input->getOption('username'));
     }
     if ($input->getOption('email') !== null) {
         //TODO: Validate, use same validator as in the askAndValidate below
         $root->setEmail($input->getOption('email'));
     }
     if ($input->getOption('email') !== null) {
         //TODO: Validate, use same validator as in the askAndValidate below
         $helper = new UserHelper($this->getContainer());
         $helper->setPassword($root, $input->getOption('password'));
     }
     $em = $this->getContainer()->get('doctrine')->getManager();
     $em->persist($root);
     $em->flush();
     $output->writeln(array('', 'The root is now usable. Have fun!', ''));
 }
Ejemplo n.º 3
0
 /**
  * Password reset step 5: reset password
  *
  * @Route("/user/reset")
  * @Method("POST")
  * @Template("FOMUserBundle:Password:reset.html.twig")
  */
 public function passwordAction()
 {
     $token = $this->get('request')->get('token');
     if (!$token) {
         return $this->render('FOMUserBundle:Login:error-notoken.html.twig');
     }
     $user = $this->getDoctrine()->getRepository("FOMUserBundle:User")->findOneByResetToken($token);
     if (!$user) {
         $mail = $this->container->getParameter('fom_user.mail_from_address');
         return $this->render('FOMUserBundle:Login:error-notoken.html.twig', array('site_email' => $mail));
     }
     $max_token_age = $this->container->getParameter("fom_user.max_reset_time");
     if (!$this->checkTimeInterval($user->getResetTime(), $max_token_age)) {
         $form = $this->createForm('form');
         return $this->render('FOMUserBundle:Login:error-tokenexpired.html.twig', array('user' => $user, 'form' => $form->createView()));
     }
     $form = $this->createForm(new UserResetPassType(), $user);
     $form->bind($this->get('request'));
     if ($form->isValid()) {
         $em = $this->getDoctrine()->getManager();
         $user->setResetToken(null);
         $helper = new UserHelper($this->container);
         $helper->setPassword($user, $user->getPassword());
         $em->flush();
         return $this->redirect($this->generateUrl('fom_user_password_done'));
     }
     return array('user' => $user, 'form' => $form->createView());
 }
Ejemplo n.º 4
0
 /**
  * @ManagerRoute("/user/{id}/update")
  * @Method({ "POST" })
  * @Template("FOMUserBundle:User:form.html.twig")
  */
 public function updateAction($id)
 {
     $user = $this->getDoctrine()->getRepository('FOMUserBundle:User')->find($id);
     if ($user === null) {
         throw new NotFoundHttpException('The user does not exist');
     }
     // ACL access check
     $securityContext = $this->get('security.context');
     if (false === $securityContext->isGranted('EDIT', $user)) {
         throw new AccessDeniedException();
     }
     // If no password is given, we'll recycle the old one
     $request = $this->get('request');
     $userData = $request->get('user');
     $keepPassword = false;
     if ($userData['password']['first'] === '' && $userData['password']['second'] === '') {
         $userData['password'] = array('first' => $user->getPassword(), 'second' => $user->getPassword());
         $keepPassword = true;
     }
     if (!array_key_exists('username', $userData)) {
         $userData['username'] = $user->getUsername();
     }
     $groupPermission = $securityContext->isGranted('EDIT', new ObjectIdentity('class', 'FOM\\UserBundle\\Entity\\Group')) || $securityContext->isGranted('OWNER', $user);
     $profile = $this->addProfileForm($user);
     $form = $this->createForm(new UserType(), $user, array('requirePassword' => false, 'profile_formtype' => $profile['formtype'], 'group_permission' => $groupPermission, 'acl_permission' => $securityContext->isGranted('OWNER', $user), 'currentUser' => $securityContext->getToken()->getUser()));
     $form->bind($userData);
     if ($form->isValid()) {
         if (!$keepPassword) {
             // Set encrypted password and create new salt
             // The unencrypted password is already set on the user!
             $helper = new UserHelper($this->container);
             $helper->setPassword($user, $user->getPassword());
         }
         $em = $this->getDoctrine()->getManager();
         // This is the same check as abote in createForm for acl_permission
         if ($securityContext->isGranted('OWNER', $user)) {
             $aclManager = $this->get('fom.acl.manager');
             $aclManager->setObjectACLFromForm($user, $form->get('acl'), 'object');
         }
         $user->getProfile()->setUid($user);
         $em->flush();
         $this->get('session')->getFlashBag()->set('success', 'The user has been updated.');
         return $this->redirect($this->generateUrl('fom_user_user_index'));
     }
     return array('user' => $user, 'form' => $form->createView(), 'form_name' => $form->getName(), 'edit' => true, 'profile_template' => $profile['template'], 'profile_assets' => $profile['assets']);
 }