/** * @param User $user */ private function authenticateUser(User $user) { $providerKey = 'main'; // firewall name $token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles()); $this->get('security.token_storage')->setToken($token); $this->get('session')->save(); }
/** * @param UserInterface|User $user * @param string $subject * @param string $body */ public function sendToUser(User $user, $subject, $body) { if (!empty($subject) && !empty($body) && !empty($user->getEmail()) && !empty($user->getUsername())) { if ($this->preventMailMerge) { $subject = $subject . ' rand:' . rand(0, 10000); } $this->send($subject, $body, $user->getEmail(), $user->getUsername()); } }
/** * @param Request $request * @param User $user * @return RedirectResponse|Response */ public function sendMailAction(Request $request, User $user) { $journal = $this->get('ojs.journal_service')->getSelectedJournal(); $em = $this->getDoctrine()->getManager(); $data = []; $serializer = $this->get('serializer'); $session = $this->get('session'); if ($request->isMethod('POST')) { $mailData = $request->get('mail'); $mailer = $this->get('mailer'); $message = $mailer->createMessage()->setFrom($this->container->getParameter('system_email'))->setTo($user->getEmail())->setSubject($mailData['subject'])->setBody($mailData['body'])->setContentType('text/html'); $mailer->send($message); $session->getFlashBag()->add('success', $this->get('translator')->trans('Email sending succefully.')); $session->save(); return $this->redirect($this->get('router')->generate('ujr_show_users_ofjournal', ['journal_id' => $journal->getId()])); } /** @var QueryBuilder $qb */ $qb = $em->createQueryBuilder(); $qb->select('t')->from('OjsJournalBundle:MailTemplate', 't')->where($qb->expr()->orX($qb->expr()->isNull('t.journalId'), $qb->expr()->eq('t.journalId', ':journal')))->setParameter('journal', $journal->getId()); $templates = $qb->getQuery()->getResult(); $data['templates'] = $templates; $data['user'] = $user; $data['parameters'] = $request->query->all(); array_walk($data['parameters'], function (&$val) { $val = urldecode($val); }); $data['templateVars'] = json_encode(array_merge(array('journal' => json_decode($serializer->serialize($journal, 'json')), 'user' => json_decode($serializer->serialize($this->getUser(), 'json'))), $data['parameters'])); $yamlParser = new Yaml\Parser(); $defaultTemplates = $yamlParser->parse(file_get_contents($this->container->getParameter('kernel.root_dir') . '/../src/Ojs/JournalBundle/Resources/data/mailtemplates.yml')); $tplKey = $request->get('template'); $data['selectedTemplate'] = $tplKey ? isset($defaultTemplates[$tplKey]) ? json_encode($defaultTemplates[$tplKey]) : null : null; return $this->render('OjsJournalBundle:JournalRole:send_mail.html.twig', $data); }
public function registerAction(Request $request) { $error = null; $user = new User(); $session = $this->get('session'); //Add default data for oauth login $oauth_login = $session->get('oauth_login', false); if ($oauth_login) { $name = explode(' ', $oauth_login['full_name']); $firstName = $name[0]; unset($name[0]); $lastName = implode(' ', $name); $user->setFirstName($firstName)->setLastName($lastName)->setUsername($this->slugify($oauth_login['full_name'])); } $form = $this->createForm(new RegisterFormType(), $user); $form->handleRequest($request); if ($form->isValid()) { // check user name exists $em = $this->getDoctrine()->getManager(); $user->setPassword($this->encodePassword($user, $user->getPassword())); $user->setToken($user->generateToken()); $user->generateApiKey(); $user->setStatus(1); $user->setIsActive(0); $em->persist($user); if ($oauth_login) { $oauth = new UserOauthAccount(); $oauth->setProvider($oauth_login['provider'])->setProviderAccessToken($oauth_login['access_token'])->setProviderRefreshToken($oauth_login['refresh_token'])->setProviderUserId($oauth_login['user_id'])->setUser($user); $em->persist($oauth); $user->addOauthAccount($oauth); $em->persist($user); } $em->flush(); //$this->authenticateUser($user); // auth. user $session->getFlashBag()->add('success', 'Success. <br>You are registered. Check your email to activate your account.'); $session->remove('oauth_login'); $session->save(); $event = new UserEvent($user); $dispatcher = $this->get('event_dispatcher'); $dispatcher->dispatch('user.register.complete', $event); return $this->redirect($this->generateUrl('login')); } return $this->render('OjsUserBundle:Security:register.html.twig', array('form' => $form->createView(), 'errors' => $form->getErrors())); }
/** * @param AdminEvent $event */ public function onJournalApplicationHappen(AdminEvent $event) { /** @var Journal $journal */ $journal = $event->getEntity(); $getMailEvent = $this->ojsMailer->getEventByName(AdminEvents::JOURNAL_APPLICATION_HAPPEN); if (!$getMailEvent) { goto lookforapplicationuser; } //send to admin user group foreach ($this->ojsMailer->getAdminUsers() as $user) { $transformParams = ['journal.title' => $journal->getTitle(), 'receiver.username' => $user->getUsername(), 'receiver.fullName' => $user->getFullName()]; $template = $this->ojsMailer->transformTemplate($getMailEvent->getTemplate(), $transformParams); $this->ojsMailer->sendToUser($user, $getMailEvent->getSubject(), $template); } lookforapplicationuser: //send to applier user $getMailEvent = $this->ojsMailer->getEventByName(AdminEvents::JOURNAL_APPLICATION_HAPPEN . '.application.user'); if (!$getMailEvent) { return; } $transformParams = ['journal.title' => $journal->getTitle(), 'journal.phone' => $journal->getPhone(), 'journal.address' => $journal->getAddress()]; if ($this->ojsMailer->currentUser() instanceof UserInterface) { $user = $this->ojsMailer->currentUser(); } else { $user = new User(); $user->setEmail($journal->getEmail())->setUsername($this->ojsMailer->translator->trans('journal.manager')); } $template = $this->ojsMailer->transformTemplate($getMailEvent->getTemplate(), $transformParams); $this->ojsMailer->sendToUser($user, $getMailEvent->getSubject(), $template); }
/** * Imports the given user * @param int $id Old ID * @param bool|true $flush Should be true if the entity should get flushed * @return null|User Newly imported user * @throws \Doctrine\DBAL\DBALException */ public function importUser($id, $flush = true) { $this->consoleOutput->writeln("Reading user #" . $id . "... ", true); $sql = "SELECT * FROM users WHERE users.user_id = :id " . ImportHelper::spamUsersFilterSql(); $statement = $this->dbalConnection->prepare($sql); $statement->bindValue('id', $id); $statement->execute(); $faker = Factory::create(); $pkpUser = $statement->fetch(); $user = null; if ($pkpUser) { if (!empty($pkpUser['username'])) { $user = $this->em->getRepository('OjsUserBundle:User')->findOneBy(['username' => $pkpUser['username']]); if (!$user) { $user = $this->em->getRepository('OjsUserBundle:User')->findOneBy(['email' => $pkpUser['email']]); } } if (is_null($user)) { $user = new User(); !empty($pkpUser['username']) ? $user->setUsername($pkpUser['username']) : $user->setUsername($faker->userName); !empty($pkpUser['email']) ? $user->setEmail($pkpUser['email']) : $user->setEmail($faker->companyEmail); !empty($pkpUser['disabled']) ? $user->setEnabled(!$pkpUser['disabled']) : $user->setEnabled(1); // Set a random password $password = mb_substr($this->tokenGenerator->generateToken(), 0, 8); $user->setPlainPassword($password); // Fields which can't be blank !empty($pkpUser['first_name']) ? $user->setFirstName($pkpUser['first_name']) : $user->setFirstName('Anonymous'); !empty($pkpUser['last_name']) ? $user->setLastName($pkpUser['last_name']) : $user->setLastName('Anonymous'); // Optional fields !empty($pkpUser['billing_address']) && $user->setBillingAddress($pkpUser['billing_address']); !empty($pkpUser['mailing_address']) && $user->setAddress($pkpUser['mailing_address']); !empty($pkpUser['gender']) && $user->setGender($pkpUser['gender']); !empty($pkpUser['phone']) && $user->setPhone($pkpUser['phone']); !empty($pkpUser['fax']) && $user->setFax($pkpUser['fax']); !empty($pkpUser['url']) && $user->setUrl($pkpUser['url']); $this->em->persist($user); if ($flush) { $this->em->flush(); } } return $user; } return null; }
/** * @param User $user * @param string $subject * @param string $body */ private function sendMail(User $user, $subject, $body) { $message = $this->mailer->createMessage(); $to = array($user->getEmail() => $user->getUsername()); $message = $message->setSubject($subject)->addFrom($this->mailSender, $this->mailSenderName)->setTo($to)->setBody($body, 'text/html'); $this->mailer->send($message); }