/** * Fetches the journal user * @param array $cache User cache * @param String $email User's email * @param Journal $journal Journal * @return JournalUser Imported or retrieved JournalUser */ private function getJournalUser(&$cache, $email, $journal) { if (!empty($cache[$email]['journal_user'])) { return $cache[$email]['journal_user']; } $journalUser = $this->em->getRepository('OjsJournalBundle:JournalUser')->findOneBy(['journal' => $journal, 'user' => $cache[$email]['user']]); if ($journalUser === null) { $journalUser = new JournalUser(); $journalUser->setUser($cache[$email]['user']); $journalUser->setJournal($journal); $this->em->persist($journalUser); } $cache[$email]['journal_user'] = $journalUser; return $cache[$email]['journal_user']; }
/** * Creates a new User entity. * * @param Request $request * @return RedirectResponse|Response */ public function createUserAction(Request $request) { $journal = $this->get('ojs.journal_service')->getSelectedJournal(); $eventDispatcher = $this->get('event_dispatcher'); if (!$this->isGranted('CREATE', $journal, 'userRole')) { throw new AccessDeniedException("You are not authorized for this page!"); } $entity = new User(); $form = $this->createCreateForm($entity, $journal->getId()); $form->handleRequest($request); if ($form->isValid()) { $em = $this->getDoctrine()->getManager(); $factory = $this->get('security.encoder_factory'); $encoder = $factory->getEncoder($entity); $password = $encoder->encodePassword($entity->getPassword(), $entity->getSalt()); $entity->setPassword($password); $entity->setEnabled(true); $em->persist($entity); $journalUser = new JournalUser(); $journalUser->setUser($entity); $journalUser->setJournal($journal); $event = new JournalItemEvent($journalUser); $eventDispatcher->dispatch(JournalUserEvents::PRE_CREATE, $event); $em->persist($event->getItem()); $em->flush(); $event = new JournalItemEvent($event->getItem()); $eventDispatcher->dispatch(JournalUserEvents::POST_CREATE, $event); if ($event->getResponse()) { return $event->getResponse(); } $this->successFlashBag('successful.create'); return $this->redirectToRoute('ojs_journal_user_edit', ['journalId' => $journal->getId(), 'id' => $journalUser->getId()]); } return $this->render('OjsJournalBundle:JournalUser:new.html.twig', array('entity' => $entity, 'form' => $form->createView())); }
/** * Creates a new User entity. * * @param Request $request * @return RedirectResponse|Response */ public function createUserAction(Request $request) { $journal = $this->get('ojs.journal_service')->getSelectedJournal(); if (!$this->isGranted('CREATE', $journal, 'userRole')) { throw new AccessDeniedException("You are not authorized for this page!"); } $entity = new User(); $form = $this->createCreateForm($entity, $journal->getId()); $form->handleRequest($request); if ($form->isValid()) { $em = $this->getDoctrine()->getManager(); $formData = $form->getData(); $factory = $this->get('security.encoder_factory'); $encoder = $factory->getEncoder($entity); $password = $encoder->encodePassword($entity->getPassword(), $entity->getSalt()); $entity->setPassword($password); $entity->setAvatar($request->get('user_avatar')); $em->persist($entity); $journalUser = new JournalUser(); $journalUser->setUser($entity); $journalUser->setJournal($journal); if (count($formData->getJournalRoles()) > 0) { $journalUser->setRoles($formData->getJournalRoles()); } $em->persist($journalUser); $em->flush(); $this->successFlashBag('successful.create'); return $this->redirectToRoute('ojs_journal_user_index', ['journalId' => $journal->getId()]); } return $this->render('OjsJournalBundle:JournalUser:new.html.twig', array('entity' => $entity, 'form' => $form->createView())); }