public function migrate() { $em = $this->container->get('Doctrine')->getManager(); $u = new User(); $u->setName('Anonymous'); $u->setEmail(User::REVIEW_USER_EMAIL); $u->setPassword('NEVERLOGSIN' . $this->generateRandomString()); $em->persist($u); $em->flush(); }
public function testTemplatesAreBeingRendered() { $t = $this->container->get('templating'); $u = new User(); $u->setName('Dummy User'); $u->setEmail('*****@*****.**'); $courses = array(); $course = array('id' => 5, 'name' => 'Dummy Course', 'shortDesc' => 'Dummy Course', 'initiative' => array('name' => 'Independent', 'code' => 'Independent'), 'slug' => 'fake_slug', 'nextOffering' => array('displayDate' => '2014-05-05', 'url' => 'http://example.com'), 'institutions' => array()); $courses[] = array('interested' => false, 'id' => 5, 'course' => $course); $counts = array('offeringCount' => array('recent' => '50', 'selfpaced' => '100')); $html = $t->renderResponse('ClassCentralMOOCTrackerBundle:Reminder:multiple.courses.inlined.html', array('courses' => $courses, 'baseUrl' => $this->container->getParameter('baseurl'), 'user' => $u, 'jobType' => CourseStartReminderJob::JOB_TYPE_1_DAY_BEFORE, 'counts' => $counts, 'unsubscribeToken' => CryptUtility::getUnsubscribeToken($u, UserPreference::USER_PREFERENCE_MOOC_TRACKER_COURSES, $this->container->getParameter('secret'))))->getContent(); $this->assertNotEmpty($html); $html = $t->renderResponse('ClassCentralMOOCTrackerBundle:Reminder:single.course.inlined.html', array('course' => $course, 'baseUrl' => $this->container->getParameter('baseurl'), 'interested' => false, 'user' => $u, 'counts' => $counts, 'jobType' => CourseStartReminderJob::JOB_TYPE_1_DAY_BEFORE, 'unsubscribeToken' => CryptUtility::getUnsubscribeToken($u, UserPreference::USER_PREFERENCE_MOOC_TRACKER_COURSES, $this->container->getParameter('secret'))))->getContent(); $this->assertNotEmpty($html); }
public function fbReceiveAuthorizationCodeAction(Request $request) { $em = $this->getDoctrine()->getManager(); $userService = $this->get('user_service'); $userSession = $this->get('user_session'); $src = $this->get('session')->get('fb_auth_src'); $logger = $this->get('logger'); $logger->info("FBAUTH: FB auth redirect"); $helper = $this->getFBLoginHelper(); try { $session = $helper->getSessionFromRedirect(); if (!$session) { // Redirect to the signup page $logger->info("FBAUTH: FB auth denied by the user"); return $this->redirect($this->generateUrl('signup')); } $fbRequest = new FacebookRequest($session, 'GET', '/me'); $fbUser = $fbRequest->execute()->getGraphObject(GraphUser::className()); $email = $fbUser->getEmail(); if (!$email) { // TODO : Render error page $logger->error("FBAUTH: Email missing"); echo "Email is required. Please revoke Class Central App from your <a href='https://www.facebook.com/settings?tab=applications'>Facebook settings page</a> and then signup again."; exit; } $name = $fbUser->getName(); $fbId = $fbUser->getId(); // Check if the fb users has logged in before using the FB Id $usersFB = $em->getRepository('ClassCentralSiteBundle:UserFb')->findOneBy(array('fbId' => $fbId)); if ($usersFB) { $user = $usersFB->getUser(); } else { // Check if an account with this email address exist. If it does then merge // these accounts $user = $em->getRepository('ClassCentralSiteBundle:User')->findOneBy(array('email' => $email)); } if ($user) { $userService->login($user); $userSession->setPasswordLessLogin(true); // Check whether the user has fb details $ufb = $user->getFb(); if ($ufb) { $logger->info("FBAUTH: FB user exists"); } else { $logger->info("FBAUTH: Email exists but UserFb table is empty"); // Create a FB info $ufb = new UserFb(); $ufb->setFbEmail($email); $ufb->setFbId($fbId); $ufb->setUser($user); } $em->persist($ufb); $em->flush(); $userSession->login($user, true); $redirectUrl = $this->getLastAccessedPage($request->getSession()) ? $this->getLastAccessedPage($request->getSession()) : $this->generateUrl('user_library'); $logger->info(' LOGIN REDIRECT URL ' . $redirectUrl); return $this->redirect($redirectUrl); } else { $logger->info("FBAUTH: New user"); $newsletterService = $this->get('newsletter'); $newsletter = $em->getRepository('ClassCentralSiteBundle:Newsletter')->findOneByCode('mooc-report'); // Create a new account $user = new User(); $user->setEmail($email); $user->setName($name); $user->setPassword($this->getRandomPassword()); // Set a random password $user->setIsverified(true); $user->setSignupType(User::SIGNUP_TYPE_FACEBOOK); $redirectUrl = $userService->createUser($user, false, empty($src) ? 'facebook' : $src); $userSession->setPasswordLessLogin(true); // Set the variable to show that the user didn't use a password to login // Create a FB info $ufb = new UserFb(); $ufb->setFbEmail($email); $ufb->setFbId($fbId); $ufb->setUser($user); $em->persist($ufb); $em->flush(); $this->uploadFacebookProfilePic($user, $fbId); // Subscribe to newsletter $subscribed = $newsletterService->subscribeUser($newsletter, $user); $logger->info("preferences subscribed : email newsletter subscription", array('email' => $user->getId(), 'newsletter' => $newsletter->getCode(), 'subscribed' => $subscribed)); return $this->redirect($redirectUrl); } } catch (FacebookRequestException $e) { $logger->info("FBAUTH: FB Auth error - " . $e->getMessage()); return null; } catch (\Exception $e) { $logger->info("FBAUTH: Api exception" . $e->getMessage()); return null; } }
public function moocTrackerSignupAction(Request $request) { $userSession = $this->get('user_session'); $userService = $this->get('user_service'); $session = $this->get('session'); $em = $this->get('doctrine')->getManager(); // Redirect user if already logged in if ($this->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')) { return $this->redirect($this->generateUrl('user_library')); } $email = $userSession->getNewsletterUserEmail(); $emailEntity = $em->getRepository('ClassCentralSiteBundle:Email')->findOneByEmail($email); if (empty($emailEntity)) { // Redirect to signup page $this->redirect($this->generateUrl('signup')); } $password = $request->request->get('password'); if (empty($password)) { $session->getFlashBag()->add('newsletter_signup_invalid_password', "Please enter a password"); return $this->redirect($this->generateUrl('newsletter_subscribed')); } $name = $request->request->get('name'); if (empty($name)) { $session->getFlashBag()->add('newsletter_signup_invalid_password', "Please enter a name"); return $this->redirect($this->generateUrl('newsletter_subscribed')); } // Password-Email are accurate. Create a user $user = new User(); $user->setEmail($email); $user->setPassword($password); $user->setName($name); foreach ($emailEntity->getNewsletters() as $newsletter) { $user->addNewsletter($newsletter); } $user = $userService->signup($user, false); // don't send an email verification. // Clean the email entities subscriptions $newsletters = $emailEntity->getNewsletters(); $newsletters->clear(); $em->persist($emailEntity); $em->flush(); // Redirect to MOOC Tracker page return $this->redirect($this->generateUrl('user_profile', array('slug' => $user->getId(), 'tab' => 'edit-profile', 'ref' => 'user_created', 'src' => 'newsletter'))); }
public function signup(\ClassCentral\SiteBundle\Entity\User $user, $emailVerification = true) { $em = $this->container->get('doctrine')->getManager(); $templating = $this->container->get('templating'); $mailgun = $this->container->get('mailgun'); $verifyTokenService = $this->container->get('verification_token'); $userSession = $this->container->get('user_session'); $user->setEmail(strtolower($user->getEmail())); // Normalize the email $password = $user->getPassword(); $user->setPassword($user->getHashedPassword($password)); // If the email has subscriptions to different newsletters, transfer it over to this user $emailEntity = $em->getRepository('ClassCentralSiteBundle:Email')->findOneByEmail($user->getEmail()); if ($emailEntity) { foreach ($emailEntity->getNewsletters() as $newsletter) { $user->addNewsletter($newsletter); } } $em->persist($user); $em->flush(); // Create user prefrences for the user $this->initPreferences($user); // Login the user $this->login($user); // Create a successfull signup notification $userSession->notifyUser(UserSession::FLASH_TYPE_SUCCESS, 'Account successfully created', "You can now build your own library of courses by adding them to <a href='/user/courses''>My Courses</a>", 30); // Send a welcome email but not in the test environment if ($this->container->getParameter('kernel.environment') != 'test') { $name = $user->getName() ? ucwords($user->getName()) : ""; $html = $templating->renderResponse('ClassCentralSiteBundle:Mail:welcome.html.twig', array('name' => $name, 'loginToken' => $this->getLoginToken($user), 'baseUrl' => $this->container->getParameter('baseurl'), 'utm' => array('medium' => Mailgun::UTM_MEDIUM, 'campaign' => 'new_user_welcome', 'source' => Mailgun::UTM_SOURCE_PRODUCT)))->getContent(); $mailgunResponse = $mailgun->sendIntroEmail($user->getEmail(), "'Dhawal Shah'<*****@*****.**>", "Welcome to Class Central, what else can you learn?", $html); if ($emailVerification) { // Send an email for verification $value = array('verify' => 1, 'email' => $user->getEmail()); $tokenEntity = $verifyTokenService->create($value, \ClassCentral\SiteBundle\Entity\VerificationToken::EXPIRY_1_YEAR); $html = $templating->renderResponse('ClassCentralSiteBundle:Mail:confirm.email.html.twig', array('token' => $tokenEntity->getToken()))->getContent(); $mailgunResponse = $mailgun->sendSimpleText($user->getEmail(), "*****@*****.**", "Please confirm your email", $html); // Send user a notification about this email $userSession->notifyUser(UserSession::FLASH_TYPE_NOTICE, 'Confirm your email address', "A confirmation email has been sent to <b>{$user->getEmail()}</b>. Click on the confirmation link in the email to activate your account", 60); } } return $user; }