def RequirePicturesqueUser(cls):
    """Makes sure the user from the environment has a Picturesque account.

    Checks first that there is a valid endpoints user, then checks if the
    current token can allow access to the user's Google+ ID and finally
    checks that a corresponding PicturesqueUser for that Google+ ID exists.

    Returns:
      The PicturesqueUser entity corresponding to the token user from the
        environment.

    Raises:
      endpoints.UnauthorizedException: If there is no endpoints current user.
        This results in a 401 response.
      endpoints.ForbiddenException: If either the token can't access the Google+
        ID or no Picturesque account exists for the user. This results in a 403
        response.
    """
    current_user = endpoints.get_current_user()
    if current_user is None:
      raise endpoints.UnauthorizedException(cls.INVALID_TOKEN)

    googleplus_user_id = auth_util.get_google_plus_user_id()
    if googleplus_user_id is None:
      raise endpoints.ForbiddenException(cls.NO_GPLUS_ID)

    existing_picturesque_user = cls.get_by_id(googleplus_user_id)
    if existing_picturesque_user is None:
      raise endpoints.ForbiddenException(cls.NO_ACCOUNT)

    return existing_picturesque_user
  def SignUp(self, unused_request):
    """Sign up to create a Picturesque user account."""

    # Args:
    #   unused_request: An instance of message_types.VoidMessage. This allows us
    #     the method to require no input (other than a token).

    # Returns:
    #   The instance of PicturesqueUser that was either created or already
    #     existed.

    # Raises:
    #   endpoints.ForbiddenException: if the token can't access the current
    #     user's Google+ ID. This results in a 403 response.
    # """
    googleplus_user_id = auth_util.get_google_plus_user_id()
    if googleplus_user_id is None:
      raise endpoints.ForbiddenException(PicturesqueUser.NO_GPLUS_ID)

    # Will not be null since user_required=True
    current_user = endpoints.get_current_user()
    return PicturesqueUser.GetOrCreateAccount(current_user, googleplus_user_id)
Exemplo n.º 3
0
 def check_user_token(*args, **kwargs):
     user_id = auth_util.get_google_plus_user_id()
     if user_test_function(user_id):
         return func(*args, **kwargs)
     else:
         raise endpoints.UnauthorizedException('User does not have access to this endpoint: %s' % user_id)