Original If you have any mistranslations, please let us know.
User Objects (Note: The OpenID features mentioned on this page are expected to be deprecated and removed. For more information, see OpenID 2.0 Support Turndown. See / open_id).)
An instance of the User class represents a user. User instances can be compared with their own. If the two instances are the same, then they belong to the same user. The application calls the users.get_current_user () function for that user. You can access the User instance.
from google.appengine.api import users
user = users.get_current_user()
if not user:
# The user is not signed in.
else:
print "Hello, %s!" % user.nickname()
You can use the users.get_current_user () function regardless of which authentication option you use for your application. A User instance can be said to consist of an email address.
user = users.User("[email protected]")
Alternatively, if you have federated_identity, you can also use the User instance as below.
user = users.User(federated_identity="http://example.com/id/ajohnson")
If User's content is called with an email address that does not match your existing Google account, a new User Object will be created, which will not match your actual Google account. This also happens when someone creates another Google account after their User Object is registered in the database. User information that has already been registered will not match when a new Google account is created. When running the development server, all registered User objects will be treated as valid Google accounts. A valid user's User object gives each user a unique ID that doesn't change until the user changes their email address. The user_id () method returns the ID as a string. No matter which authentication method you use in your application, the User object has the same form. If you change the authentication option from your Google account to OpenID, your existing User object will remain valid.
The user ID is fixed. The user ID can be used as a key name or a character string. Therefore, when using User information, you probably want to save your user ID (and maybe your most recent email address to communicate with the user by email). The example below shows how to match the user with the user ID.
from google.appengine.api import users
from google.appengine.ext import ndb
class UserPrefs(ndb.Model):
userid = ndb.StringProperty()
user = users.get_current_user()
if user:
q = ndb.gql("SELECT * FROM UserPrefs WHERE userid = :1", user.user_id())
userprefs = q.get()
We strongly recommend that you do not store the UserProperty in the database as it contains your email address and your unique ID. If the user changes their email address, trying to match the saved old User with the new User will not match. Instead, it's better to provide a user-specific ID and match it.
Recommended Posts