예제 #1
0
 /**
  * Facebook connect login
  *
  * @Route("/fblogin", name="user_fblogin")
  * @Template()
  */
 public function fbloginAction()
 {
     $request = $this->getRequest();
     $back = $request->query->get('back');
     if ($back) {
         $session = $this->getRequest()->getSession();
         $session->set('back', $back);
     }
     require __DIR__ . '/../../../../vendor/facebook/examples/example.php';
     // login ok ?
     if (isset($user_profile['id'])) {
         $session = $request->getSession();
         // existe usuario en la bd?
         $em = $this->getDoctrine()->getEntityManager();
         $user = $em->getRepository('ApplicationUserBundle:User')->findOneBy(array('facebook_id' => $user_profile['id']));
         if (!$user) {
             $user = $em->getRepository('ApplicationUserBundle:User')->findOneBy(array('email' => $user_profile['email']));
         }
         if (!$user) {
             if (!isset($user_profile['location']['name'])) {
                 $user_profile['location']['name'] = '';
             }
             if (!isset($user_profile['website'])) {
                 $user_profile['website'] = '';
             }
             // usuario referido existe?
             $ref_id = $session->get('ref_id');
             if ($ref_id) {
                 $user_ref = $em->getRepository('ApplicationUserBundle:User')->find($ref_id);
                 if (!$user_ref) {
                     $ref_id = null;
                 }
             }
             if (!$ref_id) {
                 $ref_id = null;
             }
             $user = new \Application\UserBundle\Entity\User();
             $user->setFacebookId($user_profile['id']);
             $user->setCategoryId(13);
             $user->setEmail($user_profile['email']);
             $user->setName($user_profile['name']);
             $user->setLocation($user_profile['location']['name']);
             $user->setDate(new \DateTime("now"));
             $user->setUrl($user_profile['website']);
             $user->setRefId($ref_id);
             $user->setCanContact(1);
             $user->setAvatarType(AVATAR_FACEBOOK);
             $user->setSlug(Util::slugify($user->getName()));
             $url = $this->generateUrl('user_edit') . '#fblogin';
         } else {
             // cookie
             $pass = $user->getPass();
             if (!$pass) {
                 $pass = md5($user->getDate()->format('Y-m-d H:i:s'));
             }
             setcookie("login", $user->getId() . ':' . $pass, time() + 3600 * 24 * 31, "/");
             $back = $session->get('back');
             if ($back) {
                 $url = $back;
                 $session->set('back', '');
             } else {
                 $url = $this->generateUrl('user_show', array('id' => $user->getId(), 'slug' => $user->getSlug()));
             }
         }
         // guardar ultimo login
         $user->setDateLogin(new \DateTime("now"));
         // guardar facebook id
         if (isset($user_profile['id']) && !$user->getFacebookId()) {
             $user->setFacebookId($user_profile['id']);
         }
         // guardar ip
         $user->setIp();
         // guardar total logins
         $total_logins = (int) $user->getTotalLogins() + 1;
         $user->setTotalLogins($total_logins);
         $em = $this->get('doctrine.orm.entity_manager');
         $em->persist($user);
         $em->flush();
         $session->set('id', $user->getId());
         $session->set('name', $user->getShortName());
         $session->set('slug', $user->getSlug());
         $session->set('admin', $user->getAdmin());
         $session->set('moderator', $user->getModerator());
     } else {
         $url = $loginUrl;
     }
     return $this->redirect($url);
 }