예제 #1
0
 def current_user(self):
     """Returns the active user, or None if the user has not logged in."""
     if not hasattr(self, "_current_user"):
         self._current_user = None
         cookie = facebook.get_user_from_cookie(
             self.request.COOKIES, settings.FACEBOOK_APP_ID, settings.FACEBOOK_APP_SECRET)
         if cookie:
             # Store a local instance of the user data so we don't need
             # a round-trip to Facebook on every request
             try:
                 user = FacebookUser.objects.get(uid=cookie["uid"])
             except FacebookUser.DoesNotExist:
                 graph = facebook.GraphAPI(cookie["access_token"])
                 profile = graph.get_object("me")
                 user = FacebookUser(pk=str(profile["id"]),
                                     name=profile["name"],
                                     profile_url=profile["link"],
                                     access_token=cookie["access_token"])
                 user.save()
             else:
                 if user.access_token != cookie["access_token"]:
                     user.access_token = cookie["access_token"]
                     user.save()
             self._current_user = user
     return self._current_user
예제 #2
0
 def get_current_user(self, request):
     """Provides access to the active Facebook user in self.current_user
     The property is lazy-loaded on first access, using the cookie saved
     by the Facebook JavaScript SDK to determine the user ID of the active
     user. See http://developers.facebook.com/docs/authentication/ for
     more information.
     """
     """The active user, or None if the user has not logged in."""
     
     cookie = facebook.get_user_from_cookie(
         request.COOKIES, self.__FACEBOOK_APP_ID, self.__FACEBOOK_APP_SECRET)
     if cookie:
         # Store a local instance of the user data so we don't need
         # a round-trip to Facebook on every request
         self.__is_a_new_user = False
         try:
             user = FacebookUser.objects.get(uid=cookie["uid"], aid=self.__FACEBOOK_APP_ID)
         except FacebookUser.DoesNotExist:
             graph = facebook.GraphAPI(cookie["access_token"])
             profile = graph.get_object("me")
             user = FacebookUser(uid=str(profile["id"]), 
                                 aid=self.__FACEBOOK_APP_ID,
                                 profile_url=profile["link"],
                                 access_token=cookie["access_token"])
             user.save()
             self.__is_a_new_user = True
         else:
             # Update the access token!
             graph = facebook.GraphAPI(cookie["access_token"])
             if user.access_token != cookie["access_token"]:
                 user.access_token = cookie["access_token"]
                 user.save()
         self.__current_user = user
         
     return self.__current_user
예제 #3
0
    def current_user(self):
        """Returns the active user, or None if the user has not logged in."""
        if not hasattr(self, "_current_user"):

            self._current_user = None

            if self.cookie:
                cookie = self.cookie
            else:
                cookie = self.parse_auth()

            if cookie:
                #                print ('we have cookie', cookie["uid"])
                # Store a local instance of the user data so we don't need
                # a round-trip to Facebook on every request
                try:
                    user = FacebookUser.objects.get(uid=cookie["uid"])
                except FacebookUser.DoesNotExist:
                    try:
                        graph = facebook.GraphAPI(cookie["access_token"])

                        attempt_counter = 0
                        ATTEMPT_LIMIT = 10

                        while attempt_counter < ATTEMPT_LIMIT:
                            try:
                                profile = graph.get_object("me")
                                break
                            except (IOError) as e:
                                attempt_counter += 1

                                if attempt_counter == ATTEMPT_LIMIT:
                                    raise

                                logger.info("mplib.current_user", extra={"data": {"exception": e}})

                    except facebook.GraphAPIError:
                        user = None
                    else:
                        user = FacebookUser(
                            pk=str(profile["id"]),
                            first_name=profile.get("first_name"),
                            last_name=profile.get("last_name"),
                            locale=profile.get("locale"),
                            gender=profile.get("gender", ""),
                            time_zone=profile.get("timezone", ""),
                            email=profile.get("email", ""),
                            access_token=cookie["access_token"],
                        )

                        try:
                            user.save()
                        except IntegrityError as e:
                            logger.info(
                                "IntegrityError saving user", extra={"data": {"exception": e, "profile": profile}}
                            )

                            # this user has already been saved somehow, let's skip
                            # over this problem and grab him from the db
                            user = FacebookUser.objects.get(pk=str(profile["id"]))

                        ip = FacebookUserIP()

                        ip.fb_user = user
                        ip.ip_address = ip_address(self.request)

                        ip.save()

                else:
                    if user.access_token != cookie["access_token"]:
                        user.access_token = cookie["access_token"]

                        ip = FacebookUserIP()

                        ip.fb_user = user
                        ip.ip_address = ip_address(self.request)

                        ip.save()
                        user.save()

                self._current_user = user
        return self._current_user