向openid用户提供其他信息 [英] give openid users additional information

查看:65
本文介绍了向openid用户提供其他信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在jinja2模板平台上用python为google appengine编写应用程序.我已经使OpenID在我的网站上正常工作,它允许用户登录,并且可以在右上角显示其电子邮件/ID.

So I am writing an app in python for google appengine on the jinja2 templating platform. I have gotten OpenID to work just fine on my site and it allows the user to login and I can display their email/ID up in the top right corner.

现在,我希望用户拥有自己的用户名,并能够存储有关他们的一些其他数据,例如站点访问和站点区域的某些帖子.

Now I want users to have their own usernames and be able to store some additional data about them such as site visits and certain posts to areas of the site.

我知道我需要在数据库中创建一个包含用户和所有内容的表,但是使用OpenID进行此操作的正确方法是什么.我想在某人首次登录时自动创建用户,但是我不确定如何有效地执行此操作.我认为我可以通过在登录后将其重定向到检查数据库中federated_identity的页面并将其重定向到页面上来进行管理,如果该页面已经存在,它将仅将其重定向到主页上,否则它将创建一个新用户.

I know I need to create a table in the database with users and everything but what is the correct way to do this with OpenID. I want to automatically create the user when someone logs in for the first time but I am not quite sure how to do this efficiently. I think I can manage it by redirecting them to a page after login that checks their federated_identity in a database and if it exists there already it just redirects them on to the home page but if not it creates a new user.

有没有更有效的方法,这样我就不必每次有人登录时都查询数据库,或者这是一种相当不错的方法?

Is there a more efficient way so that I am not querying the database everytime someone logs in or is that a pretty decent way to do it?

推荐答案

您是对的,您必须创建自己的用户模型并存储额外的信息以及应用程序中具有的任何业务逻辑.您要做的就是存储唯一的内容(例如federated_id)以及多余的usernamename等.

You are right, you have to create your own User model and store the extra information and whatever business logic you have in your application. All you have to do is to store something unique (federated_id for example) along with the extra username, name, etc.

使用OpenID登录后,您将检查特定的federated_id是否在数据存储区中,并将返回该特定的user_db,或者使用默认值创建一个新的user_db,并返回新创建的user_db.

After logging in with the OpenID, you will check if that particular federated_id is in your datastore and you will return that particular user_db, or create a new one with default values and return the newly created user_db.

重要的是,在您的请求中,您将始终处理自己的user_db实体,而不是users.get_current_user()实体,因为这基本上是只读且受限制的.

The important thing is that in your requests you will always deal with your own user_db entity and not the users.get_current_user() because this is basically read only and limited.

这是一个完整的示例,还演示了基本处理程序类get_user()函数,该函数将返回您的如果用户已登录,则拥有user_db实体,否则为None.如果用户是第一次登录,则会使用一些默认值(nameusernameemailadminfederated_id)创建一个新的user_db实体:

Here is a full example, that also demonstrates a base handler class with the get_user() function that will return your own user_db entity if the user is logged in and None otherwise. If the user is going to login for the first time, a new user_db entity is created with some default values (name, username, email, admin, federated_id):

from google.appengine.api import users
from google.appengine.ext import ndb
import webapp2

class User(ndb.Model):
  name = ndb.StringProperty(required=True)
  username = ndb.StringProperty(required=True)
  email = ndb.StringProperty()
  federated_id = ndb.StringProperty()
  admin = ndb.BooleanProperty()

class BaseHandler(webapp2.RequestHandler):
  def get_user_db(self):
    federated_user = users.get_current_user()
    if not federated_user:
      return None
    user_db_qry = User.query(User.federated_id == federated_user.user_id())
    user_db_list = user_db_qry.fetch(1)
    #If that returns non empty list, then return the first element which is the user_db
    if user_db_list:
      return user_db_list[0]

    #Otherwise create a new user_db entity, with default values and return
    user_db = User(
        name=federated_user.nickname().title(),
        username=federated_user.nickname(),
        email=federated_user.email(),
        federated_id=federated_user.user_id(),
        admin=users.is_current_user_admin(),
      )
    user_db.put()
    return user_db

class MainHandler(BaseHandler):
  def get(self):
    self.response.headers['Content-Type'] = 'text/html'
    if self.get_user_db():
      self.response.out.write('Hello, %s!</br>' % self.get_user_db().name)
      self.response.out.write('<a href="%s">Logout</a>' % (users.create_logout_url(self.request.uri)))
    else:
      self.response.out.write('Hello, stranger!<br>')
      self.response.out.write('<a href="%s">Login</a>' % (users.create_login_url(self.request.uri)))

app = webapp2.WSGIApplication([('/', MainHandler)],
                              debug=True)

解决这个问题的方法很多,但是我认为这是一个很好的开始.

There are many different approaches to this problem, but I think this is a good start to get the idea.

这篇关于向openid用户提供其他信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆