Esempio n. 1
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $em = $this->container->getDoctrine()->getManager($input->getOption('em'));
     $user = new User();
     $email = $input->getArgument('email');
     if ($em->getRepository('CmsAuthentication:User')->findOneByEmail($email) !== null) {
         throw new \Exception(sprintf('User with email [%s] already exists.', $email));
     }
     $user->setEmail($email);
     $user->setEmailConfirmed(true);
     $user->setActive(true);
     $user->setLogin($email);
     $user->setName($input->getArgument('name') ? $input->getArgument('name') : $user->getLogin());
     $user->setRoles(array('ROLE_USER'));
     if ($input->getArgument('group')) {
         $group = $em->getRepository('CmsAuthentication:Group')->findOneByName($input->getArgument('group'));
         if ($group) {
             $user->setGroup($group);
         } else {
             throw new \Exception(sprintf('Group [%s] does not exist.', $input->getArgument('group')));
         }
     }
     $encoded = $this->container['cms_authentication.encoder_factory']->getEncoder($user)->encodePassword($input->getArgument('password'), $user->getSalt());
     $user->setPassword($encoded);
     $em->persist($user);
     $em->flush();
     $output->writeln('User created!');
 }
 /**
  * Insert action
  */
 public function insertAction(Request $request)
 {
     $email = $request->request->filter('email', null, false, FILTER_VALIDATE_EMAIL);
     $existingUser = $this->container->getDoctrine()->getRepository('CmsAuthentication:User')->findOneByEmail($email);
     if (!empty($existingUser)) {
         throw new CmsException(null, 'User with this email is already registered!');
     }
     $name = $request->request->get('name');
     $dummyGroupId = $request->request->get('group');
     $groupName = $this->dummyGroupIdToGroupName($dummyGroupId);
     $group = $this->container->getDoctrine()->getRepository('CmsAuthentication:Group')->findOneByName($groupName);
     //		$this->checkActionPermission($group, Group::PERMISSION_MODIFY_USER_NAME);
     $user = new User();
     $user->setLogin($email);
     $user->setName($name);
     $user->setEmail($email);
     $user->setGroup($group);
     $password = substr(md5(rand() . time()), 0, 8);
     $user->setPassword($this->encodePassword($user, $password));
     $this->container->getDoctrine()->getManager()->flush();
     $message = \Swift_Message::newInstance('SiteSupra new user', sprintf('Hi %s, you are registered to SiteSupra at %s, your password is: %s', $user->getLogin(), $request->getSchemeAndHttpHost(), $password));
     $message->setTo($user->getEmail());
     $message->setFrom($this->container->getParameter('framework.swiftmailer.default_from'));
     $this->container->getMailer()->send($message);
     $this->container->getDoctrine()->getManager()->persist($user);
     $this->container->getDoctrine()->getManager()->flush();
     return new SupraJsonResponse(array('user_id' => $user->getId()));
 }